Package: lru2cache
Aleast recently used (LRU)2 layer caching mechanism based in part on the Python 2.7 back-port offunctools.lru_cacheThis was developed by3Top, Inc.for use with
our ranking and recommendation platform,http://www.3top.com.lru2cache is a decorator that can be used with any user function or method to
cache the most recent results in a local cache and using the django cache
framework to cache results in a shared cache.The first layer of caching is stored in a callable that wraps the function or
method.  As with ‘functools.lru_cache’ a dict is used to store the cached
results, therefore positional and keyword arguments must be hashable. Each
instance stores up tol1_maxsizeresults that vary on the arguments. The
discarding of the LRU cached values is handled by the decorator.The second layer of caching requires a shared cache that can make use of
Django’s cache framework.  In this case it is assumed that any LRU mechanism
is handled by the shared cache backend.This arrangement allows a process that accesses a function multiple times to
retrieve the value without the expense of requesting it from a shared cache,
while still allowing different processes to access the result from the shared
cache.Installation & ConfigurationThe easiest and best way to install this is with pip:pip install lru2cacheIf available this package will use SpookyHash V2 as a hashing mechanism.
Spooky is a good fast hashing algorithm that should be suitable for most uses.
If it is not available the package will fall back to SHA-256 from the standard
hashlib.  Because SHA-256 is a proper cryptographic hash it requires more
computation than Spooky.  To install spooky use pip:pip install spooky 2Once lru2cache is installed you will need to configure a shared cache as an
l2 cache.  If you are using Django your settings file will contain something
similar to the following in the settings file:CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 1200,
    },
    'l2cache': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': None,
    },
}If you do not want to use eitherdefaultorl2cacheyou will need to
specify the name of the cache.Benefits Over functools.lru_cacheLocal and Shared Cache- Combining both types of cache is much more
effective than either used on it’s own.  The local cache eliminates the
latency of calls to a shared cache, while the shared cache eliminates
the expense of returning the resultThe Ability to Not Cache None Results- This may seem like a minor thing
but in our environment it has greatly reduced the frequency of cache
invalidations.Usage@utils.lru2cache(l1_maxsize=128, none_cache=False, typed=False, l2cache_name='l2cache', inst_attr='id')Usage is as simple as adding the decorator to a function or method as seen in
the below examples from our test cases:from lru2cache import utils

@utils.lru2cache()
def py_cached_func(x, y):
    return 3 * x + y


class TestLRUPy(TestLRU):
    module = utils
    cached_func = py_cached_func,

    @utils.lru2cache()
    def cached_meth(self, x, y):
        return 3 * x + y

    @staticmethod
    @utils.lru2cache()
    def cached_staticmeth(x, y):
        return 3 * x + yIfl1_maxsizeis set toNone, the LRU feature is disabled and the L1 cache
can grow without bound. The LRU feature performs best when maxsize is a power-of-two.ifnone_cacheisTruethanNoneresults will be cached, otherwise they
will not.Iftypedis set toTrue, function arguments of different types will be
cached separately. For example, f(3) and f(3.0) will be treated as distinct
calls with distinct results.Ifl2cache_nameis specified it will be used as the shared cache.  Otherwise
it will attempt to use a cache namedl2cacheand if not found fall back todefault.inst_attris the attribute used to uniquely identify an object when wrapping
a method.  In Django this will typically beidhowever if it is not you will
need to specify what attribute should be used.Cache ManagementSince the lru2cache decorator does not provide a timeout for its cache although
it provides other mechanisms for programatically managing the cache.Cache StatisticsAs with lru_cache, one can view the cache statistics via a named tuple
(l1_hits, l1_misses, l2_hits, l2_misses, l1_maxsize, l1_currsize), withf.cache_info(). These stats are stored within an instance, and therefore
are specific to that instance. Cumulative statistics for the shared cache would
need to be obtained from the shared cache.Clearing Instance Cachethe cache and statistics associated with a function or method can be cleared with:f.cache_clear()Clearing Shared CacheA shared cache can easily be cleared with the following:from django.core import cache

lru2cache_cache = cache.get_cache('l2cache')
lru2cache_cache.clear()Invalidating Cached ResultsTo invalidate the cache for a specific set of arguments, including the instance
one can pass the same arguments to invalidate the both L1 and L2 caches:f.invalidate(*args, **kwargs)in the case of a method you do need to explicitly pass the instance as in the
following:foo.f.invalidate(foo, a, b)Refreshing the CacheThis is not yet implemented as a function but can be accomplished by first calling
invalidate and then calling the functionAccessing the Function without CacheThe un-cached underlying function can always be accessed withf.__wrapped__.Background and DevelopmentAt3TopWe needed a way to improve performance of
slow queries, not just those using the Django ORM, but also for queries to
other data stores and services.  We started off with a simpler centralized
caching solution using Memcached, but even those queries, when called frequently,
can start to cause delays.  Therefore we sought a means of caching at two layers.Initially we looked at the possibility of using two different mechanisms but
we quickly saw the advantage of maintaining the same set of keys for both
caches and decided to create our own mechanism.We used a backport python 3functools.lru_cache()decorator as a starting
point for developing an in instance cache with LRU capabilities.  However we
needed to ensure the keys would also be unique enough to use with a shared
cache. We leverage Django’s excellent cache framework for managing the layer 2
cache. This allows the use of any shared cache supported by Django.TestsAs a starting point I incorporated most of the tests forfunctools.lru_cache()with minor changes to make them work with python 2.7
and incorporated the l2_cache stats. We will continue to add tests to validate
the additional functionality provided by this decorator.