How do I use Flask-Cache @cache.cached() decorator with Flask-Restful? For example, I have a class Foo inherited from Resource, and Foo has get, post, put, and delete methods.
How can I can invalidate cached results after a POST
?
@api.resource('/whatever') class Foo(Resource): @cache.cached(timeout=10) def get(self): return expensive_db_operation() def post(self): update_db_here() ## How do I invalidate the value cached in get()? return something_useful()
By default, we have null, which means that there will be no cache until and unless specified. There are various other types of cache present and the most widely used is the simple cache method and other than that one may use as per the desirability of the app that is being developed using flask.
Flask is a highly flexible Python web framework built with a small core and easy-to-extend philosophy. Flask-Restful is an extension of Flask that offers extension to enable writing a clean object-oriented code for fast API development.
As Flask-Cache
implementation doesn't give you access to the underlying cache
object, you'll have to explicitly instantiate a Redis
client and use it's keys
method (list all cache keys).
cache_key
method is used to override the default key generation in your cache.cached
decorator.clear_cache
method will clear only the portion of the cache corresponding to the current resource.This is a solution that was tested only for Redis
and the implementation will probably differ a little when using a different cache engine.
from app import cache # The Flask-Cache object from config import CACHE_REDIS_HOST, CACHE_REDIS_PORT # The Flask-Cache config from redis import Redis from flask import request import urllib redis_client = Redis(CACHE_REDIS_HOST, CACHE_REDIS_PORT) def cache_key(): args = request.args key = request.path + '?' + urllib.urlencode([ (k, v) for k in sorted(args) for v in sorted(args.getlist(k)) ]) return key @api.resource('/whatever') class Foo(Resource): @cache.cached(timeout=10, key_prefix=cache_key) def get(self): return expensive_db_operation() def post(self): update_db_here() self.clear_cache() return something_useful() def clear_cache(self): # Note: we have to use the Redis client to delete key by prefix, # so we can't use the 'cache' Flask extension for this one. key_prefix = request.path keys = [key for key in redis_client.keys() if key.startswith(key_prefix)] nkeys = len(keys) for key in keys: redis_client.delete(key) if nkeys > 0: log.info("Cleared %s cache keys" % nkeys) log.info(keys)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With