Commits

Anonymous committed 767b31d

Added REDIS-based caching to get_storage method.
Added a few lines of logging.

  • Participants
  • Parent commits 33962f2

Comments (0)

Files changed (1)

File storages/backends/moka.py

 from queued_storage.backend import QueuedRemoteStorage
 import logging
 
+from django.conf import settings
+
+from redis import Redis  # depends on Redis server
+
 # Get an instance of a logger
 logger = logging.getLogger(__name__)
 logger.setLevel(logging.INFO)
 
+REDIS = getattr(settings, 'REDIS', None)
+REMOTE = "remote"  # local const
 
 class MokaStorage(QueuedRemoteStorage):
     """Moka custom storage."""
         super(MokaStorage, self).__init__(
             local='django.core.files.storage.FileSystemStorage',
             remote='storages.backends.mosso.CloudFilesStorage')
+
+        self._redis = Redis(**settings.REDIS["storage"])
+        logger.info("Connection to redis is now active: %s", self._redis)
+
+    def get_storage(self, name):
+        cache_result = self._redis.sismember(REMOTE, name)
+
+        if cache_result:
+            logger.info("Cache hit for file '%s'", name)
+            return self.remote
+
+        elif cache_result is None and self.remote.exists(name):
+            logger.info("Caching remote storage for file '%s'", name)
+            self._redis.sadd(REMOTE, name)
+            return self.remote
+
+        logger.info("File '%s' was not found on the remote resource", name)
+        return self.local