Updates
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,9 @@
|
||||
class BaseCompressor:
|
||||
def __init__(self, options):
|
||||
self._options = options
|
||||
|
||||
def compress(self, value: bytes) -> bytes:
|
||||
raise NotImplementedError
|
||||
|
||||
def decompress(self, value: bytes) -> bytes:
|
||||
raise NotImplementedError
|
||||
@@ -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:
|
||||
raise CompressorError from e
|
||||
@@ -0,0 +1,9 @@
|
||||
from django_redis.compressors.base import BaseCompressor
|
||||
|
||||
|
||||
class IdentityCompressor(BaseCompressor):
|
||||
def compress(self, value: bytes) -> bytes:
|
||||
return value
|
||||
|
||||
def decompress(self, value: bytes) -> bytes:
|
||||
return value
|
||||
@@ -0,0 +1,20 @@
|
||||
from lz4.frame import compress as _compress
|
||||
from lz4.frame import decompress as _decompress
|
||||
|
||||
from django_redis.compressors.base import BaseCompressor
|
||||
from django_redis.exceptions import CompressorError
|
||||
|
||||
|
||||
class Lz4Compressor(BaseCompressor):
|
||||
min_length = 15
|
||||
|
||||
def compress(self, value: bytes) -> bytes:
|
||||
if len(value) > self.min_length:
|
||||
return _compress(value)
|
||||
return value
|
||||
|
||||
def decompress(self, value: bytes) -> bytes:
|
||||
try:
|
||||
return _decompress(value)
|
||||
except Exception as e:
|
||||
raise CompressorError from e
|
||||
@@ -0,0 +1,20 @@
|
||||
import lzma
|
||||
|
||||
from django_redis.compressors.base import BaseCompressor
|
||||
from django_redis.exceptions import CompressorError
|
||||
|
||||
|
||||
class LzmaCompressor(BaseCompressor):
|
||||
min_length = 100
|
||||
preset = 4
|
||||
|
||||
def compress(self, value: bytes) -> bytes:
|
||||
if len(value) > self.min_length:
|
||||
return lzma.compress(value, preset=self.preset)
|
||||
return value
|
||||
|
||||
def decompress(self, value: bytes) -> bytes:
|
||||
try:
|
||||
return lzma.decompress(value)
|
||||
except lzma.LZMAError as e:
|
||||
raise CompressorError from e
|
||||
@@ -0,0 +1,20 @@
|
||||
import zlib
|
||||
|
||||
from django_redis.compressors.base import BaseCompressor
|
||||
from django_redis.exceptions import CompressorError
|
||||
|
||||
|
||||
class ZlibCompressor(BaseCompressor):
|
||||
min_length = 15
|
||||
preset = 6
|
||||
|
||||
def compress(self, value: bytes) -> bytes:
|
||||
if len(value) > self.min_length:
|
||||
return zlib.compress(value, self.preset)
|
||||
return value
|
||||
|
||||
def decompress(self, value: bytes) -> bytes:
|
||||
try:
|
||||
return zlib.decompress(value)
|
||||
except zlib.error as e:
|
||||
raise CompressorError from e
|
||||
@@ -0,0 +1,19 @@
|
||||
import pyzstd
|
||||
|
||||
from django_redis.compressors.base import BaseCompressor
|
||||
from django_redis.exceptions import CompressorError
|
||||
|
||||
|
||||
class ZStdCompressor(BaseCompressor):
|
||||
min_length = 15
|
||||
|
||||
def compress(self, value: bytes) -> bytes:
|
||||
if len(value) > self.min_length:
|
||||
return pyzstd.compress(value)
|
||||
return value
|
||||
|
||||
def decompress(self, value: bytes) -> bytes:
|
||||
try:
|
||||
return pyzstd.decompress(value)
|
||||
except pyzstd.ZstdError as e:
|
||||
raise CompressorError from e
|
||||
Reference in New Issue
Block a user