File: cache.py

package info (click to toggle)
python-django-celery-results 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 696 kB
  • sloc: python: 2,373; makefile: 312; sh: 7; sql: 2
file content (39 lines) | stat: -rw-r--r-- 1,129 bytes parent folder | download | duplicates (3)
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
"""Celery cache backend using the Django Cache Framework."""

from celery.backends.base import KeyValueStoreBackend
from django.core.cache import cache as default_cache
from django.core.cache import caches
from kombu.utils.encoding import bytes_to_str


class CacheBackend(KeyValueStoreBackend):
    """Backend using the Django cache framework to store task metadata."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Must make sure backend doesn't convert exceptions to dict.
        self.serializer = 'pickle'

    def get(self, key):
        key = bytes_to_str(key)
        return self.cache_backend.get(key)

    def set(self, key, value):
        key = bytes_to_str(key)
        self.cache_backend.set(key, value, self.expires)

    def delete(self, key):
        key = bytes_to_str(key)
        self.cache_backend.delete(key)

    def encode(self, data):
        return data

    def decode(self, data):
        return data

    @property
    def cache_backend(self):
        backend = self.app.conf.cache_backend
        return caches[backend] if backend else default_cache