from django.core.cache import caches

from kolibri.utils.conf import OPTIONS


cache_options = OPTIONS["Cache"]


class CrossProcessCache(object):
    def __init__(self, default_timeout=cache_options["CACHE_TIMEOUT"]):
        self.default_timeout = default_timeout

    def __contains__(self, key):
        if key in caches["default"]:
            return True
        if cache_options["CACHE_BACKEND"] != "redis" and key in caches["process_cache"]:
            return True
        return False

    def get(self, key, default=None, version=None):
        if key in caches["default"] or cache_options["CACHE_BACKEND"] == "redis":
            return caches["default"].get(key, default=default, version=version)
        item = caches["process_cache"].get(key, default=None, version=None)
        caches["default"].set(key, item, timeout=self.default_timeout, version=version)
        return item

    def set(self, key, value, timeout=None, version=None):
        caches["default"].set(
            key, value, timeout=timeout or self.default_timeout, version=version
        )
        if cache_options["CACHE_BACKEND"] != "redis":
            caches["process_cache"].set(
                key, value, timeout=timeout or self.default_timeout, version=version
            )

from django.core.cache import caches

from kolibri.utils.conf import OPTIONS


cache_options = OPTIONS["Cache"]


class CrossProcessCache(object):
    def __init__(self, default_timeout=cache_options["CACHE_TIMEOUT"]):
        self.default_timeout = default_timeout

    def __contains__(self, key):
        if key in caches["default"]:
            return True
        if cache_options["CACHE_BACKEND"] != "redis" and key in caches["process_cache"]:
            return True
        return False

    def get(self, key, default=None, version=None):
        if key in caches["default"] or cache_options["CACHE_BACKEND"] == "redis":
            return caches["default"].get(key, default=default, version=version)
        if key in caches["process_cache"]:
            item = caches["process_cache"].get(key, default=None, version=None)
            caches["default"].set(
                key, item, timeout=self.default_timeout, version=version
            )
            return item
        return default

    def set(self, key, value, timeout=None, version=None):
        caches["default"].set(
            key, value, timeout=timeout or self.default_timeout, version=version
        )
        if cache_options["CACHE_BACKEND"] != "redis":
            caches["process_cache"].set(
                key, value, timeout=timeout or self.default_timeout, version=version
            )

Add extra safeguards to process cache.