Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
D
Django-Redis-Cache
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shared
Django-Redis-Cache
Commits
a7bb91b0
Commit
a7bb91b0
authored
Feb 19, 2014
by
Sean Bleier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds MAX_CONNECTIONS option for the connection pool.
parent
dd3645d4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
7 deletions
+43
-7
cache.py
redis_cache/cache.py
+9
-2
tcptests.py
tcptests.py
+2
-1
settings.py
tests/settings.py
+1
-0
tests.py
tests/testapp/tests.py
+31
-4
No files found.
redis_cache/cache.py
View file @
a7bb91b0
...
...
@@ -47,8 +47,8 @@ class CacheConnectionPool(object):
def
get_connection_pool
(
self
,
host
=
'127.0.0.1'
,
port
=
6379
,
db
=
1
,
password
=
None
,
parser_class
=
None
,
unix_socket_path
=
None
):
connection_identifier
=
(
host
,
port
,
db
,
parser_class
,
unix_socket_path
)
unix_socket_path
=
None
,
max_connections
=
None
):
connection_identifier
=
(
host
,
port
,
db
,
parser_class
,
unix_socket_path
,
max_connections
)
if
not
self
.
_connection_pools
.
get
(
connection_identifier
):
connection_class
=
(
unix_socket_path
and
UnixDomainSocketConnection
or
Connection
...
...
@@ -58,6 +58,7 @@ class CacheConnectionPool(object):
'password'
:
password
,
'connection_class'
:
connection_class
,
'parser_class'
:
parser_class
,
'max_connections'
:
max_connections
,
}
if
unix_socket_path
is
None
:
kwargs
.
update
({
...
...
@@ -101,8 +102,10 @@ class CacheClass(BaseCache):
'port'
:
port
,
'unix_socket_path'
:
unix_socket_path
,
}
connection_pool
=
pool
.
get_connection_pool
(
parser_class
=
self
.
parser_class
,
max_connections
=
self
.
max_connections
,
**
kwargs
)
self
.
_client
=
redis
.
Redis
(
...
...
@@ -122,6 +125,10 @@ class CacheClass(BaseCache):
def
options
(
self
):
return
self
.
params
.
get
(
'OPTIONS'
,
{})
@
property
def
max_connections
(
self
):
return
self
.
options
.
get
(
'MAX_CONNECTIONS'
,
None
)
@
property
def
db
(
self
):
_db
=
self
.
params
.
get
(
'db'
,
self
.
options
.
get
(
'DB'
,
1
))
...
...
tcptests.py
View file @
a7bb91b0
...
...
@@ -21,7 +21,8 @@ cache_settings = {
'OPTIONS'
:
{
'DB'
:
15
,
'PASSWORD'
:
'yadayada'
,
'PARSER_CLASS'
:
'redis.connection.HiredisParser'
'PARSER_CLASS'
:
'redis.connection.HiredisParser'
,
'MAX_CONNECTIONS'
:
2
,
},
},
},
...
...
tests/settings.py
View file @
a7bb91b0
...
...
@@ -17,6 +17,7 @@ CACHES = {
'OPTIONS'
:
{
# optional
'DB'
:
15
,
'PASSWORD'
:
'yadayada'
,
'MAX_CONNECTIONS'
:
2
,
},
},
}
...
...
tests/testapp/tests.py
View file @
a7bb91b0
...
...
@@ -10,17 +10,21 @@ from django import VERSION
from
django.core.cache
import
get_cache
from
django.test
import
TestCase
from
.models
import
Poll
,
expensive_calculation
from
redis_cache.cache
import
RedisCache
,
ImproperlyConfigured
,
pool
import
redis
from
redis.connection
import
UnixDomainSocketConnection
from
redis_cache.cache
import
RedisCache
,
ImproperlyConfigured
,
pool
# functions/classes for complex data type tests
def
f
():
return
42
class
C
:
def
m
(
n
):
return
24
class
RedisCacheTests
(
TestCase
):
"""
A common set of tests derived from Django's own cache tests
...
...
@@ -349,11 +353,11 @@ class RedisCacheTests(TestCase):
def
test_multiple_connection_pool_connections
(
self
):
pool
.
_connection_pools
=
{}
c1
=
get_cache
(
'redis_cache.cache://127.0.0.1:6379?db=15'
)
get_cache
(
'redis_cache.cache://127.0.0.1:6379?db=15'
)
self
.
assertEqual
(
len
(
pool
.
_connection_pools
),
1
)
c2
=
get_cache
(
'redis_cache.cache://127.0.0.1:6379?db=14'
)
get_cache
(
'redis_cache.cache://127.0.0.1:6379?db=14'
)
self
.
assertEqual
(
len
(
pool
.
_connection_pools
),
2
)
c3
=
get_cache
(
'redis_cache.cache://127.0.0.1:6379?db=15'
)
get_cache
(
'redis_cache.cache://127.0.0.1:6379?db=15'
)
self
.
assertEqual
(
len
(
pool
.
_connection_pools
),
2
)
def
test_setting_string_integer_retrieves_string
(
self
):
...
...
@@ -366,8 +370,31 @@ class RedisCacheTests(TestCase):
self
.
assertTrue
(
self
.
cache
.
set
(
"bool_f"
,
False
))
self
.
assertEqual
(
self
.
cache
.
get
(
"bool_f"
),
False
)
def
test_max_connections
(
self
):
pool
.
_connection_pools
=
{}
cache
=
get_cache
(
'default'
)
def
noop
(
*
args
,
**
kwargs
):
pass
release
=
cache
.
_client
.
connection_pool
.
release
cache
.
_client
.
connection_pool
.
release
=
noop
self
.
assertEqual
(
cache
.
_client
.
connection_pool
.
max_connections
,
2
)
cache
.
set
(
'a'
,
'a'
)
self
.
assertEqual
(
cache
.
_client
.
connection_pool
.
_created_connections
,
1
)
cache
.
set
(
'a'
,
'a'
)
self
.
assertEqual
(
cache
.
_client
.
connection_pool
.
_created_connections
,
2
)
with
self
.
assertRaises
(
redis
.
ConnectionError
):
cache
.
set
(
'a'
,
'a'
)
self
.
assertEqual
(
cache
.
_client
.
connection_pool
.
_created_connections
,
2
)
cache
.
_client
.
connection_pool
.
release
=
release
cache
.
_client
.
connection_pool
.
max_connections
=
2
**
31
if
__name__
==
'__main__'
:
import
unittest
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment