# Path: dju_common/tcache.py
# def cache_set(key, value, timeout=DEFAULT_TIMEOUT, version=None, tags=None, cache=None, **kwargs):
#     assert isinstance(tags, (NoneType, list, basestring, set))
#     if cache is None:
#         cache = default_cache
#     r = cache.set(key, value, timeout=timeout, version=version, **kwargs)
#     if not tags:
#         return r
#     if isinstance(tags, basestring):
#         tags = [tags]
#     tag_keys = [CACHE_TAG_KEY % tag for tag in tags]
#     if cache.__class__.__name__ == 'RedisCache':
#         from django_redis.exceptions import ConnectionInterrupted
#         try:
#             redis_client = cache.client.get_client()
#             for tag_key in tag_keys:
#                 redis_client.sadd(tag_key, key)
#         except ConnectionInterrupted:
#             pass  # todo add logging
#     else:
#         for tag_key in tag_keys:
#             keys = cache.get(tag_key) or set()
#             keys.add(key)
#             cache.set(tag_key, keys, timeout=None)
#     return r


from django import template
from django.core.cache.utils import make_template_fragment_key
from django.core.cache import cache
from dju_common.tcache import cache_set


class CacheNode(template.Node):
    def __init__(self, nodelist, expire_time_var, fragment_name, vary_on, tags):
        self.nodelist = nodelist
        self.expire_time_var = expire_time_var
        self.fragment_name = fragment_name
        self.vary_on = vary_on
        self.tags = tags

    def get_tags(self, context):
        if self.tags:
            tags = self.tags.resolve(context)
            if isinstance(tags, basestring):
                tags = [tag.strip() for tag in tags.split(',')]
            elif not isinstance(tags, (list, tuple, set)):
                raise template.TemplateSyntaxError('"tcache" tag variable "tags" invalid')
            return filter(None, tags) or None

    def render(self, context):
        try:
            expire_time = self.expire_time_var.resolve(context)
            expire_time = int(expire_time)
        except (ValueError, TypeError):
            raise template.TemplateSyntaxError('"tcache" tag got a non-integer timeout value: %r' % expire_time)
        vary_on = [var.resolve(context) for var in self.vary_on]
        cache_key = make_template_fragment_key(self.fragment_name, vary_on)
        value = cache.get(cache_key)
        if value is None:
            value = self.nodelist.render(context)

# Path: dju_common/tcache.py
# def cache_set(key, value, timeout=DEFAULT_TIMEOUT, version=None, tags=None, cache=None, **kwargs):
#     assert isinstance(tags, (NoneType, list, basestring, set))
#     if cache is None:
#         cache = default_cache
#     r = cache.set(key, value, timeout=timeout, version=version, **kwargs)
#     if not tags:
#         return r
#     if isinstance(tags, basestring):
#         tags = [tags]
#     tag_keys = [CACHE_TAG_KEY % tag for tag in tags]
#     if cache.__class__.__name__ == 'RedisCache':
#         from django_redis.exceptions import ConnectionInterrupted
#         try:
#             redis_client = cache.client.get_client()
#             for tag_key in tag_keys:
#                 redis_client.sadd(tag_key, key)
#         except ConnectionInterrupted:
#             pass  # todo add logging
#     else:
#         for tag_key in tag_keys:
#             keys = cache.get(tag_key) or set()
#             keys.add(key)
#             cache.set(tag_key, keys, timeout=None)
#     return r


# Path: dju_common/templatetags/dju_tcache.py
from django import template
from django.core.cache.utils import make_template_fragment_key
from django.core.cache import cache
from dju_common.tcache import cache_set



class CacheNode(template.Node):
    def __init__(self, nodelist, expire_time_var, fragment_name, vary_on, tags):
        self.nodelist = nodelist
        self.expire_time_var = expire_time_var
        self.fragment_name = fragment_name
        self.vary_on = vary_on
        self.tags = tags

    def get_tags(self, context):
        if self.tags:
            tags = self.tags.resolve(context)
            if isinstance(tags, basestring):
                tags = [tag.strip() for tag in tags.split(',')]
            elif not isinstance(tags, (list, tuple, set)):
                raise template.TemplateSyntaxError('"tcache" tag variable "tags" invalid')
            return filter(None, tags) or None

    def render(self, context):
        try:
            expire_time = self.expire_time_var.resolve(context)
            expire_time = int(expire_time)
        except (ValueError, TypeError):
            raise template.TemplateSyntaxError('"tcache" tag got a non-integer timeout value: %r' % expire_time)
        vary_on = [var.resolve(context) for var in self.vary_on]
        cache_key = make_template_fragment_key(self.fragment_name, vary_on)
        value = cache.get(cache_key)
        if value is None:
            value = self.nodelist.render(context)

            cache_set(cache_key, value, timeout=expire_time, tags=self.get_tags(context))