Popular recipes tagged "cache"http://code.activestate.com/recipes/tags/cache/2013-03-06T06:04:18-08:00ActiveState Code RecipesA memoize decorator for instance methods (Python)
2010-11-04T20:23:35-07:00Daniel Millerhttp://code.activestate.com/recipes/users/4016391/http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/
<p style="color: grey">
Python
recipe 577452
by <a href="/recipes/users/4016391/">Daniel Miller</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/functools/">functools</a>, <a href="/recipes/tags/memoize/">memoize</a>, <a href="/recipes/tags/partial/">partial</a>).
</p>
<p>A simple result-caching decorator for instance methods. NOTE: does not work with plain old non-instance-method functions. The cache is stored on the instance to prevent memory leaks caused by long-term caching beyond the life of the instance (almost all other recipes I found suffer from this problem when used with instance methods).</p>
Py2.6+ and Py3.0+ backport of Python 3.3's LRU Cache (Python)
2013-03-06T05:38:15-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/
<p style="color: grey">
Python
recipe 578078
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>).
Revision 6.
</p>
<p>Full-featured O(1) LRU cache backported from Python3.3. The full Py3.3 API is supported (thread safety, maxsize, keyword args, type checking, __wrapped__, and cache_info). Includes Py3.3 optimizations for better memory utilization, fewer dependencies, and fewer dict lookups.</p>
Probably the fastest memoization decorator in the world (Python)
2012-08-02T07:27:12-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-decorator-in-the-/
<p style="color: grey">
Python
recipe 578231
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memo/">memo</a>, <a href="/recipes/tags/memoization/">memoization</a>).
</p>
<p>With apologies to a certain European brand of beer, this is probably the fastest memoization decorator in the world. Implemented using the __missing__ method in a dict subclass. </p>
Cached Class (Python)
2012-01-06T02:24:08-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/577998-cached-class/
<p style="color: grey">
Python
recipe 577998
by <a href="/recipes/users/4180313/">Peter Donis</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorators/">decorators</a>).
Revision 5.
</p>
<p>A class decorator that ensures that only one instance of
the class exists for each distinct set of constructor
arguments.</p>
<p>Note that if a decorated class is subclassed, each subclass is cached separately. (This is because each cached subclass is a different <code>cls</code> argument to the <code>__new__</code> method.)</p>
Simplified, highly optimized LRU Cache (Python)
2011-12-01T00:57:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577969-simplified-highly-optimized-lru-cache/
<p style="color: grey">
Python
recipe 577969
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>).
Revision 3.
</p>
<p>An O(1) LRU cache. Short, sweet, and fast. No bells and whistles.</p>
JSON instead of pickle for memcached (Python)
2012-01-10T22:31:36-08:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578011-json-instead-of-pickle-for-memcached/
<p style="color: grey">
Python
recipe 578011
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/memcache/">memcache</a>, <a href="/recipes/tags/memcached/">memcached</a>, <a href="/recipes/tags/pickle/">pickle</a>).
</p>
<p>Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) does'n know pickle, but json yes.</p>
Simplified LRU Cache (Python)
2013-03-06T06:04:18-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577970-simplified-lru-cache/
<p style="color: grey">
Python
recipe 577970
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>).
Revision 6.
</p>
<p>An O(1) LRU cache. Short, sweet, and fast.</p>
Simple caching decorator (Python)
2010-12-01T00:43:01-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577479-simple-caching-decorator/
<p style="color: grey">
Python
recipe 577479
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/memoization/">memoization</a>).
</p>
<p>Memoizing decorator. Has the same API as the functools.lru_cache() in Py3.2 but without the LRU feature, so it takes less memory, runs faster, and doesn't need locks to keep the dictionary in a consistent state.</p>
Simple local cache and cache decorator (Python)
2010-12-08T09:06:34-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577492-simple-local-cache-and-cache-decorator/
<p style="color: grey">
Python
recipe 577492
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Simple local cache.
It saves local data in singleton dictionary with convenient interface</p>
<h4 id="examples-of-use">Examples of use:</h4>
<pre class="prettyprint"><code># Initialize
SimpleCache({'data':{'example':'example data'}})
# Getting instance
c = SimpleCache.getInstance()
c.set('re.reg_exp_compiled',re.compile(r'\W*'))
reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*'))
# --------------------------------------------------------------
c = SimpleCache.getInstance()
reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*'))
# --------------------------------------------------------------
@scache
def func1():
return 'OK'
</code></pre>
Method Caching (Python)
2010-07-27T20:26:08-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577338-method-caching/
<p style="color: grey">
Python
recipe 577338
by <a href="/recipes/users/4174445/">Danillo Souza</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Save each set of parameters with your respective return value in cache.</p>
Cached Property (Python)
2010-05-19T22:38:24-07:00Ken Seeharthttp://code.activestate.com/recipes/users/4167995/http://code.activestate.com/recipes/576563-cached-property/
<p style="color: grey">
Python
recipe 576563
by <a href="/recipes/users/4167995/">Ken Seehart</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/property/">property</a>).
</p>
<p>A cached property is a read-only property that is calculated on demand and automatically cached. If the value has already been calculated, the cached value is returned.</p>