Create new cache warming command

Replaces ikcachevalidate and ikcacheinvalidate, and uses the "sources"
abstraction. Closes #165
This commit is contained in:
Matthew Tretter 2012-10-24 23:36:00 -04:00
parent 84f3b6475b
commit fb8c411f75
5 changed files with 42 additions and 66 deletions

View file

@ -1,14 +0,0 @@
from django.core.management.base import BaseCommand
from django.db.models.loading import cache
from ...utils import invalidate_app_cache
class Command(BaseCommand):
help = ('Invalidates the image cache for a list of apps.')
args = '[apps]'
requires_model_validation = True
can_import_settings = True
def handle(self, *args, **options):
apps = args or cache.app_models.keys()
invalidate_app_cache(apps)

View file

@ -1,30 +0,0 @@
from optparse import make_option
from django.core.management.base import BaseCommand
from django.db.models.loading import cache
from ...utils import validate_app_cache
class Command(BaseCommand):
help = ('Validates the image cache for a list of apps.')
args = '[apps]'
requires_model_validation = True
can_import_settings = True
option_list = BaseCommand.option_list + (
make_option('--force-revalidation',
dest='force_revalidation',
action='store_true',
default=False,
help='Invalidate each image file before validating it, thereby'
' ensuring its revalidation. This is very similar to'
' running ikcacheinvalidate and then running'
' ikcachevalidate; the difference being that this option'
' causes files to be invalidated and validated'
' one-at-a-time, whereas running the two commands in series'
' would invalidate all images before validating any.'
),
)
def handle(self, *args, **options):
apps = args or cache.app_models.keys()
validate_app_cache(apps, options['force_revalidation'])

View file

@ -0,0 +1,36 @@
from optparse import make_option
from django.core.management.base import BaseCommand
import re
from ...files import ImageSpecCacheFile
from ...specs import registry
class Command(BaseCommand):
help = ('Warm the image cache for the specified specs (or all specs if none'
' was provided). Simple wildcard matching (using asterisks) is'
' supported.')
args = '[spec_ids]'
def handle(self, *args, **options):
specs = registry.get_spec_ids()
if args:
patterns = self.compile_patterns(args)
specs = (id for id in specs if any(p.match(id) for p in patterns))
for spec_id in specs:
self.stdout.write('Validating spec: %s\n' % spec_id)
spec = registry.get_spec(spec_id) # TODO: HINTS! (Probably based on source, so this will need to be moved into loop below.)
for source in registry.get_sources(spec_id):
for source_file in source.files():
if source_file:
self.stdout.write(' %s\n' % source_file)
try:
# TODO: Allow other validation actions through command option
ImageSpecCacheFile(spec, source_file).validate()
except Exception, err:
# TODO: How should we handle failures? Don't want to error, but should call it out more than this.
self.stdout.write(' FAILED: %s\n' % err)
def compile_patterns(self, spec_ids):
return [re.compile('%s$' % '.*'.join(re.escape(part) for part in id.split('*'))) for id in spec_ids]

View file

@ -72,6 +72,9 @@ class SpecRegistry(object):
else:
return spec
def get_spec_ids(self):
return self._specs.keys()
def add_sources(self, spec_id, sources):
"""
Associates sources with a spec id
@ -82,6 +85,9 @@ class SpecRegistry(object):
self._sources[source] = set()
self._sources[source].add(spec_id)
def get_sources(self, spec_id):
return [source for source in self._sources if spec_id in self._sources[source]]
def before_access_receiver(self, sender, spec, file, **kwargs):
spec.image_cache_strategy.invoke_callback('before_access', file)

View file

@ -146,28 +146,6 @@ def _get_models(apps):
return models
def invalidate_app_cache(apps):
for model in _get_models(apps):
print 'Invalidating cache for "%s.%s"' % (model._meta.app_label, model.__name__)
for obj in model._default_manager.order_by('-pk'):
for f in get_spec_files(obj):
f.invalidate()
def validate_app_cache(apps, force_revalidation=False):
for model in _get_models(apps):
for obj in model._default_manager.order_by('-pk'):
model_name = '%s.%s' % (model._meta.app_label, model.__name__)
if force_revalidation:
print 'Invalidating & validating cache for "%s"' % model_name
else:
print 'Validating cache for "%s"' % model_name
for f in get_spec_files(obj):
if force_revalidation:
f.invalidate()
f.validate()
def suggest_extension(name, format):
original_extension = os.path.splitext(name)[1]
try: