File: djangocache.py

package info (click to toggle)
python-memoize 1.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112 kB
  • sloc: python: 324; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,136 bytes parent folder | download | duplicates (4)
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
import time

from django.conf import settings
from django.core.cache import caches


class Cache(object):
    """
    Will simply proxy cache calls to django's internal
    cache framework
    """
    def __init__(self, name='default'):
        self._cache = caches[name]

    def get(self, key):
        return self._cache.get(key)

    def delete(self, key):
        return self._cache.delete(key)

    def expire_at(self, key, max_age):
        raise NotImplementedError('Unsupported by django cache backend')

    def exists(self, key):
        return self.get(key) is not None

    def __getitem__(self):
        return self.get(key)

    def __setitem__(self, key, value):
        expiry = value[2]
        seconds = None
        if expiry:
            # this is a bit ugly, but django's cache framework
            # requires an expiry in seconds
            # to we reverse-compute that value from the expiry given in the
            # data tuple
            now = time.time()
            seconds = int(expiry - now)
        return self._cache.set(key, value, seconds)

    def __delitem__(key):
        return self.delete(key)