issue when using with multi-cache backends #30

Closed
feifangit opened this Issue Feb 13, 2012 · 4 comments

Comments

Projects
None yet
3 participants
@feifangit

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

<settings.py>
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',
    },
}    

}

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

because, in <django-redis-cache / redis_cache / cache.py>
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

This comment has been minimized.

Show comment
Hide comment
@sebleier

sebleier Feb 13, 2012

Owner

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

Owner

sebleier commented Feb 13, 2012

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

@feifangit

This comment has been minimized.

Show comment
Hide comment
@feifangit

feifangit Feb 13, 2012

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

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

@sebleier

This comment has been minimized.

Show comment
Hide comment
@sebleier

sebleier Feb 13, 2012

Owner

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

Cheers,

--Sean

Owner

sebleier commented Feb 13, 2012

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

Cheers,

--Sean

@jezdez

This comment has been minimized.

Show comment
Hide comment
@jezdez

jezdez May 18, 2012

Contributor

Fixed in ab08444.

Contributor

jezdez commented May 18, 2012

Fixed in ab08444.

@sebleier sebleier closed this Jul 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment