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

Make cache value serializable #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions solo/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings
from django.db import models
from django.forms import model_to_dict

try:
from django.core.cache import caches
Expand Down Expand Up @@ -41,7 +42,8 @@ def set_to_cache(self):
cache = get_cache(cache_name)
cache_key = self.get_cache_key()
timeout = getattr(settings, 'SOLO_CACHE_TIMEOUT', solo_settings.SOLO_CACHE_TIMEOUT)
cache.set(cache_key, self, timeout)
cache_value = model_to_dict(self)
cache.set(cache_key, cache_value, timeout)

@classmethod
def get_cache_key(cls):
Expand All @@ -56,8 +58,9 @@ def get_solo(cls):
return obj
cache = get_cache(cache_name)
cache_key = cls.get_cache_key()
obj = cache.get(cache_key)
if not obj:
cache_value = cache.get(cache_key)
if not cache_value:
obj, created = cls.objects.get_or_create(pk=cls.singleton_instance_id)
obj.set_to_cache()
return obj
return obj
return cls(**cache_value)