Esempio n. 1
0
def news(request):
    # cache is like a database
    cache = caches('redis_backend')
    result = cache.get('news')

    if result:
        return HttpResponse(result)

    news_list = []
    for i in range(10):
        news_list.append("recently trade war %d" % i)

    sleep(5)
    data = {'news_list': news_list}
    response = render(request, 'news.html', context=locals())

    cache.set('news', response.content, timeout=30)
    return response
Esempio n. 2
0
 def set_cached_choices(self, choices):
     if not self.cache_config['enabled']:
         return
     c = caches(self.cache_config['cache'])
     return c.set(self.cache_config['key'] % self.field_path, choices)
Esempio n. 3
0
 def get_cached_choices(self):
     if not self.cache_config['enabled']:
         return None
     c = caches(self.cache_config['cache'])
     return c.get(self.cache_config['key'] % self.field_path)
from django import template
from django.conf import settings

try:
    from django.core.cache import caches
except ImportError:
    from django.core.cache import get_cache as caches

if caches.__module__.startswith('debug_toolbar'):
    try:
        from debug_toolbar.panels.cache import base_get_cache as caches
    except ImportError:
        from debug_toolbar.panels.cache import get_cache as caches

get_cache = lambda cache_name: caches(cache_name) if hasattr(
    caches, '__call__') else caches[cache_name]
logger = logging.getLogger(__name__)
register = template.Library()


@register.simple_tag(takes_context=True)
def get_cache_stats(context):
    """
    Reads the cache stats out of the memcached cache backend. Returns `None`
    if no cache stats supported.
    """
    cache_stats = []
    for cache_backend_nm, cache_backend_attrs in six.iteritems(
            settings.CACHES):
        try:
def get_cache(cache_name):
    if hasattr(caches, '__call__'):
        return caches(cache_name)
    return caches[cache_name]
Esempio n. 6
0
 def __init__(self):
     self.cache = caches(SSO_SETTING_CACHE)
Esempio n. 7
0
 def set_cached_choices(self,choices):
     if not self.cache_config['enabled']:
         return
     c = caches(self.cache_config['cache'])
     return c.set(self.cache_config['key']%self.field_path,choices)
Esempio n. 8
0
 def get_cached_choices(self):
     if not self.cache_config['enabled']:
         return None
     c = caches(self.cache_config['cache'])
     return c.get(self.cache_config['key']%self.field_path)
Esempio n. 9
0
def get_cache(cache_name):
    if hasattr(caches, '__call__'):
        return caches(cache_name)
    return caches[cache_name]
Esempio n. 10
0
 def set_cached_choices(self, choices):
     if not self.cache_config["enabled"]:
         return
     c = caches(self.cache_config["cache"])
     return c.set(self.cache_config["key"] % self.field_path, choices)
Esempio n. 11
0
 def get_cached_choices(self):
     if not self.cache_config["enabled"]:
         return None
     c = caches(self.cache_config["cache"])
     return c.get(self.cache_config["key"] % self.field_path)
from django import template
from django.conf import settings
try:
    from django.core.cache import caches
except:
    from django.core.cache import get_cache as caches

if caches.__module__.startswith('debug_toolbar'):
    from debug_toolbar.panels.cache import original_get_cache as caches

get_cache = lambda cache_name: caches(cache_name) if hasattr(caches, '__call__') else caches[cache_name]
register = template.Library()

class CacheStats(template.Node):
    """
    Reads the cache stats out of the memcached cache backend. Returns `None`
    if no cache stats supported.
    """
    def render(self, context):
        cache_stats = []
        for cache_backend_nm, cache_backend_attrs in settings.CACHES.iteritems():
            try:
                cache_backend = get_cache(cache_backend_nm)
                this_backend_stats = cache_backend._cache.get_stats()
                # returns list of (name, stats) tuples
                for server_name, server_stats in this_backend_stats:
                    cache_stats.append(("%s: %s" % (
                        cache_backend_nm, server_name), server_stats))
            except AttributeError: # this backend probably doesn't support that
                continue
        context['cache_stats'] = cache_stats
Esempio n. 13
0
 def set_cached_choices(self, choices):
     if not self.cache_config["enabled"]:
         return
     c = caches(self.cache_config["cache"])
     return c.set(self.cache_config["key"] % self.field_path, choices)
Esempio n. 14
0
 def get_cached_choices(self):
     if not self.cache_config["enabled"]:
         return None
     c = caches(self.cache_config["cache"])
     return c.get(self.cache_config["key"] % self.field_path)