The solution which worked for me is we implemented a custom serializer and adder that in the redis_cache config.
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'
)
if you use the encoding method as 'bytes'
. you can get bytes else you will get a string.
and the below-mentioned line in the cache config
inside the 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.
}
}
}