2

I am working in a system in which multiple code bases access the same redis instance, So while reading some data written from some other code base, in django framework, i am getting the following error.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/django_redis/cache.py", line 32, in _decorator
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django_redis/cache.py", line 81, in get
    client=client)
  File "/usr/local/lib/python3.5/dist-packages/django_redis/client/default.py", line 210, in get
    return self.decode(value)
  File "/usr/local/lib/python3.5/dist-packages/django_redis/client/default.py", line 318, in decode
    value = self._serializer.loads(value)
  File "/usr/local/lib/python3.5/dist-packages/django_redis/serializers/pickle.py", line 35, in loads
    return pickle.loads(value)
_pickle.UnpicklingError: invalid load key, '{'.

Basically Django is trying to de-serialize(unpickle) the data, though the data is not even serialized(pickeled). Can i turn off this pickling and un-pickling in django-redis

  • you need to share codes regarding pickling and unpickling, without those we can't help you – ruddra Apr 15 '19 at 09:47
  • actually i am not using my own algorithm for the same, I am using django framework, which already have this implemented. – vivek munjal Apr 15 '19 at 10:06

1 Answers1

3

You can control the serialization behavior via the django caches options. Try change the cache serializer to JSONSerializer or DummySerializer.

For example, if you are using the django-redis-cache package, change your cache setting to something like:

CACHES = {
    'default': {
        'BACKEND': ...,
        'LOCATION': ...,
        'OPTIONS': {
            'SERIALIZER_CLASS': 'redis_cache.serializers.JSONSerializer',
            ...
        },
        ...
    }
}

Note that if you are using the django-redis package, the key & value of the option would be a little different:

CACHES = {
    "default": {
        "BACKEND": ...,
        "LOCATION": ...,
        "OPTIONS": {
            ...
            "SERIALIZER": "django_redis.serializers.json.JSONSerializer",
        }
    }
}
ConnectionLost
  • 742
  • 4
  • 14