Make it possible to configure IMAGEKIT_CACHE_TIMEOUT

By default cache forever
This commit is contained in:
Venelin Stoykov 2017-02-09 00:44:32 +02:00
parent 95e484d073
commit 4d1ee41f2e
4 changed files with 34 additions and 8 deletions

View file

@ -100,13 +100,21 @@ ImageKit. Each has its own pros and cons.
Caching Data About Generated Files Caching Data About Generated Files
---------------------------------- ----------------------------------
The easiest, and most significant improvement you can make to improve the Generally, once a file is generated, you will never be removing it, so by
performance of your site is to have ImageKit cache the state of your generated default ImageKit will use default cache to cache the state of generated
files. The default cache file backend will already do this (if ``DEBUG`` is files "forever" (or only 5 minutes when ``DEBUG = True``).
``False``), using your default Django cache backend, but you can make it way
better by setting ``IMAGEKIT_CACHE_BACKEND``. Generally, once a file is The time for which ImageKit will cache state is configured with
generated, you will never be removing it; therefore, if you can, you should set ``IMAGEKIT_CACHE_TIMEOUT``. If set to ``None`` this means "never expire"
``IMAGEKIT_CACHE_BACKEND`` to a cache backend that will cache forever. (default when ``DEBUG = False``). You can reduce this timeout if you want
or set it to some numeric value in seconds if your cache backend behaves
differently and for example do not cache values if timeout is ``None``.
If you clear your cache durring deployment or some other reason probably
you do not want to lose the cache for generated images especcialy if you
are using some slow remote storage (like Amazon S3). Then you can configure
seprate cache (for example redis) in your ``CACHES`` config and tell ImageKit
to use it instead of the default cache by setting ``IMAGEKIT_CACHE_BACKEND``.
Pre-Generating Images Pre-Generating Images

View file

@ -55,6 +55,15 @@ Settings
.. _`Django cache section`: https://docs.djangoproject.com/en/1.8/topics/cache/#accessing-the-cache .. _`Django cache section`: https://docs.djangoproject.com/en/1.8/topics/cache/#accessing-the-cache
.. attribute:: IMAGEKIT_CACHE_TIMEOUT
:default: ``None``
Use when you need to override the timeout used to cache file state.
By default it is "cache forever".
It's highly recommended that you use a very high timeout.
.. attribute:: IMAGEKIT_CACHE_PREFIX .. attribute:: IMAGEKIT_CACHE_PREFIX
:default: ``'imagekit:'`` :default: ``'imagekit:'``

View file

@ -2,6 +2,7 @@ from ..utils import get_singleton, get_cache, sanitize_cache_key
import warnings import warnings
from copy import copy from copy import copy
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
class CacheFileState(object): class CacheFileState(object):
@ -74,7 +75,7 @@ class CachedFileBackend(object):
if state == CacheFileState.DOES_NOT_EXIST: if state == CacheFileState.DOES_NOT_EXIST:
self.cache.set(key, state, self.existence_check_timeout) self.cache.set(key, state, self.existence_check_timeout)
else: else:
self.cache.set(key, state) self.cache.set(key, state, settings.IMAGEKIT_CACHE_TIMEOUT)
def __getstate__(self): def __getstate__(self):
state = copy(self.__dict__) state = copy(self.__dict__)

View file

@ -14,6 +14,7 @@ class ImageKitConf(AppConf):
CACHE_BACKEND = None CACHE_BACKEND = None
CACHE_PREFIX = 'imagekit:' CACHE_PREFIX = 'imagekit:'
CACHE_TIMEOUT = None
USE_MEMCACHED_SAFE_CACHE_KEY = True USE_MEMCACHED_SAFE_CACHE_KEY = True
def configure_cache_backend(self, value): def configure_cache_backend(self, value):
@ -26,6 +27,13 @@ class ImageKitConf(AppConf):
return value return value
def configure_cache_timeout(self, value):
if value is None and settings.DEBUG:
# If value is not configured and is DEBUG set it to 5 minutes
return 300
# Otherwise leave it as is. If it is None then valies will never expire
return value
def configure_default_file_storage(self, value): def configure_default_file_storage(self, value):
if value is None: if value is None:
value = settings.DEFAULT_FILE_STORAGE value = settings.DEFAULT_FILE_STORAGE