Skip to content

Commit 324ef87

Browse files
committed
Added optional caching of mtime checks. Set COMPRESS_MTIME_DELAY to the amount of time (in seconds) to cache the getmtime results.
1 parent 15c225b commit 324ef87

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

compressor/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.conf import settings as django_settings
66
from django.template.loader import render_to_string
77

8+
from django.core.cache import cache
89
from django.core.files.base import ContentFile
910
from django.core.files.storage import get_storage_class
1011

@@ -28,6 +29,16 @@ def get_hexdigest(plaintext):
2829
return sha.new(plaintext).hexdigest()
2930

3031

32+
def get_mtime(filename):
33+
if settings.MTIME_DELAY:
34+
key = "django_compressor.mtime.%s" % filename
35+
mtime = cache.get(key)
36+
if mtime is None:
37+
mtime = os.path.getmtime(filename)
38+
cache.set(key, mtime, settings.MTIME_DELAY)
39+
return mtime
40+
return os.path.getmtime(filename)
41+
3142
class Compressor(object):
3243

3344
def __init__(self, content, output_prefix="compressed"):
@@ -53,7 +64,7 @@ def soup(self):
5364

5465
@property
5566
def mtimes(self):
56-
return [os.path.getmtime(h[1]) for h in self.split_contents() if h[0] == 'file']
67+
return [get_mtime(h[1]) for h in self.split_contents() if h[0] == 'file']
5768

5869
@property
5970
def cachekey(self):

compressor/conf/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@
2727
# the upper bound on how long any compression should take to be generated
2828
# (used against dog piling, should be a lot smaller than REBUILD_TIMEOUT
2929
MINT_DELAY = getattr(settings, 'COMPRESS_MINT_DELAY', 30) # 30 seconds
30+
31+
# check for file changes only after a delay (in seconds, disabled by default)
32+
MTIME_DELAY = getattr(settings, 'COMPRESS_MTIME_DELAY', None)

compressor/filters/css_default.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from compressor.filters import FilterBase, FilterError
66
from compressor.conf import settings
7-
from compressor import get_hexdigest
7+
from compressor import get_hexdigest, get_mtime
88

99
class CssAbsoluteFilter(FilterBase):
1010
def input(self, filename=None, **kwargs):
@@ -17,7 +17,7 @@ def input(self, filename=None, **kwargs):
1717
self.media_path = self.media_path.lstrip('/')
1818
self.media_url = settings.MEDIA_URL.rstrip('/')
1919
try:
20-
mtime = os.path.getmtime(filename)
20+
mtime = get_mtime(filename)
2121
self.mtime = get_hexdigest(str(int(mtime)))[:12]
2222
except OSError:
2323
self.mtime = None

0 commit comments

Comments
 (0)