cache_it is a decorator which wraps caching(read/write) routine. Decorated functions automatically access your memcache and get/set values depending on the situation. All you have to do is decorate your function by @cache_it.
>>> from cache_it import init_cache, cache_it
>>> init_cache(['127.0.0.1:11211'])
>>> @cache_it(prefix='PREFIX:')
>>> def cached_function(key):
>>> return 'value'
>>> cached_function('key')
'value' # cached => key:'PREFIX:key' value:'value'
>>> cached_function('key')
'value' # getting from a cache
You can decorate not only functions but also methods in the same way.
class User(Document)
@cache_it(prefix='PREFIX')
def __getitem__(self, key):
return user[key]
If you want to ignore exceptions occured when accessing memcache, you have to set "ignore_exception" into True.
@cache_it(prefix='PREFIX', ignore_exception=True)
def cached_function(key):
return 'value'
@cache_it(prefix='PREFIX', ttl=60) # 60sec
def cached_function(key):
return 'value'
Encoders are called before setting a value. Decoders, on the other hand, are called after getting a value.
@cache_it(prefix='PREFIX',
encoder=lambda x: x['foo'],
decoder=lambda x: {'foo': x})
def cached_function(key):
return {'foo': 'bar'}