object attribute is missing after manager.py is restarted when using django redis cache

46 views Asked by At

Here is the sample code:

class CrossSection(LoggerMixin):
    pass

class CrossSectionFactory(LoggerMixin):
    cross_section_sequence = {}

    def add(self, date):
        cross_section = CrossSection()
        self.cross_section_sequence[date] = cross_section

class Analyzer(LoggerMixin):
    cross_section_factory = None

    def __init__(self, logger=None, **kwargs):
        self.cross_section_factory = CrossSectionFactory(logger=logger, **kwargs)

    def init(self):
        pass

# ----
from django.core.cache import cache

class MyDetail(APIView):
    def post(self, request, analyzer_id, date, format=None):
        analyzer = cache.get("analyzer:1")
        if analyzer is None:
            analyzer = Analyzer(**{})
            analyzer.init()
        cache.set("analyzer:1", analyzer)

settings.py:

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://xxx:[email protected]:6379",
        "TIMEOUT": None,
    }
}

If the manger.py is not restarted, it can get analyzer with all attributes, but if manger.py restarts, the other attributes are all good except analyzer.cross_section_factory.cross_section_sequence, it become {} again.

I tried set/get the analyzer.cross_section_factory directly, it seems that works fine.

Do you know why this happens? How do I fix it?

====

Update: I changed the cache key, then I can set/get the analyzer now. It seems that the set operation didn't work properly, do you know why?

0

There are 0 answers