def calc_last_modified(request, *args, **kwargs): """ Returns the file's modified time as the last-modified date """ assert "cache_name" in kwargs, "Must specify cache_name as a keyword arg." try: cache = get_cache(kwargs["cache_name"]) assert isinstance(cache, FileBasedCache) or isinstance(cache, LocMemCache), "requires file-based or mem-based cache." except InvalidCacheBackendError: return None key = django_get_cache_key(request, cache=cache) if key is None or not cache.has_key(key): return None if isinstance(cache, FileBasedCache): fname = cache._key_to_file(cache.make_key(key)) if not os.path.exists(fname): # would happen only if cache expired AFTER getting the key return None last_modified = datetime.datetime.fromtimestamp(os.path.getmtime(fname)) elif isinstance(cache, LocMemCache): # It's either in the cache (and valid), and therefore anything since the server # started would be fine. # Or, it's not in the cache at all. creation_time = cache._expire_info[cache.make_key(key)] - settings.CACHE_TIME last_modified = datetime.datetime.fromtimestamp(creation_time) return last_modified
def caching(): """ Find articles and adding them into cache if cache is empty :param articles: All articles want to be listed in the index page :return: result: articles in the cache """ cache_key = 'articles' cache_time = 60 * 60 # time to live in seconds cache = get_cache("default") result = cache.get(cache_key) if not result: articles = Article.objects.select_related().order_by( '-pub_date').prefetch_related('categories') print articles # if articles has no image for article in articles: if article.image == "": article.image = "blog/not-found.png" article.save() cache.set(cache_key, articles, cache_time) result = cache.get(cache_key) return result
def calc_last_modified(request, *args, **kwargs): """ Returns the file's modified time as the last-modified date """ assert "cache_name" in kwargs, "Must specify cache_name as a keyword arg." try: cache = get_cache(kwargs["cache_name"]) assert isinstance(cache, FileBasedCache) or isinstance( cache, LocMemCache), "requires file-based or mem-based cache." except InvalidCacheBackendError: return None key = django_get_cache_key(request, cache=cache) if key is None or not cache.has_key(key): return None if isinstance(cache, FileBasedCache): fname = cache._key_to_file(cache.make_key(key)) if not os.path.exists( fname ): # would happen only if cache expired AFTER getting the key return None last_modified = datetime.datetime.fromtimestamp( os.path.getmtime(fname)) elif isinstance(cache, LocMemCache): # It's either in the cache (and valid), and therefore anything since the server # started would be fine. # Or, it's not in the cache at all. creation_time = cache._expire_info[cache.make_key( key)] - settings.CACHE_TIME last_modified = datetime.datetime.fromtimestamp(creation_time) return last_modified
def calc_last_modified(request, *args, **kwargs): """ Returns the file's modified time as the last-modified date """ assert "cache_name" in kwargs, "Must specify cache_name as a keyword arg." try: cache = get_cache(kwargs["cache_name"]) assert isinstance(cache, FileBasedCache), "requires file-based cache." except InvalidCacheBackendError: return None key = get_cache_key(request, cache=cache) if key is None: return None fname = cache._key_to_file(cache.make_key(key)) if not os.path.exists(fname): # would happen only if cache expired AFTER getting the key return None last_modified = datetime.datetime.fromtimestamp(os.path.getmtime(fname)) return last_modified
def calc_last_modified(request, *args, **kwargs): """ Returns the file's modified time as the last-modified date """ assert "cache_name" in kwargs, "Must specify cache_name as a keyword arg." try: cache = get_cache(kwargs["cache_name"]) assert isinstance(cache, FileBasedCache), "requires file-based cache." except InvalidCacheBackendError: return None key = get_cache_key(request, cache=cache) if key is None: return None fname = cache._key_to_file(cache.make_key(key)) if not os.path.exists( fname): # would happen only if cache expired AFTER getting the key return None last_modified = datetime.datetime.fromtimestamp(os.path.getmtime(fname)) return last_modified
def get_web_cache(): return get_cache(settings.CACHE_NAME) if caching_is_enabled() else None
def delete_cache(): cache_key = 'articles' cache = get_cache("default") cache.delete(cache_key)
def get_web_cache(): return get_cache('default') if caching_is_enabled() else None
def get_web_cache(): return get_cache("web_cache") if settings.CACHE_TIME != 0 else None
def get_web_cache(): return get_cache("web_cache") if caching_is_enabled() else None