Vivekananda Tadala avatar Vivekananda Tadala committed 4e37bc1

added redis storage service to django-storage

Comments (0)

Files changed (2)

docs/backends/redisimagestore.rst

+Redisimagestore
+===============
+
+This storage allows you to use Redis to store images in redis.
+in settings we need the following 
+
+	DIS_STORAGE_OPTIONS = { 'HOST': '127.0.0.1',
+                          'PORT':6379,
+                          'DB' : 0,
+                         }
+
+Serving files from redis using nginx
+************************************
+For serving files using nginx you need to compile nginx with HttpRedis module http://wiki.nginx.org/HttpRedis
+
+Once that is done you need to have folling in nginx conf
+
+server {
+
+    listen   media.pegox.local:80;
+    server_name media.pegox.local;
+    location / {
+        expires 24h;
+        if ($uri ~* "/(.*)" ) {
+                set $redis_key $1;
+        }
+        redis_pass     127.0.0.1:6379;
+    }
+
+}
+
+The above settings worked for me.
+Will update the document once I am completely done with the implementation

storages/backends/redisimagestore.py

+from django.conf import settings
+from django.core.files.storage import Storage
+from django.core.exceptions import ImproperlyConfigured
+import string
+import random
+import StringIO
+
+try:
+    import redis
+except ImportError:
+    raise ImproperlyConfigured, "Could not load redis dependency.\
+    \nSee https://github.com/andymccurdy/redis-py"
+
+__all__ = ( 'Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage' )
+
+class RedisStorage( Storage ):
+    _connection = None
+    def __init__( self, redis_settings = None ):
+        if not self._connection:
+            if not redis_settings:
+                redis_settings = settings.REDIS_STORAGE_OPTIONS
+            self._connection = redis.StrictRedis( redis_settings.get( "HOST", "localhost" ), redis_settings.get( "PORT", 6379 ), redis_settings.get( "DB", 0 ) )
+
+    def _open( self, name, mode = 'rb' ):
+        '''
+        open the file from redis
+        @param name:
+        @param mode: unused parameter
+        '''
+        content = self._connection.get( name )
+        return StringIO( content )
+
+    def _save( self, name, file ):
+        '''
+        save the file to redis
+        @param name:
+        @param file:
+        '''
+        file.seek( 0 )
+        content = file.read()
+        self._connection.set( name, content )
+        return name
+
+    def read( self, name ):
+        '''
+        returns the content of the file
+        @param name:
+        '''
+        return self._connection.get( name )
+
+    def delete( self, name ):
+        '''
+        delete the file from redis
+        @param name:
+        '''
+        self._connection.delete( name )
+
+    def get_available_name( self, name ):
+        '''
+        check if the given key already exists and returns new name based on the given key
+        @param name:
+        '''
+        if self._connection.exists( name ):
+            chars = string.ascii_uppercase + string.digits + string.lowercase
+            size = 6
+            rand_str = ''.join( random.choice( chars ) for x in range( size ) )
+            return "%s_%s" % ( rand_str, name )
+        else:
+            return name
+
+    def save( self, name, file ):
+        '''
+        saves the content of the file in redis
+        @param name:
+        @param file:
+        '''
+        return self._save( name, file )
+
+    def url( self, name ):
+        '''
+        using MEDIA_URL generates the url for the saved file
+        @param name:
+        '''
+        return settings.MEDIA_URL + name
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.