Skip to content

Commit

Permalink
Make it possible to configure IMAGEKIT_CACHE_TIMEOUT
Browse files Browse the repository at this point in the history
By default cache forever
  • Loading branch information
vstoykov committed Feb 17, 2017
1 parent 95e484d commit 4d1ee41
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
22 changes: 15 additions & 7 deletions docs/caching.rst
Expand Up @@ -100,13 +100,21 @@ ImageKit. Each has its own pros and cons.
Caching Data About Generated Files
----------------------------------

The easiest, and most significant improvement you can make to improve the
performance of your site is to have ImageKit cache the state of your generated
files. The default cache file backend will already do this (if ``DEBUG`` is
``False``), using your default Django cache backend, but you can make it way
better by setting ``IMAGEKIT_CACHE_BACKEND``. Generally, once a file is
generated, you will never be removing it; therefore, if you can, you should set
``IMAGEKIT_CACHE_BACKEND`` to a cache backend that will cache forever.
Generally, once a file is generated, you will never be removing it, so by
default ImageKit will use default cache to cache the state of generated
files "forever" (or only 5 minutes when ``DEBUG = True``).

The time for which ImageKit will cache state is configured with
``IMAGEKIT_CACHE_TIMEOUT``. If set to ``None`` this means "never expire"
(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
Expand Down
9 changes: 9 additions & 0 deletions docs/configuration.rst
Expand Up @@ -55,6 +55,15 @@ Settings
.. _`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

:default: ``'imagekit:'``
Expand Down
3 changes: 2 additions & 1 deletion imagekit/cachefiles/backends.py
Expand Up @@ -2,6 +2,7 @@
import warnings
from copy import copy
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings


class CacheFileState(object):
Expand Down Expand Up @@ -74,7 +75,7 @@ def set_state(self, file, state):
if state == CacheFileState.DOES_NOT_EXIST:
self.cache.set(key, state, self.existence_check_timeout)
else:
self.cache.set(key, state)
self.cache.set(key, state, settings.IMAGEKIT_CACHE_TIMEOUT)

def __getstate__(self):
state = copy(self.__dict__)
Expand Down
8 changes: 8 additions & 0 deletions imagekit/conf.py
Expand Up @@ -14,6 +14,7 @@ class ImageKitConf(AppConf):

CACHE_BACKEND = None
CACHE_PREFIX = 'imagekit:'
CACHE_TIMEOUT = None
USE_MEMCACHED_SAFE_CACHE_KEY = True

def configure_cache_backend(self, value):
Expand All @@ -26,6 +27,13 @@ def configure_cache_backend(self, 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):
if value is None:
value = settings.DEFAULT_FILE_STORAGE
Expand Down

0 comments on commit 4d1ee41

Please sign in to comment.