Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Flask-Cache with Flask-Restful

Tags:

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() 
like image 318
cyberra Avatar asked Jul 18 '14 03:07

cyberra


People also ask

Does Flask-cache by default?

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.

What is the difference between Flask and flask RESTful?

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.


1 Answers

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).

  • The cache_key method is used to override the default key generation in your cache.cached decorator.
  • The 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) 
like image 103
JahMyst Avatar answered Sep 24 '22 06:09

JahMyst