Skip to content

Commit d7f1292

Browse files
authored
Merge pull request #688 from myungsegyo/master
2 parents 55097ae + 4dee637 commit d7f1292

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ David Zderic / dzderic <https://github.com/dzderic>
1414
Kirill Zaitsev / teferi <https://github.com/teferi>
1515
Jon Dufresne <https://github.com/jdufresne>
1616
Anès Foufa <https://github.com/AnesFoufa>
17+
Segyo Myung <https://github.com/myungsegyo>

README.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ Let see an example, of how make it work with *lzma* compression format:
291291
}
292292
}
293293
294+
*Gzip* compression support:
295+
296+
.. code-block:: python
297+
298+
import gzip
299+
300+
CACHES = {
301+
"default": {
302+
# ...
303+
"OPTIONS": {
304+
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
305+
}
306+
}
307+
}
308+
294309
Memcached exceptions behavior
295310
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
296311

changelog.d/688.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support gzip compression

django_redis/compressors/gzip.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import gzip
2+
3+
from django_redis.compressors.base import BaseCompressor
4+
from django_redis.exceptions import CompressorError
5+
6+
7+
class GzipCompressor(BaseCompressor):
8+
min_length = 15
9+
10+
def compress(self, value: bytes) -> bytes:
11+
if len(value) > self.min_length:
12+
return gzip.compress(value)
13+
return value
14+
15+
def decompress(self, value: bytes) -> bytes:
16+
try:
17+
return gzip.decompress(value)
18+
except gzip.BadGzipFile as e:
19+
raise CompressorError from e

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ commands =
9191
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_usock {posargs}
9292
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_zlib {posargs}
9393
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_zstd {posargs}
94+
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_gzip {posargs}
9495
{envpython} -m coverage report
9596
{envpython} -m coverage xml
9697

tests/settings/sqlite_gzip.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
SECRET_KEY = "django_tests_secret_key"
2+
3+
CACHES = {
4+
"default": {
5+
"BACKEND": "django_redis.cache.RedisCache",
6+
"LOCATION": ["redis://127.0.0.1:6379?db=1", "redis://127.0.0.1:6379?db=1"],
7+
"OPTIONS": {
8+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
9+
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
10+
},
11+
},
12+
"doesnotexist": {
13+
"BACKEND": "django_redis.cache.RedisCache",
14+
"LOCATION": "redis://127.0.0.1:56379?db=1",
15+
"OPTIONS": {
16+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
17+
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
18+
},
19+
},
20+
"sample": {
21+
"BACKEND": "django_redis.cache.RedisCache",
22+
"LOCATION": "redis://127.0.0.1:6379?db=1,redis://127.0.0.1:6379?db=1",
23+
"OPTIONS": {
24+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
25+
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
26+
},
27+
},
28+
"with_prefix": {
29+
"BACKEND": "django_redis.cache.RedisCache",
30+
"LOCATION": "redis://127.0.0.1:6379?db=1",
31+
"OPTIONS": {
32+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
33+
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
34+
},
35+
"KEY_PREFIX": "test-prefix",
36+
},
37+
}
38+
39+
INSTALLED_APPS = ["django.contrib.sessions"]
40+
41+
USE_TZ = False

0 commit comments

Comments
 (0)