# Path: contentious/contrib/basicedit/models.py
# class ContentItem(ContentItemBase):
#     """ Model for storing content items. """
#     pass
# 
# Path: contentious/contrib/basicedit/utils.py
# def content_dict_cache_key():
#     prefix = getattr(settings, "CONTENT_CACHE_PREFIX", "")
#     return "%scontent_dict_cache" % prefix
# 
# def get_cache_timeout():
#     return getattr(settings, "CONTENT_CACHE_TIMEOUT", None)


from django.core.cache import cache
from contentious.contrib.basicedit.models import ContentItem
from contentious.contrib.basicedit.utils import (
    content_dict_cache_key,
    get_cache_timeout,
)

    def _get_content_dict(self, template_context):
        """ An efficient way for us to fetch content data without hitting the DB
            multiple times on the same request.  Tries to get the content by:
            1. getting it from a temporary cache on the request object, 2. getting
            it from memcache, 3. getting it from the database.
            Returns a dict of dicts.
        """
        request = template_context['request']
        #The first time we fetch the content on a given request we store it on the request object
        try:
            return request._content_cache_dict
        except AttributeError:
            pass
        cache_key = content_dict_cache_key()
        content_dict = cache.get(cache_key)
        if content_dict is None:
            content_objects = ContentItem.objects.all()
            content_dict = {obj.key: obj.content_dict for obj in content_objects}
            cache.set(cache_key, content_dict, get_cache_timeout())
        request._content_cache_dict = content_dict
        return content_dict

    def _clear_caches(self, template_context):
        """ Clear our caches from both the request object and memcache. """
        request = template_context['request']
        try:
            del request._content_cache_dict
        except AttributeError:
            pass

# Path: contentious/contrib/basicedit/models.py
# class ContentItem(ContentItemBase):
#     """ Model for storing content items. """
#     pass
# 
# Path: contentious/contrib/basicedit/utils.py
# def content_dict_cache_key():
#     prefix = getattr(settings, "CONTENT_CACHE_PREFIX", "")
#     return "%scontent_dict_cache" % prefix
# 
# def get_cache_timeout():
#     return getattr(settings, "CONTENT_CACHE_TIMEOUT", None)


# Path: contentious/contrib/basicedit/api.py
from django.core.cache import cache
from contentious.contrib.basicedit.models import ContentItem
from contentious.contrib.basicedit.utils import (
    content_dict_cache_key,
    get_cache_timeout,
)


    def _get_content_dict(self, template_context):
        """ An efficient way for us to fetch content data without hitting the DB
            multiple times on the same request.  Tries to get the content by:
            1. getting it from a temporary cache on the request object, 2. getting
            it from memcache, 3. getting it from the database.
            Returns a dict of dicts.
        """
        request = template_context['request']
        #The first time we fetch the content on a given request we store it on the request object
        try:
            return request._content_cache_dict
        except AttributeError:
            pass
        cache_key = content_dict_cache_key()
        content_dict = cache.get(cache_key)
        if content_dict is None:
            content_objects = ContentItem.objects.all()
            content_dict = {obj.key: obj.content_dict for obj in content_objects}
            cache.set(cache_key, content_dict, get_cache_timeout())
        request._content_cache_dict = content_dict
        return content_dict

    def _clear_caches(self, template_context):
        """ Clear our caches from both the request object and memcache. """
        request = template_context['request']
        try:
            del request._content_cache_dict
        except AttributeError:
            pass

        cache.delete(content_dict_cache_key())