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
e6517983
Commit
e6517983
authored
Jul 20, 2015
by
Sean Bleier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for compression/decompression.
parent
5dc7a9e1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
196 additions
and
6 deletions
+196
-6
Makefile
Makefile
+1
-1
base.py
redis_cache/backends/base.py
+35
-4
compressors.py
redis_cache/compressors.py
+53
-0
compressor_tests.py
tests/testapp/tests/compressor_tests.py
+106
-0
socket_timeout_tests.py
tests/testapp/tests/socket_timeout_tests.py
+1
-1
No files found.
Makefile
View file @
e6517983
...
...
@@ -21,7 +21,7 @@ clean:
.PHONY
:
test
test
:
install_requirements
PYTHONPATH
=
$(PYTHONPATH)
: django-admin.py
test
tests.testapp.tests.socket_timeout_tests:SocketTimeoutTestCase.test_socket_timeout
--settings
=
tests.settings
-s
PYTHONPATH
=
$(PYTHONPATH)
: django-admin.py
test
--settings
=
tests.settings
-s
.PHONY
:
shell
shell
:
...
...
redis_cache/backends/base.py
View file @
e6517983
...
...
@@ -60,12 +60,21 @@ class BaseRedisCache(BaseCache):
self
.
connection_pool_class_kwargs
=
(
self
.
get_connection_pool_class_kwargs
()
)
# Serializer
self
.
serializer_class
=
self
.
get_serializer_class
()
self
.
serializer_class_kwargs
=
self
.
get_serializer_class_kwargs
()
self
.
serializer
=
self
.
serializer_class
(
**
self
.
serializer_class_kwargs
)
# Compressor
self
.
compressor_class
=
self
.
get_compressor_class
()
self
.
compressor_class_kwargs
=
self
.
get_compressor_class_kwargs
()
self
.
compressor
=
self
.
compressor_class
(
**
self
.
compressor_class_kwargs
)
def
__getstate__
(
self
):
return
{
'params'
:
self
.
params
,
'server'
:
self
.
server
}
...
...
@@ -136,6 +145,21 @@ class BaseRedisCache(BaseCache):
def
get_serializer_class_kwargs
(
self
):
return
self
.
options
.
get
(
'SERIALIZER_CLASS_KWARGS'
,
{})
def
get_compressor_class
(
self
):
compressor_class
=
self
.
options
.
get
(
'COMPRESSOR_CLASS'
,
'redis_cache.compressors.NoopCompressor'
)
module_name
,
class_name
=
compressor_class
.
rsplit
(
'.'
,
1
)
module
=
import_module
(
module_name
)
try
:
return
getattr
(
module
,
class_name
)
except
AttributeError
:
raise
ImportError
(
'cannot import name
%
s'
%
class_name
)
def
get_compressor_class_kwargs
(
self
):
return
self
.
options
.
get
(
'COMPRESSOR_CLASS_KWARGS'
,
{})
def
get_master_client
(
self
):
"""
Get the write server:port of the master cache
...
...
@@ -175,17 +199,25 @@ class BaseRedisCache(BaseCache):
def
deserialize
(
self
,
value
):
return
self
.
serializer
.
deserialize
(
value
)
def
compress
(
self
,
value
):
return
self
.
compressor
.
compress
(
value
)
def
decompress
(
self
,
value
):
return
self
.
compressor
.
decompress
(
value
)
def
get_value
(
self
,
original
):
try
:
value
=
int
(
original
)
except
(
ValueError
,
TypeError
):
value
=
self
.
deserialize
(
original
)
value
=
self
.
decompress
(
original
)
value
=
self
.
deserialize
(
value
)
return
value
def
prep_value
(
self
,
value
):
if
isinstance
(
value
,
int
)
and
not
isinstance
(
value
,
bool
):
return
value
return
self
.
serialize
(
value
)
value
=
self
.
serialize
(
value
)
return
self
.
compress
(
value
)
def
make_key
(
self
,
key
,
version
=
None
):
if
not
isinstance
(
key
,
CacheKey
):
...
...
@@ -382,8 +414,7 @@ class BaseRedisCache(BaseCache):
keys
=
client
.
keys
(
'*'
)
for
key
in
keys
:
timeout
=
client
.
ttl
(
key
)
value
=
self
.
deserialize
(
client
.
get
(
key
))
value
=
self
.
get_value
(
client
.
get
(
key
))
if
timeout
is
None
:
client
.
set
(
key
,
self
.
prep_value
(
value
))
...
...
redis_cache/compressors.py
0 → 100644
View file @
e6517983
import
zlib
try
:
import
bz2
except
ImportError
:
pass
class
BaseCompressor
(
object
):
def
__init__
(
self
,
**
kwargs
):
super
(
BaseCompressor
,
self
)
.
__init__
()
def
compress
(
self
,
value
):
raise
NotImplementedError
def
decompress
(
self
,
value
):
raise
NotImplementedError
class
NoopCompressor
(
BaseCompressor
):
def
compress
(
self
,
value
):
return
value
def
decompress
(
self
,
value
):
return
value
class
ZLibCompressor
(
BaseCompressor
):
def
__init__
(
self
,
level
=
6
):
self
.
level
=
level
super
(
ZLibCompressor
,
self
)
.
__init__
()
def
compress
(
self
,
value
):
return
zlib
.
compress
(
value
,
self
.
level
)
def
decompress
(
self
,
value
):
return
zlib
.
decompress
(
value
)
class
BZip2Compressor
(
BaseCompressor
):
def
__init__
(
self
,
compresslevel
=
9
):
self
.
compresslevel
=
compresslevel
super
(
BZip2Compressor
,
self
)
.
__init__
()
def
compress
(
self
,
value
):
return
bz2
.
compress
(
value
,
compresslevel
=
self
.
compresslevel
)
def
decompress
(
self
,
value
):
return
bz2
.
decompress
(
value
)
tests/testapp/tests/compressor_tests.py
0 → 100644
View file @
e6517983
# -*- coding: utf-8 -*-
try
:
from
django.test
import
override_settings
except
ImportError
:
from
django.test.utils
import
override_settings
from
django.test
import
TestCase
from
tests.testapp.tests.base_tests
import
BaseRedisTestCase
LOCATION
=
"127.0.0.1:6381"
class
CompressionTestCase
(
object
):
def
test_compression
(
self
):
key
=
'a'
noop_cache
=
self
.
get_cache
(
'noop'
)
string
=
10000
*
'a'
self
.
cache
.
set
(
key
,
string
)
noop_cache
.
set
(
key
,
string
)
self
.
assertEqual
(
self
.
cache
.
get
(
key
),
noop_cache
.
get
(
key
))
self
.
assertNotEqual
(
self
.
cache
,
noop_cache
)
noop_client
,
=
list
(
noop_cache
.
clients
.
values
())
default_client
,
=
list
(
self
.
cache
.
clients
.
values
())
versioned_key
=
self
.
cache
.
make_key
(
key
)
self
.
assertLess
(
len
(
default_client
.
get
(
versioned_key
)),
len
(
noop_client
.
get
(
versioned_key
)),
)
@
override_settings
(
CACHES
=
{
'default'
:
{
'BACKEND'
:
'redis_cache.RedisCache'
,
'LOCATION'
:
LOCATION
,
'OPTIONS'
:
{
'DB'
:
14
,
'PASSWORD'
:
'yadayada'
,
'PARSER_CLASS'
:
'redis.connection.HiredisParser'
,
'PICKLE_VERSION'
:
-
1
,
'COMPRESSOR_CLASS'
:
'redis_cache.compressors.ZLibCompressor'
,
'COMPRESSOR_CLASS_KWARGS'
:
{
'level'
:
5
,
},
'CONNECTION_POOL_CLASS'
:
'redis.ConnectionPool'
,
'CONNECTION_POOL_CLASS_KWARGS'
:
{
'max_connections'
:
2
,
},
},
},
'noop'
:
{
'BACKEND'
:
'redis_cache.RedisCache'
,
'LOCATION'
:
LOCATION
,
'OPTIONS'
:
{
'DB'
:
15
,
'PASSWORD'
:
'yadayada'
,
'PARSER_CLASS'
:
'redis.connection.HiredisParser'
,
'PICKLE_VERSION'
:
-
1
,
'COMPRESSOR_CLASS'
:
'redis_cache.compressors.NoopCompressor'
,
},
},
}
)
class
ZLibTestCase
(
CompressionTestCase
,
BaseRedisTestCase
,
TestCase
):
pass
@
override_settings
(
CACHES
=
{
'default'
:
{
'BACKEND'
:
'redis_cache.RedisCache'
,
'LOCATION'
:
LOCATION
,
'OPTIONS'
:
{
'DB'
:
14
,
'PASSWORD'
:
'yadayada'
,
'PARSER_CLASS'
:
'redis.connection.HiredisParser'
,
'PICKLE_VERSION'
:
-
1
,
'COMPRESSOR_CLASS'
:
'redis_cache.compressors.BZip2Compressor'
,
'COMPRESSOR_CLASS_KWARGS'
:
{
'compresslevel'
:
5
,
},
'CONNECTION_POOL_CLASS'
:
'redis.ConnectionPool'
,
'CONNECTION_POOL_CLASS_KWARGS'
:
{
'max_connections'
:
2
,
},
},
},
'noop'
:
{
'BACKEND'
:
'redis_cache.RedisCache'
,
'LOCATION'
:
LOCATION
,
'OPTIONS'
:
{
'DB'
:
15
,
'PASSWORD'
:
'yadayada'
,
'PARSER_CLASS'
:
'redis.connection.HiredisParser'
,
'PICKLE_VERSION'
:
-
1
,
'COMPRESSOR_CLASS'
:
'redis_cache.compressors.NoopCompressor'
,
},
},
}
)
class
BZip2TestCase
(
CompressionTestCase
,
BaseRedisTestCase
,
TestCase
):
pass
tests/testapp/tests/socket_timeout_tests.py
View file @
e6517983
...
...
@@ -8,7 +8,7 @@ from django.test import TestCase
from
redis.exceptions
import
ConnectionError
from
tests.testapp.tests.base_tests
import
SetupMixin
LOCATION
=
"127.0.0.1:638
2
"
LOCATION
=
"127.0.0.1:638
1
"
@
override_settings
(
...
...
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