mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-16 21:30:23 +00:00
Support should_verify_existence on strategies
This prevents extra IO. Different defaults are used for async backends since we can’t assume that `existence_required` resulted in existence synchronously.
This commit is contained in:
parent
c92f53c1b0
commit
00b4388245
4 changed files with 15 additions and 1 deletions
|
|
@ -128,7 +128,14 @@ class ImageCacheFile(BaseIKFile, ImageFile):
|
|||
# Dispatch the existence_required signal before checking to see if the
|
||||
# file exists. This gives the strategy a chance to create the file.
|
||||
existence_required.send(sender=self, file=self)
|
||||
return self.cachefile_backend.exists(self)
|
||||
|
||||
try:
|
||||
check = self.cachefile_strategy.should_verify_existence(self)
|
||||
except AttributeError:
|
||||
# All synchronous backends should have created the file as part of
|
||||
# `existence_required` if they wanted to.
|
||||
check = getattr(self.cachefile_backend, 'is_async', False)
|
||||
return self.cachefile_backend.exists(self) if check else True
|
||||
|
||||
def __getstate__(self):
|
||||
state = copy(self.__dict__)
|
||||
|
|
|
|||
|
|
@ -121,6 +121,8 @@ class BaseAsync(Simple):
|
|||
"""
|
||||
Base class for cache file backends that generate files asynchronously.
|
||||
"""
|
||||
is_async = True
|
||||
|
||||
def generate(self, file, force=False):
|
||||
# Schedule the file for generation, unless we know for sure we don't
|
||||
# need to. If an already-generated file sneaks through, that's okay;
|
||||
|
|
|
|||
|
|
@ -29,6 +29,9 @@ class Optimistic(object):
|
|||
def on_source_saved(self, file):
|
||||
file.generate()
|
||||
|
||||
def should_verify_existence(self, file):
|
||||
return False
|
||||
|
||||
|
||||
class DictStrategy(object):
|
||||
def __init__(self, callbacks):
|
||||
|
|
|
|||
|
|
@ -77,5 +77,7 @@ class DummyAsyncCacheFileBackend(Simple):
|
|||
A cache file backend meant to simulate async generation.
|
||||
|
||||
"""
|
||||
is_async = True
|
||||
|
||||
def generate(self, file, force=False):
|
||||
pass
|
||||
|
|
|
|||
Loading…
Reference in a new issue