Giter Site home page Giter Site logo

django-custom-cache-page's Introduction

A flexible implementation of Django's cache_page decorator, supports building custom keys, cache groups, O(1) cache invalidation on redis, and more.

Description

This package provides a customizable cache_page decorator that allows you to define your custom key generation functions with a few utilities and invalidate large cache groups on redis at no cost.

The story behind this package:

we used django's default cache_page decorator for around a year, it was really easy and quick to set up and it served its purpose, but it was very limiting, it had no way of building custom keys for cached views and that was making it quite impossible to invalidate view caches properly, data inconsistencies appeared everywhere, and we had to build a customizable solution for caching.

Installation

pip install django-custom-cache-page

Features:

The customizable cache_page supports the following:

  • accepts a function to generate a key based on the request.
  • has an option to have versioned groups in the cache key
    • this is really useful with redis, deleting a large number of caches (without storing all the keys!) on redis has a performance of O(n) and it blocks any other operation because redis is single-threaded. meaning that it will take a longer time as the size of your cache DBs grow, it's impossible to do this operation frequently at scale without choking redis. our solution to achieve O(1) invalidation is to have versioned groups in the cache key, where the group version is stored in redis separately, all you need to invalidate a group of caches is to increment the group version and all the old caches will become invalid and will expire gradually. (be careful, this won't help with keys that don't expire.)
  • supports disabling cache for some requests by setting do_not_cache attribute on the request to True.

There are two default utilities to generate cache keys:

  • generate_cache_key: similar to Django's default
  • generate_query_params_cache_key: generate a key using query parameters only

but you can create your own key generation function and pass it to the decorator.

Example

views.py:

from django.http import HttpResponse

from custom_cache_page.cache import cache_page
from custom_cache_page.utils import generate_query_params_cache_key

@cache_page(60 * 60, generate_query_params_cache_key)
def my_view(request):
    return HttpResponse("okay")

using groups:

from django.http import HttpResponse
from custom_cache_page.cache import cache_page

@cache_page(
        timeout=60 * 60,
        key_func=lambda r: r.path,
        versioned=False,
        group_func=lambda r: 'cached_views',
        prefix='prefix'
)
def my_view(request):
    return HttpResponse("okay")

to invalidate a group:

from custom_cache_page.utils import invalidate_group_caches

invalidate_group_caches('cached_views')

Hashed keys:

All keys generated by this package are hashed using md5 for performance reasons. if you wanted to delete the keys manually use the hashing utility first:

from custom_cache_page.utils import hash_key
key = 'prefix:cached_views:0:/bo'
hashed_key =  hash_key(key)

Running Tests

pytest

Development installation

git clone https://github.com/zidsa/django-custom-cache-page.git
cd django-custom-cache-page
pip install --editable .

django-custom-cache-page's People

Contributors

hussam-almarzoq avatar imhmdb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

django-custom-cache-page's Issues

install with pip throws exception

Collecting django-custom-cache-page
Downloading https://wheels.aldryn.net/v1/pypi/aldryn-baseproject-v4-py36/%2Bsimple/django-custom-cache-page/0.3/download/342080/django-custom-cache-page-0.3.tar.gz (4.4 kB)
ERROR: Command errored out with exit status 1:
command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lliuwd14/django-custom-cache-page/setup.py'"'"'; file='"'"'/tmp/pip-install-lliuwd14/django-custom-cache-page/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-mp2rxp_a
cwd: /tmp/pip-install-lliuwd14/django-custom-cache-page/
Complete output (5 lines):
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-lliuwd14/django-custom-cache-page/setup.py", line 6, in
with open('HISTORY.md') as history_file:
FileNotFoundError: [Errno 2] No such file or directory: 'HISTORY.md'
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.