Skip to content
DRF Cache provides easy to use cache framework for django-rest-framwork apps
Branch: master
Clone or download
Latest commit 9e4a67b Apr 13, 2016
Type Name Latest commit message Commit time
Failed to load latest commit information.
rest_framework_cache Releasing the v0.1 Apr 8, 2016
tests Adding test for get_all_cache_keys Apr 8, 2016
.gitignore Initial commit Dec 16, 2015
.travis.yml Running tests with multiple django version Mar 22, 2016
LICENSE Initial commit Dec 16, 2015 Updating Travis widget Apr 12, 2016 Adding mock to tests requirements Mar 21, 2016


Current version at PyPI Build Status

DRF Cache provides easy to use, powerful and flexible cache framework for django-rest-framwork apps.


Install using pip...

pip install rest-framework-cache

Add 'rest_framework_cache' to your INSTALLED_APPS setting.


You must execute autodiscover to load your serializers. To do this change your adding the following code (sunch as Django admin):

from rest_framework_cache.registry import cache_registry



This lib does not install any dependency, but your project obviously have to be using Django Rest Framework.


To use the DRF cache you must register your serializer into cache registry ( such as Django admin ). You also must change your serializer to inherit the CachedSerializerMixin:

from rest_framework import serializers

# You must import the CachedSerializerMixin and cache_registry
from rest_framework_cache.serializers import CachedSerializerMixin
from rest_framework_cache.registry import cache_registry

from .models import Comment

class CommentSerializer(serializers.ModelSerializer, CachedSerializerMixin):

    class Meta:
        model = Comment



To the cache successfully work you must configure the Django CACHES setting. We recomend that you take a look on Django cache docs here

Using cache backend different of the default

If you need use a cache backend different of the default you can specify it on the RF_CACHE_BACKEND.

To do this edit your like this:

# ...
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '',
    'rest_backend': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake',

    'DEFAULT_CACHE_BACKEND': 'rest_backend',
# ...

Cache timeout

You can set the cache timeout using DEFAULT_CACHE_TIMEOUT.

    'DEFAULT_CACHE_TIMEOUT': 86400, # Default is 1 day

How it works

Accessing the cache

When the representation of CachedSerializerMixin is called the fist thing that will be executed is a verification that checks if the request objects is already in cache, if yes the cached object will be returned without touch the database, otherwise the object will be requested to the database stored on cache and returned.

Cleaning the cache

When your serializer is declareted using the CachedSerializerMixin the DRF cache register a signal to the serializer model. When a instance of the model has changed or deleted the signal clear related object on the cache backend.

You can’t perform that action at this time.