Skip to content

Commit

Permalink
Support gzip compression
Browse files Browse the repository at this point in the history
  • Loading branch information
myungsegyo committed Oct 30, 2023
1 parent e562b5c commit 1d9c517
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ Let see an example, of how make it work with *lzma* compression format:
}
}
*Gzip* compression support:

.. code-block:: python
import gzip
CACHES = {
"default": {
# ...
"OPTIONS": {
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
}
}
}
Memcached exceptions behavior
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
19 changes: 19 additions & 0 deletions django_redis/compressors/gzip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import gzip

from ..exceptions import CompressorError
from .base import BaseCompressor


class GzipCompressor(BaseCompressor):
min_length = 15

def compress(self, value: bytes) -> bytes:
if len(value) > self.min_length:
return gzip.compress(value)
return value

def decompress(self, value: bytes) -> bytes:
try:
return gzip.decompress(value)
except gzip.BadGzipFile as e:
raise CompressorError(e)
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ commands =
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_usock {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_zlib {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_zstd {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_gzip {posargs}
{envpython} -m coverage report
{envpython} -m coverage xml

Expand Down
41 changes: 41 additions & 0 deletions tests/settings/sqlite_gzip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SECRET_KEY = "django_tests_secret_key"

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": ["redis://127.0.0.1:6379?db=1", "redis://127.0.0.1:6379?db=1"],
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
},
},
"doesnotexist": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:56379?db=1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
},
},
"sample": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379?db=1,redis://127.0.0.1:6379?db=1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
},
},
"with_prefix": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379?db=1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"COMPRESSOR": "django_redis.compressors.gzip.GzipCompressor",
},
"KEY_PREFIX": "test-prefix",
},
}

INSTALLED_APPS = ["django.contrib.sessions"]

USE_TZ = False

0 comments on commit 1d9c517

Please sign in to comment.