Commit 113c1b6d authored by Ales Zoulek's avatar Ales Zoulek

Removed custom connection pool

The are two reasons:
* django already implements CacheClass instance as singleton, we don't
need to reimplement that
* connection pool in redis.py is already tested, we shouldn't have to
test it again
* default keywargs charset='utf-8' and errors='strict' are defaults in
redis.py, se we don't need to copy it over
parent d2a51db3
...@@ -16,25 +16,6 @@ except ImportError: ...@@ -16,25 +16,6 @@ except ImportError:
class CacheConnectionPool(object):
_connection_pool = None
def get_connection_pool(self, host='127.0.0.1', port=6379, db=1, password=None,
socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict'):
if self._connection_pool is None:
kwargs = {
'db': db,
'password': password,
'socket_timeout': socket_timeout,
'encoding': charset,
'encoding_errors': errors,
'host': host,
'port': port
}
self._connection_pool = redis.ConnectionPool(**kwargs)
return self._connection_pool
pool = CacheConnectionPool()
class CacheKey(object): class CacheKey(object):
""" """
A stub string class that we can use to check if a key was created already. A stub string class that we can use to check if a key was created already.
...@@ -74,8 +55,7 @@ class CacheClass(BaseCache): ...@@ -74,8 +55,7 @@ class CacheClass(BaseCache):
else: else:
host = server or 'localhost' host = server or 'localhost'
port = 6379 port = 6379
self._cache = redis.Redis(host=host, port=port, db=db, self._cache = redis.Redis(host=host, port=port, db=db, password=password)
password=password, connection_pool=pool.get_connection_pool(host=host, port=port, db=db, password=password))
def make_key(self, key, version=None): def make_key(self, key, version=None):
......
...@@ -10,7 +10,7 @@ from django import VERSION ...@@ -10,7 +10,7 @@ from django import VERSION
from django.core.cache import get_cache from django.core.cache import get_cache
from django.test import TestCase from django.test import TestCase
from models import Poll, expensive_calculation from models import Poll, expensive_calculation
from redis_cache.cache import RedisCache, pool, ImproperlyConfigured from redis_cache.cache import RedisCache, ImproperlyConfigured
# functions/classes for complex data type tests # functions/classes for complex data type tests
def f(): def f():
...@@ -33,7 +33,8 @@ class RedisCacheTests(TestCase): ...@@ -33,7 +33,8 @@ class RedisCacheTests(TestCase):
self.cache.clear() self.cache.clear()
def reset_pool(self): def reset_pool(self):
pool._connection_pool = None if hasattr(self, 'cache'):
self.cache._cache.connection_pool.disconnect()
def get_cache(self, backend=None): def get_cache(self, backend=None):
if VERSION[0] == 1 and VERSION[1] < 3: if VERSION[0] == 1 and VERSION[1] < 3:
...@@ -276,26 +277,6 @@ class RedisCacheTests(TestCase): ...@@ -276,26 +277,6 @@ class RedisCacheTests(TestCase):
self.assertEqual(self.cache.get(old_key), None) self.assertEqual(self.cache.get(old_key), None)
self.assertEqual(self.cache.get(new_key), 'spam') self.assertEqual(self.cache.get(new_key), 'spam')
def test_connection_pool(self):
# First, let's make sure there are no connections in the pool
self.assertEqual(self.cache._cache.connection_pool._created_connections, 0)
# Now, let's tie up two connections in the pool.
c1 = self.cache._cache.connection_pool.get_connection("_")
self.assertEqual(self.cache._cache.connection_pool._created_connections, 1)
c2 = self.cache._cache.connection_pool.get_connection("_")
self.assertEqual(self.cache._cache.connection_pool._created_connections, 2)
# with 2 connections tied up, lets access a view makes sure it creates
# another connection
self.client.get("/")
self.assertEqual(self.cache._cache.connection_pool._created_connections, 3)
# The previous request releases the connection, let's call the view again
# and make sure that only 3 connections are created
self.client.get("/")
self.assertEqual(self.cache._cache.connection_pool._created_connections, 3)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment