From 324ef87b2d397764d7ae502f3ce82bd2ff40854b Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Wed, 2 Jun 2010 12:15:34 +0200 Subject: [PATCH] Added optional caching of mtime checks. Set COMPRESS_MTIME_DELAY to the amount of time (in seconds) to cache the getmtime results. --- compressor/__init__.py | 13 ++++++++++++- compressor/conf/settings.py | 3 +++ compressor/filters/css_default.py | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compressor/__init__.py b/compressor/__init__.py index 4bd52d5..e52b92a 100644 --- a/compressor/__init__.py +++ b/compressor/__init__.py @@ -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): diff --git a/compressor/conf/settings.py b/compressor/conf/settings.py index 3925e2c..d65d4f6 100644 --- a/compressor/conf/settings.py +++ b/compressor/conf/settings.py @@ -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) diff --git a/compressor/filters/css_default.py b/compressor/filters/css_default.py index 0b33039..fca6e87 100644 --- a/compressor/filters/css_default.py +++ b/compressor/filters/css_default.py @@ -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