Permalink
Browse files

Only create storage instances when required, not on import.

This drastically speeds up startup times for anything that imports all
models (which is pretty much anything in Django). It saves having to
connect to the remote storage, in particular, until it's required.
  • Loading branch information...
1 parent 52f9c12 commit 341a6dbac04f66a76ea03f53029aaddbcd5e3c5c @malcolmt committed Dec 27, 2010
Showing with 14 additions and 2 deletions.
  1. +14 −2 queued_storage/backend.py
View
@@ -11,15 +11,27 @@ class QueuedRemoteStorage(Storage):
def __init__(self, local, remote, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX, task=None):
self.local_class = local
- self.local = get_storage_class(self.local_class)()
self.remote_class = remote
- self.remote = get_storage_class(self.remote_class)()
self.cache_prefix = cache_prefix
+ self._local_instance = None
+ self._remote_instance = None
# allow users to override the task that uploads the image to the remote
# server
self.task = task or SaveToRemoteTask
+ @property
+ def local(self):
+ if self._local_instance is None:
+ self._local_instance = get_storage_class(self.local_class)()
+ return self._local_instance
+
+ @property
+ def remote(self):
+ if self._remote_instance is None:
+ self._remote_instance = get_storage_class(self.remote_class)()
+ return self._remote_instance
+
def get_storage(self, name):
cache_result = cache.get(self.get_cache_key(name))

0 comments on commit 341a6db

Please sign in to comment.