-
Notifications
You must be signed in to change notification settings - Fork 0
/
django_zonecache.py
64 lines (47 loc) · 1.97 KB
/
django_zonecache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""A thin layer for the django cache interface to specify cache zones.
Django cache does not offer the functionality to purge a zone ('group') of
cached objects. This module prefixes all keys for the items within one zone
with a zone key. If the zone should be invalidated then the zone key
is incremented.
All actions take two cache lookups, one for the zone key and one for the
actual value.
"""
from django.core.cache import cache
def add(key, value, timeout=None, version=None, zone=None):
key, version = make_zone_key(key, version, zone)
return cache.add(key, value, timeout, version=version)
def get(key, default=None, version=None, zone=None):
key, version = make_zone_key(key, version, zone)
return cache.get(key, default, version)
def set(key, value, timeout=None, version=None, zone=None, refreshed=False):
key, version = make_zone_key(key, version, zone)
return cache.set(key, value, timeout, version)
def delete(key, version=None, zone=None):
key, version = make_zone_key(key, version, zone)
return cache.delete(key, version)
def invalidate_zone(zone):
""" Invalidates all cache keys belonging to zone """
key, version = _zone_metadata(zone)
try:
cache.incr(key)
except ValueError:
cache.add(key, version)
cache.incr(key)
def make_zone_key(key, version=None, zone=None):
"""Create the unique zone key"""
if zone:
zone_key, zone_version = _zone_metadata(zone, version)
if version:
key = '%d:%s' % (version, key)
key = '%s:%s' % (zone, key)
return key, zone_version
return key, version
def _zone_metadata(zone, version=None):
""" This can be useful sometimes if you're doing a very large number
of operations and you want to avoid all of the extra cache hits.
"""
zone_key = '_:zone_version::%s' % zone
if version:
zone_key += ':%d' % version
zone_version = cache.get(zone_key, default=1)
return zone_key, zone_version