Commands for validating and invalidating the cache

This commit is contained in:
Matthew Tretter 2012-02-02 00:19:46 -05:00
parent c4fc09c688
commit 8b6ba687e4
4 changed files with 75 additions and 31 deletions

View file

@ -0,0 +1,14 @@
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

@ -0,0 +1,30 @@
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',
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

@ -1,31 +0,0 @@
"""
Flushes and re-caches all images under ImageKit.
"""
from django.db.models.loading import cache
from django.core.management.base import BaseCommand
from .utils import get_spec_files
class Command(BaseCommand):
help = ('Clears all ImageKit cached files.')
args = '[apps]'
requires_model_validation = True
can_import_settings = True
def handle(self, *args, **options):
return flush_cache(args, options)
def flush_cache(apps, options):
apps = [a.strip(',') for a in apps]
if apps:
for app_label in apps:
app = cache.get_app(app_label)
for model in [m for m in cache.get_models(app)]:
print 'Flushing cache for "%s.%s"' % (app_label, model.__name__)
for obj in model.objects.order_by('-pk'):
for f in get_spec_files(obj):
f.invalidate()
else:
print 'Please specify one or more app names'

View file

@ -1,6 +1,7 @@
import tempfile
import types
from django.db.models.loading import cache
from django.utils.functional import wraps
from imagekit.lib import Image, ImageFile
@ -134,3 +135,33 @@ def format_to_extension(format):
if not extension:
raise UnknownFormatError(format)
return extension
def _get_models(apps):
models = []
for app_label in apps or []:
app = cache.get_app(app_label)
models += [m for m in cache.get_models(app)]
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()