Skip to content

Commit

Permalink
Only create storage instances when required, not on import.
Browse files Browse the repository at this point in the history
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
malcolmt committed Dec 27, 2010
1 parent 52f9c12 commit 341a6db
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions queued_storage/backend.py
Expand Up @@ -11,15 +11,27 @@ class QueuedRemoteStorage(Storage):


def __init__(self, local, remote, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX, task=None): def __init__(self, local, remote, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX, task=None):
self.local_class = local self.local_class = local
self.local = get_storage_class(self.local_class)()
self.remote_class = remote self.remote_class = remote
self.remote = get_storage_class(self.remote_class)()
self.cache_prefix = cache_prefix 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 # allow users to override the task that uploads the image to the remote
# server # server
self.task = task or SaveToRemoteTask 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): def get_storage(self, name):
cache_result = cache.get(self.get_cache_key(name)) cache_result = cache.get(self.get_cache_key(name))


Expand Down

0 comments on commit 341a6db

Please sign in to comment.