Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gzip compression #688

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ David Zderic / dzderic <https://github.com/dzderic>
Kirill Zaitsev / teferi <https://github.com/teferi>
Jon Dufresne <https://github.com/jdufresne>
Anès Foufa <https://github.com/AnesFoufa>
Segyo Myung <https://github.com/myungsegyo>
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
1 change: 1 addition & 0 deletions changelog.d/688.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support gzip compression
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 django_redis.compressors.base import BaseCompressor
from django_redis.exceptions import CompressorError


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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not available for python 3.6 and 3.7, read here

raise CompressorError from e
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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
Loading