https://github.com/amandasaurus/django-cache-by-user
Make your django site share a cache for all anonymous users, increasing cache hits
https://github.com/amandasaurus/django-cache-by-user
Last synced: 4 months ago
JSON representation
Make your django site share a cache for all anonymous users, increasing cache hits
- Host: GitHub
- URL: https://github.com/amandasaurus/django-cache-by-user
- Owner: amandasaurus
- License: gpl-3.0
- Created: 2014-06-18T14:58:29.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2014-08-08T09:05:59.000Z (almost 11 years ago)
- Last Synced: 2024-12-27T00:24:39.348Z (6 months ago)
- Language: Python
- Size: 217 KB
- Stars: 1
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
django-cache-by-user
====================Make your django site share a cache for all anonymous users, increasing cache
hits, increasing page load time, reducing CPU load.When using [Django's per view cache](), the session middleware will add ``Vary:
Cookie`` so that cached pages will not be shared between users. Nearly all the
time this is what you want. However if you have a popular webpage that is often
visited by non-logged in users, and you want to cache it, you have a problem.
Every different anonymous user will cause a cache hit.By using ``django-cache-by-user``, you can share the cache for a page between anonymous users.
Installation
============pip install django-cache-by-user
Add it as a middleware:
MIDDLEWARE_CLASSES = (
….
'cache_by_user.middleware.AddUserIDHashMiddleware',
…
)Usage
=====Every response will now have a ``X-UserIDHash``
header. All anonymous users will have the same value, all logged in users will
have a different value based on their userid.The value is hashed and based on the userid and your ``SECRET_KEY`` setting.
Neither your ``SECRET_KEY`` nor user ids are revealed though.It adds ``X-UserIDHash`` to the ``Vary`` header of the response. To get the real benefit you need to use [django-dont-vary-on](https://github.com/rory/django-dont-vary-on) to remove ``Cookie`` from the Vary header.
@dont_vary_on("Cookie")
def my_view_with_anonymous(request):
...