Merge pull request #17 from aexeagmbh/patch-redis

Fix redis backend and add some doc
This commit is contained in:
Andrew Godwin 2015-09-17 09:32:42 -07:00
commit d947d42e36
3 changed files with 25 additions and 2 deletions

View File

@ -27,6 +27,10 @@ class RedisChannelBackend(BaseChannelBackend):
return redis.Redis(host=self.host, port=self.port) return redis.Redis(host=self.host, port=self.port)
def send(self, channel, message): def send(self, channel, message):
# if channel is no str (=> bytes) convert it
if not isinstance(channel, str):
channel = channel.decode('utf-8')
# Write out message into expiring key (avoids big items in list) # Write out message into expiring key (avoids big items in list)
key = self.prefix + uuid.uuid4().get_hex() key = self.prefix + uuid.uuid4().get_hex()
self.connection.set( self.connection.set(
@ -59,7 +63,7 @@ class RedisChannelBackend(BaseChannelBackend):
content = self.connection.get(result[1]) content = self.connection.get(result[1])
if content is None: if content is None:
continue continue
return result[0][len(self.prefix):], json.loads(content) return result[0][len(self.prefix):].decode("utf-8"), json.loads(content.decode("utf-8"))
else: else:
return None, None return None, None

View File

@ -21,6 +21,20 @@ Database
Redis Redis
----- -----
To use the Redis backend you have to install the redis package::
pip install -U redis
Also you need to set the following in the ``CHANNEL_BACKENDS`` setting::
CHANNEL_BACKENDS = {
"default": {
"BACKEND": "channels.backends.redis_py.RedisChannelBackend",
"HOST": "redis-hostname",
},
}
Writing Custom Backends Writing Custom Backends
----------------------- -----------------------

View File

@ -36,11 +36,16 @@ here's an example for a remote Redis server::
CHANNEL_BACKENDS = { CHANNEL_BACKENDS = {
"default": { "default": {
"BACKEND": "channels.backends.redis.RedisChannelBackend", "BACKEND": "channels.backends.redis_py.RedisChannelBackend",
"HOST": "redis-channel", "HOST": "redis-channel",
}, },
} }
To use the Redis backend you have to install the redis package::
pip install -U redis
Make sure the same setting file is used across all your workers, interfaces Make sure the same setting file is used across all your workers, interfaces
and WSGI apps; without it, they won't be able to talk to each other and things and WSGI apps; without it, they won't be able to talk to each other and things
will just fail to work. will just fail to work.