Package: django-cache-memoize
License: MPL 2.0Django utility for a memoization decorator that uses the Django cache framework.For versions of Python and Django, check outthe tox.ini file.Key FeaturesMemoized function calls can be invalidated.Works with non-trivial arguments and keyword argumentsInsight into cache hits and cache missed with a callback.Ability to use as a “guard” for repeated execution when storing the function
result isn’t important or needed.Installationpipinstalldjango-cache-memoizeUsage# Import the decoratorfromcache_memoizeimportcache_memoize# Attach decorator to cacheable function with a timeout of 100 seconds.@cache_memoize(100)defexpensive_function(start,end):returnrandom.randint(start,end)# Just a regular Django viewdefmyview(request):# If you run this view repeatedly you'll get the same# output every time for 100 seconds.returnhttp.HttpResponse(str(expensive_function(0,100)))The caching usesDjango’s default cache framework. Ultimately, it callsdjango.core.cache.cache.set(cache_key, function_out, expiration).
So if you have a function that returns something that can’t be pickled and
cached it won’t work.For cases like this, Django exposes a simple, low-level cache API. You can
use this API to store objects in the cache with any level of granularity
you like. You can cache any Python object that can be pickled safely:
strings, dictionaries, lists of model objects, and so forth. (Most
common Python objects can be pickled; refer to the Python documentation
for more information about pickling.)Seedocumentation.Example UsageThis blog post:How to use django-cache-memoizeIt demonstrates similarly to the above Usage example but with a little more
detail. In particular it demonstrates the difference betweennotusingdjango-cache-memoizeand then adding it to your code after.Advanced Usageargs_rewriteInternally the decorator rewrites every argument and keyword argument to
the function it wraps into a concatenated string. The first thing you
might want to do is help the decorator rewrite the arguments to something
more suitable as a cache key string. For example, suppose you have instances
of a class whose__str__method doesn’t return a unique value. For example:classRecord(models.Model):name=models.CharField(max_length=100)lastname=models.CharField(max_length=100)friends=models.ManyToManyField(SomeOtherModel)def__str__(self):returnself.name# Example use:>>>record=Record.objects.create(name='Peter',lastname='Bengtsson')>>>print(record)Peter>>>record2=Record.objects.create(name='Peter',lastname='Different')>>>print(record2)PeterThis is a contrived example, but basicallyyou knowthat thestr()conversion of certain arguments isn’t safe. Then you can pass in a callable
calledargs_rewrite. It gets the same positional and keyword arguments
as the function you’re decorating. Here’s an example implementation:fromcache_memoizeimportcache_memoizedefcount_friends_args_rewrite(record):# The 'id' is always unique. Use that instead of the default __str__returnrecord.id@cache_memoize(100,args_rewrite=count_friends_args_rewrite)defcount_friends(record):# Assume this is an expensive function that can be memoize cached.returnrecord.friends.all().count()prefixBy default the prefix becomes the name of the function. Consider:fromcache_memoizeimportcache_memoize@cache_memoize(10,prefix='randomness')deffunction1():returnrandom.random()@cache_memoize(10,prefix='randomness')deffunction2():# different name, same arguments, same functionalityreturnrandom.random()# Example use>>>function1()0.39403406043780986>>>function1()0.39403406043780986>>># ^ repeated of course>>>function2()0.39403406043780986>>># ^ because the prefix was forcibly the same, the cache key is the samehit_callableIf set, a function that gets called with the original argument and keyword
argumentsifthe cache was able to find and return a cache hit.
For example, suppose you want to tell yourstatsdserver every time
there’s a cache hit.fromcache_memoizeimportcache_memoizedef_cache_hit(user,**kwargs):statsdthing.incr(f'cachehit:{user.id}',1)@cache_memoize(10,hit_callable=_cache_hit)defcalculate_tax(user,tax=0.1):return...miss_callableExact same functionality ashit_callableexcept the obvious difference
that it gets called if it wasnota cache hit.store_resultThis is useful if you have a function you want to make sure only gets called
once per timeout expiration but you don’t actually care that much about
what the function return value was. Perhaps because you know that the
function returns something that would quickly fill up yourmemcachedor
perhaps you know it returns something that can’t be pickled. Then you
can setstore_resulttoFalse. This is equivalent to your function
returningTrue.fromcache_memoizeimportcache_memoize@cache_memoize(1000,store_result=False)defsend_tax_returns(user):# something something time consuming...returnsome_none_pickleable_thingdefmyview(request):# View this view as much as you like the 'send_tax_returns' function# won't be called more than once every 1000 seconds.send_tax_returns(request.user)cache_exceptionsThis is useful if you have a function that can raise an exception as valid
result. If the cached function raises any of specified exceptions is the
exception cached and raised as normal. Subsequent cached calls will
immediately re-raise the exception and the function will not be executed.cache_exceptionsaccepts an Exception or a tuple of Exceptions.This option allows you to cache said exceptions like any other result.
Only exceptions raised from the list of classes provided as cache_exceptions
are cached, all others are propagated immediately.>>>fromcache_memoizeimportcache_memoize>>>classInvalidParameter(Exception):...pass>>>@cache_memoize(1000,cache_exceptions=(InvalidParameter,))...defrun_calculations(parameter):...# something something time consuming...raiseInvalidParameter>>>run_calculations(1)Traceback(mostrecentcalllast):...InvalidParameter# run_calculations will now raise InvalidParameter immediately# without running the expensive calculation>>>run_calculations(1)Traceback(mostrecentcalllast):...InvalidParametercache_aliasThecache_aliasargument allows you to use a cache other than the default.# Given settings like:# CACHES = {#     'default': {...},#     'other': {...},# }@cache_memoize(1000,cache_alias='other')defmyfunc(start,end):returnrandom.random()Cache invalidationWhen you want to “undo” some caching done, you simply call the function
again with the same arguments except you add.invalidateto the function.fromcache_memoizeimportcache_memoize@cache_memoize(10)defexpensive_function(start,end):returnrandom.randint(start,end)>>>expensive_function(1,100)65>>>expensive_function(1,100)65>>>expensive_function(100,200)121>>>exensive_function.invalidate(1,100)>>>expensive_function(1,100)89>>>expensive_function(100,200)121An “alias” of doing the same thing is to pass a keyword argument called_refresh=True. Like this:# Continuing from the code block above>>>expensive_function(100,200)121>>>expensive_function(100,200,_refresh=True)177>>>expensive_function(100,200)177There is no way to clear more than one cache key. In the above example,
you had to know the “original arguments” when you wanted to invalidate
the cache. There is no method “search” for all cache keys that match a
certain pattern.CompatibilityPython 3.8, 3.9, 3.10 & 3.11Django 3.2, 4.1 & 4.2Check out thetox.inifile for more up-to-date compatibility by
test coverage.Prior ArtHistoryMozilla Symbol Serveris written in Django. It’s a web service that
sits between C++ debuggers and AWS S3. It shuffles symbol files in and out of
AWS S3. Symbol files are for C++ (and other compiled languages) what
sourcemaps are for JavaScript.This service gets a LOT of traffic. The download traffic (proxying requests
for symbols in S3) gets about ~40 requests per second. Due to the nature
of the application most of these GETs result in a 404 Not Found but instead
of asking AWS S3 for every single file, these lookups are cached in a
highly configuredRedisconfiguration. This Redis cache is also connected
to the part of the code that uploads new files.New uploads are arriving as zip file bundles of files, from Mozilla’s build
systems, at a rate of about 600MB every minute, each containing on average
about 100 files each. When a new upload comes in we need to quickly be able
find out if it exists in S3 and this gets cached since often the same files
are repeated in different uploads. But when a file does get uploaded into S3
we need to quickly and confidently invalidate any local caches. That way you
get to keep a really aggressive cache without any stale periods.This is the use casedjango-cache-memoizewas built for and tested in.
It was originally written for Python 3.6 in Django 1.11 but when
extracted, made compatible with Python 2.7 and as far back as Django 1.8.django-cache-memoizeis also used inSongSear.chto cache short
queries in the autocomplete search input. All autocomplete is done by
Elasticsearch, which is amazingly fast, but not as fast asmemcached.“Competition”There is alreadydjango-memoizebyThomas Vavrys.
It too is available as a memoization decorator you use in Django. And it
uses the default cache framework as a storage. It usedinspecton the
decorated function to build a cache key.In benchmarks running bothdjango-memoizeanddjango-cache-memoizeI founddjango-cache-memoizeto be~4 times fasteron average.Another key difference is thatdjango-cache-memoizeusesstr()anddjango-memoizeusesrepr()which in certain cases of mutable objects
(e.g. class instances) as arguments the caching will not work. For example,
this doesnotwork indjango-memoize:frommemoizeimportmemoize@memoize(60)defcount_user_groups(user):returnuser.groups.all().count()defmyview(request):# this will never be memoizedprint(count_user_groups(request.user))However, this works…fromcache_memoizeimportcache_memoize@cache_memoize(60)defcount_user_groups(user):returnuser.groups.all().count()defmyview(request):# this *will* work as expectedprint(count_user_groups(request.user))DevelopmentThe most basic thing is to clone the repo and run:pipinstall-e".[dev]"toxCode style is all blackAll code has to be formatted withBlackand the best tool for checking this istherapistsince it can help you run
all, help you fix things, and help you make sure linting is passing before
you git commit. This project also usesflake8to check other things
Black can’t check.To check linting withtoxuse:tox-elint-py36To install thetherapistpre-commit hook simply run:therapistinstallWhen you runtherapist runit will only check the files you’ve touched.
To run it for all files use:therapistrun--use-tracked-filesAnd to fix all/any issues run:therapistrun--use-tracked-files--fix