python - flask: How to store and retrieve POST calls? -


@app.route('/path/<user>/<id>', methods=['post']) @cache.cached(key_prefix='/path/<user>', unless=true) def post_kv(user, id):     cache.set(user, id)         return value  @app.route('/path/<user>', methods=['get']) @cache.cached(key_prefix='/path/<user>', unless=true) def getkv(user):     cache.get(**kwargs) 

i want able make post calls path described, store them, , values user. above code runs, has bugs , doesn't perform needed. frankly, flask-cache docs aren't helping. how can implement cache.set , cache.get perform needed?

in flask, implementing custom cache wrapper simple.

from werkzeug.contrib.cache import simplecache, memcachedcache  class cache(object):      cache = simplecache(threshold = 1000, default_timeout = 100)     # cache = memcachedcache(servers = ['127.0.0.1:11211'], default_timeout = 100, key_prefix = 'my_prefix_')      @classmethod     def get(cls, key = none):         return cls.cache.get(key)      @classmethod     def delete(cls, key = none):         return cls.cache.delete(key)      @classmethod     def set(cls, key = none, value = none, timeout = 0):         if timeout:             return cls.cache.set(key, value, timeout = timeout)         else:                 return cls.cache.set(key, value)      @classmethod     def clear(cls):         return cls.cache.clear()  ...  @app.route('/path/<user>/<id>', methods=['post']) def post_kv(user, id):     cache.set(user, id)     return "cached {0}".format(id)  @app.route('/path/<user>', methods=['get']) def get_kv(user):     id = cache.get(user)     return "from cache {0}".format(id) 

also, simple memory cache single process environments , simplecache class exists development server , not 100% thread safe. in production environments should use memcachedcache or rediscache.

make sure implement logic when item not found in cache.


Comments

Popular posts from this blog

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -