Reduce calls to backend

Reading from an `ImageCacheFile`, will result in accessing its `file`
attribute repeatedly which would result in the `before_access` signal
being dispatched, which in turn would result in many unnecessary calls
to the image cache backend. With this change, we don't send
`before_access` if the file has already been created.

Similarly, we don't need to try to generate the image if we know for
certain that it's already been generated (because we have a reference
to it).
This commit is contained in:
Matthew Tretter 2013-05-10 02:56:44 -04:00
parent bc49f9cf8b
commit 906fbbd463

View file

@ -56,7 +56,8 @@ class ImageCacheFile(BaseIKFile, ImageFile):
super(ImageCacheFile, self).__init__(storage=storage)
def _require_file(self):
before_access.send(sender=self, file=self)
if not getattr(self, '_file', None):
before_access.send(sender=self, file=self)
def generate(self, force=False):
"""
@ -64,7 +65,8 @@ class ImageCacheFile(BaseIKFile, ImageFile):
whether the file already exists or not.
"""
self.cachefile_backend.generate(self, force)
if force or not getattr(self, '_file', None):
self.cachefile_backend.generate(self, force)
def _generate(self):
# Generate the file