Решение, которое сработало для меня, заключается в том, что мы реализовали собственный сериализатор и сумматор, которые в конфигурации redis_cache.
import six
from django.utils.encoding import force_bytes
from django_redis.serializers.pickle import PickleSerializer
try:
import cPickle as pickle
except ImportError:
import pickle
class CcustomPickleSerializer(PickleSerializer):
def loads(self, value):
if six.PY3:
return self._loads_py3(value)
return super().loads(force_bytes(value))
def _loads_py3(self, value):
return pickle.loads(
force_bytes(value),
fix_imports=True,
encoding='latin1'
)
, если вы используете метод кодирования как 'bytes'
. Вы можете получить байты, иначе вы получите строку.
и нижеупомянутую строку в cache config
внутри settings.py
.
'SERIALIZER':'file.location.CustomPickleSerializer'
in.
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/0',
'OPTIONS': {
'KEY_PREFIX': 'personify',
'SERIALIZER':'file.location.CustomPickleSerializer',
'PARSER_CLASS': 'redis.connection.HiredisParser', # Hiredis is WAY faster than redis-py
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'CONNECTION_POOL_KWARGS': {
'max_connections': 100
},
'PICKLE_VERSION': 2, # Make sure we're using v2 of pickle, which is pretty efficient.
'SOCKET_CONNECT_TIMEOUT': 5, # Seconds (timeout for the connection to be established).
'SOCKET_TIMEOUT': 5, # Seconds (timeout for read and write operations after the connection is established).
'IGNORE_EXCEPTIONS': False # Set to True to act like memcached - i.e. don't raise errors.
}
}
}