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:
@@ -5,6 +5,7 @@ from django import template
|
|||||||
from django.conf import settings as django_settings
|
from django.conf import settings as django_settings
|
||||||
from django.template.loader import render_to_string
|
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.base import ContentFile
|
||||||
from django.core.files.storage import get_storage_class
|
from django.core.files.storage import get_storage_class
|
||||||
|
|
||||||
@@ -28,6 +29,16 @@ def get_hexdigest(plaintext):
|
|||||||
return sha.new(plaintext).hexdigest()
|
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):
|
class Compressor(object):
|
||||||
|
|
||||||
def __init__(self, content, output_prefix="compressed"):
|
def __init__(self, content, output_prefix="compressed"):
|
||||||
@@ -53,7 +64,7 @@ class Compressor(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def mtimes(self):
|
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
|
@property
|
||||||
def cachekey(self):
|
def cachekey(self):
|
||||||
|
|||||||
@@ -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
|
# 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
|
# (used against dog piling, should be a lot smaller than REBUILD_TIMEOUT
|
||||||
MINT_DELAY = getattr(settings, 'COMPRESS_MINT_DELAY', 30) # 30 seconds
|
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)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import posixpath
|
|||||||
|
|
||||||
from compressor.filters import FilterBase, FilterError
|
from compressor.filters import FilterBase, FilterError
|
||||||
from compressor.conf import settings
|
from compressor.conf import settings
|
||||||
from compressor import get_hexdigest
|
from compressor import get_hexdigest, get_mtime
|
||||||
|
|
||||||
class CssAbsoluteFilter(FilterBase):
|
class CssAbsoluteFilter(FilterBase):
|
||||||
def input(self, filename=None, **kwargs):
|
def input(self, filename=None, **kwargs):
|
||||||
@@ -17,7 +17,7 @@ class CssAbsoluteFilter(FilterBase):
|
|||||||
self.media_path = self.media_path.lstrip('/')
|
self.media_path = self.media_path.lstrip('/')
|
||||||
self.media_url = settings.MEDIA_URL.rstrip('/')
|
self.media_url = settings.MEDIA_URL.rstrip('/')
|
||||||
try:
|
try:
|
||||||
mtime = os.path.getmtime(filename)
|
mtime = get_mtime(filename)
|
||||||
self.mtime = get_hexdigest(str(int(mtime)))[:12]
|
self.mtime = get_hexdigest(str(int(mtime)))[:12]
|
||||||
except OSError:
|
except OSError:
|
||||||
self.mtime = None
|
self.mtime = None
|
||||||
|
|||||||
Reference in New Issue
Block a user