Ticket #8907: query.py.diff

File query.py.diff, 1.3 KB (added by bradcater, 16 years ago)

Diff adding ORM-level caching support.

Line 
15a6
2> from django.core.cache import cache
310a12
4> import md5
5123a126
6> self._cacheme = True
7482a486,509
8> def nocache(self):
9> self._cacheme = False
10> return self
11>
12> def cache(self,time,force=False):
13> """
14> Caches QuerySet objects if their cachekey is not already in the cache.
15> Returns the original QuerySet. Will cache the new result if force=True.
16> """
17> self._cacheme = True
18> # must cache for a positive number of seconds
19> if time > 0:
20> cachekey = self._cachekey()
21> # only cache if it is not currently cached
22> if force or not cache.get(cachekey):
23> cache.set(cachekey,self,time)
24> return self
25> else:
26> raise Exception('Django ORM caching error: You must specify a positive time interval.')
27>
28> def _cachekey(self):
29> m = md5.new(str(self.query))
30> return m.hexdigest()
31>
32599a627,634
33> # make sure to set new object's cacheme value to old value
34> c._cacheme = self._cacheme
35> # only test against the cache if cacheme variable is True
36> if c._cacheme:
37> cachekey = c._cachekey()
38> cached = cache.get(cachekey)
39> if cached:
40> c = cached
Back to Top