Flask-CacheOBJ APIs

This part of the documentation covers all interfaces of Flask-CacheOBJ.

class flask_cacheobj.CacheOBJ(app=None)

Use redis as cache layer for Flask applications.

Register it with:

app = Flask(__name__)
cache = FlaskCacheOBJ(app)

Or:

app = Flask(__name__)
cache = FlaskCacheOBJ()
cache.init_app(app)
counter

A decorator that can cache counter. Alias for flask_cacheobj.cache.cache_counter.

Define counter cache strategy and decorator your function:

STAT = {
    'key': 'item:stat:{item_id}',
    'expire': 60,
}

@cache.counter(STAT)
def get_item_stat(item_id):
    return to_dict(ItemStat.query.filter_by(item_id).first())
delete

A decorator that can delete object after function executed. Alias for flask_cacheobj.cache.delete_obj.

Define cache strategy and decorate your function:

ITEM = {
    'key': 'item:{item_id}',
    'expire': 86400,
}

@cache.delete(ITEM)
def update_item(item_id, attributes):
    return Item.query.get(item_id).update(**attributes)
hash

A decorator that can cache hash. Alias for flask_cacheobj.cache.cache_hash.

Define hash cache strategy and decorator your function:

ITEM_HASH = {
    'hash_key': 'item',
    'key': '{item_id}',
    'expire': 86400,
}

@cache.hash(ITEM)
def get_item(item_id):
    return to_dict(Item.query.get(item_id))
list

A decorator that can cache list. Alias for flask_cacheobj.cache.cache_list.

Define list cache strategy and decorator your function:

MEMBERS = {
    'key': 'item:members:{item_id}',
    'expire': 60,
}

@cache.list(STAT)
def get_item_members(item_id):
    members = ItemMember.query.filter_by(item_id).all()
    return [member.user_id for member in members]
mc

Redis instance used to cache value. Replace redis instance if you want.

Usage:

cache.mc = redis.StrictRedis()
obj

A decorator that can cache object. Alias for flask_cacheobj.cache.cache_obj.

Define cache strategy and decorate your function:

ITEM = {
    'key': 'item:{item_id}',
    'expire': 86400,
}

@cache.obj(ITEM)
def get_item(item_id):
    return to_dict(Item.query.get(item_id))
flask_cacheobj.cache.cache_obj(cache_key_reg, packer=<function encode>, unpacker=<function decode>)

A decorator that can cache function result.

Example:

@cache_obj({'key': 'item:{item_id}'})
def get_item(item_id):
    return Item.query.get(item_id)

@cache_obj({'key': 'item:{item_id}', 'expire': 60})
def get_item(item_id):
    return Item.query.get(item_id)

@cache_obj({'key': lambda filename: md5(filename).hexdigest()})
def get_file(filename):
    return remote.fetch_file_data(filename)
Parameters:
  • cache_key_reg – a dict-like object contains key and expire. cache_obj will inspect function arguments and format cache_key_reg[‘key’] if cache_key_reg[‘key’] is a string, or apply them to cache_key_reg[‘key’] if cache_key_reg[‘key’] is a callable object. expire is an integer. The unit is second.
  • packer – a method used to encode function result.
  • unpacker – a callable object used to decode msgpack data.
flask_cacheobj.cache.delete_cache(cache_key_reg, **kw)

Delete cache via cache key registry.

Example:

delete_cache({'key': 'item:{item_id}'}, item_id=1)
flask_cacheobj.cache.cache_hash(cache_key_reg, packer=<function encode>, unpacker=<function decode>)

A decorator that can cache function result into redis hash.

Example:

@cache_hash({'key': '{item_id}', 'hash_key': 'item'})
def get_item(item_id):
    return Item.query.get(item_id)
Parameters:cache_key_reg – a dict-like object contains hash_key, key. hash_key is a string as redis hash key. key is a template string or a callable object as hash field key.
flask_cacheobj.cache.hash_del(cache_key_reg, **kw)

Delete hash field.

Example:

hash_del({'key': '{item_id}', 'hash_key': 'item'}, item_id=1)
flask_cacheobj.cache.cache_list(cache_key_reg, packer=<type 'str'>, unpacker=<type 'int'>)
flask_cacheobj.cache.cache_counter(cache_key_reg, packer=<type 'str'>, unpacker=<type 'int'>)

A decorator that can cache counter function result.

Example:

@cache_counter({'key': 'group_members:{group_id}', 'expire': 60})
def get_group_members_count(group_id):
    return GroupMember.query.filter_by(group_id).count()
Parameters:cache_key_reg – a dict-like object contains key and expire.
flask_cacheobj.cache.inc_counter(cache_key_reg, delta=1, **kw)

Increase counter value.

Parameters:
  • cache_key_reg – a dict-like object contains key.
  • delta – increment delta. an integer. Default is 1.
  • kw – A dict of named parameters used to format cache_key_reg[‘key’].
flask_cacheobj.cache.dec_counter(cache_key_reg, delta=1, **kw)

Decrease counter value.

Parameters:
  • cache_key_reg – a dict-like object contains key.
  • delta – decrement delta. an integer. Default is 1.
  • kw – A dict of named parameters used to format cache_key_reg[‘key’].
flask_cacheobj.cache.get_counter(cache_key_reg, **kw)

Get counter value.

Parameters:
  • cache_key_reg – a dict-like object contains key.
  • kw – A dict of named parameters used to format cache_key_reg[‘key’].
flask_cacheobj.cache.set_counter(cache_key_reg, value=0, **kw)

Set counter value.

Parameters:
  • cache_key_reg – a dict-like object contains key.
  • value – reset counter value with given value.
  • kw – A dict of named parameters used to format cache_key_reg[‘key’].