Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A smarter local memory cache backend for Django

Project description

Latest PyPI version Latest Travis CI build status

A smarter local memory cache backend for Django.

Cache Performance

Set Performance. Delete performance.


lrucache_backend is an in-memory cache that improves upon the existing LocMemCache that Django provides.

Comes with cache timeouts and a smart eviction strategy that prefers to keep keys that are used often and evict keys that are not.

Originally developed to avoid poorly reimplementing local object stores for service layer objects. For example:

def get_data_before(self):
    if not hasattr(self, '__data'):
        self.__data = self.expensive_query()
    return self.__data

def get_data_after(self):
    lcache = caches['local']
    data = lcache.get('our_data')
    if not data:
        data = self.expensive_query()
        lcache.set('our_data', data, timeout=600)
    return data

The benefits (despite the longer method) include timeouts, sharing data between requests, and avoiding network requests. This is especially useful when there are hundreds or thousands of property accesses that would hit the cache where network overhead would be prohibitive. The Fat model pattern can greatly benefit from tiered caching.

Good for?

An in memory cache is good for small data that changes rarely. It’s effectively a global dictionary shared between requests in the same process. Small lookup tables and database backed settings are good candidates.

A small number of keys should be used to avoid engaging the culling strategy of the cache. Performance goes down fast as soon as the maximum number of keys are reached, and keys start to evict.

This should not be used as your primary cache, but it makes for an excellent secondary cache when you want to avoid the overhead of a network call.

Use for:

  • Small lookup tables
  • Settings
  • Backing store for your service objects
  • Remembering values for the duration of a request or celery task
  • Small global template fragments like sidebars or footers
  • Secondary cache

Bad for?

An in memory cache is terrible for data that changes often. Because the cache is process local, it’s extremely difficult to coordinate cache invalidation from external processes. For that reason, this library does nothing to support cache invalidation.

The cache shares memory with the application, so it’s extremely important to avoid storing a lot of keys, or any large values.

Do not use for:

  • Instance attributes/properties
  • Full templates
  • Tables with a large number of rows
  • Large values
  • Large lists
  • Primary cache

Differences from LocMemCache

  • Avoids pickling
  • Avoids key name validation
  • Uses an LRU eviction algorithm rather than a random percentage culling strategy


pip install django-lrucache-backend


lru-dict is implemented in C and is unlikely to work with non-CPython implementations. There are compatible pure python libraries. If you need this ability, please open an Issue!


Configure your CACHES Django setting appropriately:

    'local': {
        'BACKEND': 'lrucache_backend.LRUObjectCache',
        'TIMEOUT': 600,
        'OPTIONS': {
            'MAX_ENTRIES': 100
        'NAME': 'optional-name'

And then use the cache as you would any other:

>>> from django.core.cache import caches

>>> local = caches['local']
>>> local.set('key', 123)
>>> local.get('key')
... 123

If you’re going to use this cache backend, then it’s highly recommended to use it as a non-default cache. That is, do not configure this cache under the default name.

Local memory caches compete for memory with your application so it’s in your best interests to use it as sparingly and deliberately as possible.


Django 1.11 - Django 2.2. Python support is 2.7 for Django 1.11, and 3.6+ for Django >= 2.0. Python 3.4 - 3.5 probably works with Django < 2.1, but is no longer tested.




django-lrucache-backend was written by Josh Smeaton.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-lrucache-backend, version 2.0.0
Filename, size File type Python version Upload date Hashes
Filename, size django_lrucache_backend-2.0.0-py2.py3-none-any.whl (8.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size django-lrucache-backend-2.0.0.tar.gz (5.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page