Friday, August 20, 2010

Django cache (storage)

If you need to store something useful between client requests, you can use Django cache framework.

It allows you to use memory, file, database or custom storage type.

Its using is very simple:

Add CACHE_BACKEND = 'locmem://' in settings to use memory cache

Import cache: "from django.core.cache import cache" in .py file.
Get value: cache.get('stored_data'). It returns None if there is no 'stored_data'.
Set value: cache.set('stored_data', my_data, 60). '60' is optional - timeout of cache expiration.
Delete value: cache.delete('stored_data')