Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for adding POST params into a cache key #150

Closed
havok2063 opened this issue Dec 12, 2019 · 4 comments
Closed

support for adding POST params into a cache key #150

havok2063 opened this issue Dec 12, 2019 · 4 comments

Comments

@havok2063
Copy link

Some routes in my application are POST routes called via AJAX or an API. The route itself has no arguments but I process the request form for input parameters. Is there a way to add these parameters to the cache_key? Toy example..

class DoStuff(FlaskView):
    @route('/dostuff/')
    @cache.memoize(timeout=300)
    def dostuff(self):
          args = process_request(request)
          objectid = args.get('objectid')
          ...
          ... do some stuff
          return jsonify(....)

I'd like to be able to have this route cached based on the input form parameters, like objectid. Is there a way to do this? It's not clear in the documentation.

@bimhud
Copy link

bimhud commented May 1, 2020

I'm having the same issue when trying to apply cache for POST request. @havok2063 Not sure if you find a way out? In the other scenario, I'm thinking to use a standalone redis cache for doing this, but not sure if I can use flask-caching?

@bimhud
Copy link

bimhud commented May 1, 2020

The other way out is to required unique key (UUID) as a part of request_path, that can be composed as an md5 hash on the post payload or params. So, if a client wants to get benefits from cache, they have to provide that key

@havok2063
Copy link
Author

@bimhud Yes I did find a way to do it. I utilized the make_cache_key function on the memoize decorator. See the memoize API. I had to dig through the API and source code to figure out how to use it. Essentially you need to define a new function that creates the cache key and set that function as the make_cache_key function. Inside that new function you can grab the Flask request arguments. Here is an example using the above example route:

from flask import request 

# Flask route definition
class DoStuff(FlaskView):
    @route('/dostuff/')
    @cache.memoize(timeout=300)
    def dostuff(self):
          args = process_request(request)
          objectid = args.get('objectid')
          ...
          ... do some stuff
          return jsonify(....)

# create the cache key definition function
def get_dostuff_cache_key(*args, **kwargs):
    args = process_request(request)
    objectid = args.get('objectid')
    ... do some stuff
    key = 'custom_cache_key_{0}'.format(objectid)
    return key

# attach function 
DoStuff.dostuff.make_cache_key = get_dostuff_cache_key

@sh4nks
Copy link
Collaborator

sh4nks commented May 31, 2020

See PR #159

@sh4nks sh4nks closed this as completed May 31, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants