Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
def get_web_cache():
    return get_cache(settings.CACHE_NAME) if caching_is_enabled() else None
Esempio n. 7
0
def delete_cache():
    cache_key = 'articles'
    cache = get_cache("default")
    cache.delete(cache_key)
Esempio n. 8
0
def get_web_cache():
    return get_cache(settings.CACHE_NAME) if caching_is_enabled() else None
Esempio n. 9
0
def get_web_cache():
    return get_cache('default') if caching_is_enabled() else None
Esempio n. 10
0
def get_web_cache():
    return get_cache("web_cache") if settings.CACHE_TIME != 0 else None
Esempio n. 11
0
def get_web_cache():
    return get_cache("web_cache") if caching_is_enabled() else None
Esempio n. 12
0
def get_web_cache():
    return get_cache('default') if caching_is_enabled() else None
Esempio n. 13
0
def get_web_cache():
    return get_cache("web_cache") if caching_is_enabled() else None