sebleier/django-redis-cache — Issue #30: issue when using with multi-cache backends
Author: feifangit
Created: 2012-02-13 09:26:04+00:00
State: open

since Django 1.3 start supporting multi-cache backends. There's a issue in current code.

CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '192.168.32.139:6379',
'OPTIONS': {
'DB': 2,
#'PARSER_CLASS': 'redis.connection.HiredisParser',
}
},

'2ndcache': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '192.168.32.139:6379',
'OPTIONS': {
'DB': 3,
#'PARSER_CLASS': 'redis.connection.HiredisParser',
},
}
}

...
cache.set('x',1) #default cache object, using db 2
get_cache('2ndcache').set('x',2) # another cache object, should use db3 according to . But it didn't worked as I expected.

because, in
line 46, a new pool will be created when _connection_pool is None. But since one pool works for one db.
so, the when we call
get_cache('2ndcache').set('x',2)
the CacheConnectionPool.get_connection_pool still return the connection pool for db 2, not 3.

MY MODIFICATION FOR THE CODE:
use a dict, to indicate every pair of db:pool

class CacheConnectionPool(object):
_connection_pools = {} #id: reis.ConnectionPool

def get_connection_pool(self, host='127.0.0.1', port=6379, db=1,
password=None, parser_class=None,
unix_socket_path=None):
if self._connection_pools.get(db) is None:
connection_class = (
unix_socket_path and UnixDomainSocketConnection or Connection
)
logging.error("----%d" % db)
kwargs = {
'db': db,
'password': password,
'connection_class': connection_class,
'parser_class': parser_class,
}
if unix_socket_path is None:
kwargs.update({
'host': host,
'port': port,
})
else:
kwargs['path'] = unix_socket_path
self._connection_pools[db] = redis.ConnectionPool(**kwargs)
return self._connection_pools.get(db)

Thanks ~