Added optional caching of mtime checks. Set COMPRESS_MTIME_DELAY to the amount of time (in seconds) to cache the getmtime results.

This commit is contained in:
Jannis Leidel
2010-06-02 12:15:34 +02:00
parent 15c225b32a
commit 324ef87b2d
3 changed files with 17 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ from django import template
from django.conf import settings as django_settings
from django.template.loader import render_to_string
from django.core.cache import cache
from django.core.files.base import ContentFile
from django.core.files.storage import get_storage_class
@@ -28,6 +29,16 @@ def get_hexdigest(plaintext):
return sha.new(plaintext).hexdigest()
def get_mtime(filename):
if settings.MTIME_DELAY:
key = "django_compressor.mtime.%s" % filename
mtime = cache.get(key)
if mtime is None:
mtime = os.path.getmtime(filename)
cache.set(key, mtime, settings.MTIME_DELAY)
return mtime
return os.path.getmtime(filename)
class Compressor(object):
def __init__(self, content, output_prefix="compressed"):
@@ -53,7 +64,7 @@ class Compressor(object):
@property
def mtimes(self):
return [os.path.getmtime(h[1]) for h in self.split_contents() if h[0] == 'file']
return [get_mtime(h[1]) for h in self.split_contents() if h[0] == 'file']
@property
def cachekey(self):

View File

@@ -27,3 +27,6 @@ REBUILD_TIMEOUT = getattr(settings, 'COMPRESS_REBUILD_TIMEOUT', 2592000) # 30 da
# the upper bound on how long any compression should take to be generated
# (used against dog piling, should be a lot smaller than REBUILD_TIMEOUT
MINT_DELAY = getattr(settings, 'COMPRESS_MINT_DELAY', 30) # 30 seconds
# check for file changes only after a delay (in seconds, disabled by default)
MTIME_DELAY = getattr(settings, 'COMPRESS_MTIME_DELAY', None)

View File

@@ -4,7 +4,7 @@ import posixpath
from compressor.filters import FilterBase, FilterError
from compressor.conf import settings
from compressor import get_hexdigest
from compressor import get_hexdigest, get_mtime
class CssAbsoluteFilter(FilterBase):
def input(self, filename=None, **kwargs):
@@ -17,7 +17,7 @@ class CssAbsoluteFilter(FilterBase):
self.media_path = self.media_path.lstrip('/')
self.media_url = settings.MEDIA_URL.rstrip('/')
try:
mtime = os.path.getmtime(filename)
mtime = get_mtime(filename)
self.mtime = get_hexdigest(str(int(mtime)))[:12]
except OSError:
self.mtime = None