Redis get and set decorator
<p>Currently working on python and redis. I have Flask as my framework and working on Blueprints.</p>

<p>Looking into implementing cache with redis for my APIs, I have tried <strong>Flask-Cache</strong> and <strong>redis-simple-cache</strong>.
Downside are <a href="http://pythonhosted.org/Flask-Cache/" rel="nofollow">Flask-Cache</a> saves the function even when you change the parameter of the function. It only saves it once per function.
As per <a href="https://github.com/vivekn/redis-simple-cache" rel="nofollow">redis-simple-cache</a>, it save its keys as <code>SimpleCache-&lt;key name&gt;</code> which not advisable on my end.</p>

<p>So my question is, how can you create a decorator which save and retrieve or check if there is a key existing for the specific key.
I know a save decorator is possible. But is a retrieve or check decorator possible?? Please correct me if I am wrong. Thank you.</p>

<p>You seem to not have read the <a href="http://pythonhosted.org/Flask-Cache/" rel="nofollow">Flask-Cache documentation</a> very closely. Caching does <em>not</em> ignore parameters and the cache key is customisable. The decorators the project supplies already give you the functionality you seek.</p>

<p>From the <a href="http://pythonhosted.org/Flask-Cache/#caching-view-functions" rel="nofollow"><em>Caching View Functions</em> section</a>:</p>

<blockquote>
  <p>This decorator will use <code>request.path</code> by default for the <code>cache_key</code>.</p>
</blockquote>

<p>So the <em>default</em> cache key is <code>request.path</code>, but you can specify a different key. Since Flask <em>view</em> functions get their arguments from path elements, the default <code>request.path</code> makes a great key.</p>

<p>From the <a href="http://pythonhosted.org/Flask-Cache/#flask.ext.cache.Cache.cached" rel="nofollow"><code>@cached()</code> decorator documentation</a>:</p>

<blockquote>
  <p><code>cached(timeout=None, key_prefix='view/%s', unless=None)</code></p>
  
  <p>By default the cache key is <em>view/request.path</em>. You are able to use this decorator with any function by changing the <em>key_prefix</em>. If the token <em>%s</em> is located within the <em>key_prefix</em> then it will replace that with <em>request.path</em>. You are able to use this decorator with any function by changing the <em>key_prefix</em>.</p>
</blockquote>

<p>and</p>

<blockquote>
  <p><code>key_prefix</code> â <em>[...]</em> Can optionally be a callable which takes no arguments but returns a string that will be used as the <code>cache_key</code>.</p>
</blockquote>

<p>So you can set <code>key_prefix</code> to a function, and it'll be called (without arguments) to produce the key.</p>

<p>Moreover:</p>

<blockquote>
  <p>The returned decorated function now has three function attributes assigned to it. These attributes are readable/writable:</p>
  
  <p><em>[...]</em></p>
  
  <p><code>make_cache_key</code><br>
  A function used in generating the cache_key used.</p>
</blockquote>

<p>This function is passed the same arguments the view function is passed. In all, this allows you to produce any cache key you want; either use <code>key_prefix</code> and pull out more information from the <code>request</code> or <code>g</code> or other sources, or assign to <code>view_function.make_cache_key</code> and access the same arguments the view function receives.</p>

<p>Then there is the <a href="http://pythonhosted.org/Flask-Cache/#flask.ext.cache.Cache.memoize" rel="nofollow"><code>@memoize()</code> decorator</a>:</p>

<blockquote>
  <p><code>memoize(timeout=None, make_name=None, unless=None)</code></p>
  
  <p>Use this to cache the result of a function, taking its arguments into account in the cache key.</p>
</blockquote>

<p>So this decorator caches return values purely based on the arguments passed into the function. It too supports a <code>make_cache_key</code> function. </p>

<p>I've used both decorators to make a Google App Engine Flask project scale to double-digit millions of views per month for a CMS-backed site, storing results in the Google memcached structure. Doing this with Redis would only require setting a configuration option.</p>

python caching redis decorator