Skip to content

Loading…

issue when using with multi-cache backends #30

Closed
feifangit opened this Issue · 4 comments

3 participants

@feifangit

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
        )
        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 ~

@sebleier
Owner

Thanks for the report, do you mind forking and issuing a pull request? Also, tests would be nice too ;)

@feifangit

sure, I am brand new to github, but I'll try that>.<

@sebleier
Owner

I just want you to get credit for any changes you make.

Cheers,

--Sean

@jezdez

Fixed in ab08444.

@sebleier sebleier closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.