Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use string values for django-cache keys #230 #242

Merged
merged 7 commits into from Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions django_celery_results/backends/cache.py
Expand Up @@ -3,6 +3,7 @@
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):
Expand All @@ -15,12 +16,15 @@ def __init__(self, *args, **kwargs):
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):
Expand Down
13 changes: 13 additions & 0 deletions t/unit/backends/test_cache.py
Expand Up @@ -4,6 +4,7 @@
import pytest
from billiard.einfo import ExceptionInfo
from celery import result, states, uuid
from kombu.utils.encoding import bytes_to_str

from django_celery_results.backends.cache import CacheBackend

Expand Down Expand Up @@ -59,6 +60,18 @@ def test_is_pickled(self):
assert rindb.get('foo') == 'baz'
assert rindb.get('bar').data == 12345

def test_convert_key_from_byte_to_str(self):
""" Tests that key in byte form passed into cache
are succesfully completed """
tid = bytes_to_str(uuid())

assert self.b.get_status(tid) == states.PENDING
assert self.b.get_result(tid) is None

self.b.mark_as_done(tid, 42)
assert self.b.get_status(tid) == states.SUCCESS
assert self.b.get_result(tid) == 42

def test_mark_as_failure(self):
einfo = None
tid3 = uuid()
Expand Down