From bee166e45805f1e11abb98516e232f3a4aa11f25 Mon Sep 17 00:00:00 2001 From: amirreza Date: Wed, 12 Nov 2025 17:12:35 +0330 Subject: [PATCH] use stdlib `zstd` library instead of `pyzstd` --- CHANGES.md | 2 ++ README.rst | 2 +- django_valkey/compressors/zstd.py | 20 +++++++++++++------- docs/configure/compressors.md | 15 ++++++++++----- pyproject.toml | 5 ++++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e39d16f..3d53d3e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,9 +1,11 @@ + ### Breaking changes - `BackendCommands` and `AsyncBackendCommands` are no longer decorated with `omit_exception`. - added `omit_exception_async` to decorate async operations, instead of using `omit_exception` for both sync and async. - `omit_exception` no longer supports async functions and generators. - added `DecoratedBackendCommands` and `DecoratedAsyncBackendCommands` as commands decorated with `omit_exception` and `omit_exception_async`. - `AsyncValkeyCache` and `ValkeyCache` no longer inherit from `BackendCommands` and `AsyncBackendCommands`, they inherit from `DecoratedBackendCommands` and `DecoratedAsyncBackendCommands` instead. +- stdlib `zstd` is used instead of `pyzstd` (`zstd` is the same as `pyzstd`, but since the package name are different this is counted is breaking change) ### improvement - removed the undecorator loop from cluster client diff --git a/README.rst b/README.rst index e7930e6..325fc38 100644 --- a/README.rst +++ b/README.rst @@ -91,7 +91,7 @@ Install 3rd party compression .. code-block:: console - python -m pip install django-valkey[pyzstd] + python -m pip install django-valkey[zstd] # not need since python 3.14 .. code-block:: console diff --git a/django_valkey/compressors/zstd.py b/django_valkey/compressors/zstd.py index 824f926..a320d6f 100644 --- a/django_valkey/compressors/zstd.py +++ b/django_valkey/compressors/zstd.py @@ -1,9 +1,13 @@ -import pyzstd - +import sys from django.conf import settings from django_valkey.compressors.base import BaseCompressor +if sys.version_info >= (3, 14): + from compression import zstd +else: + from backports import zstd + class ZStdCompressor(BaseCompressor): """ @@ -19,7 +23,8 @@ class ZStdCompressor(BaseCompressor): } compression parameters: - to set `level_or_option` use either `CACHE_COMPRESS_LEVEL` or `COMPRESS_ZSTD_OPTIONS` in your settings. + to set `level` use `CACHE_COMPRESS_LEVEL` + to set `options` `COMPRESS_ZSTD_OPTIONS` in your settings. if `COMPRESSION_ZSTD_OPTIONS` is set, level won't be used to set `minimum size` set `CACHE_COMPRESS_MIN_LENGTH` in your settings, defaults to 15. @@ -41,15 +46,16 @@ class ZStdCompressor(BaseCompressor): decomp_zstd_dict = getattr(settings, "DECOMPRESS_ZSTD_DICT", None) def _compress(self, value: bytes) -> bytes: - return pyzstd.compress( + return zstd.compress( value, - level_or_option=self.options or self.level or 1, + options=self.options, + level=self.level or 1, zstd_dict=self.zstd_dict, ) def _decompress(self, value: bytes) -> bytes: - return pyzstd.decompress( + return zstd.decompress( value, zstd_dict=self.decomp_zstd_dict or self.zstd_dict, - option=self.decomp_options or self.options, + options=self.decomp_options or self.options, ) diff --git a/docs/configure/compressors.md b/docs/configure/compressors.md index 0111f38..eaf90d4 100644 --- a/docs/configure/compressors.md +++ b/docs/configure/compressors.md @@ -182,16 +182,21 @@ compress_zlib_wbits = 15 # defaults to 15 (NOTE: only available in python 3.11 ### Zstd compression -to use zstd compression you need to have the pyzstd library installed +as of `django-valkey` 0.4.0, the zstd library from python stdlib is used instead of `pyzstd`. + +if you are using python 3.14 or above, you don't need to install anything, +otherwise, you need to install the `backports.zstd` library: ```shell -pip install django-valkey[pyzstd] +pip install django-valkey[zstd] +# or +pip install django-valkey[pyzstd] # this is for backwards compatibility ``` -or simply +or ```shell -pip install pyzstd +pip install backports.zstd ``` then you can configure it as such: @@ -216,4 +221,4 @@ COMPRESS_ZSTD_DICT = {...} DECOMPRESS_ZSTD_DICT = {...} # note: if you don't set this, the above one will be used. ``` -*NOTE* the values shown here are only examples and *not* best practice or anything. \ No newline at end of file +*NOTE* the values shown here are only examples and *not* best practice or anything. diff --git a/pyproject.toml b/pyproject.toml index d5998ab..b6f74ae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,7 +49,10 @@ lz4 = [ "lz4>=4.3.3", ] pyzstd = [ - "pyzstd>=0.16.2", + "backports.zstd>=1.0.0", +] +zstd = [ + "backports.zstd>=1.0.0", ] msgpack = [ "msgpack>=1.1.0",