Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ On Django >= 1.3::
'OPTIONS': {
'DB': 1,
'PASSWORD': 'yadayada',
'PARSER_CLASS': 'redis.connection.HiredisParser'
'PARSER_CLASS': 'redis.connection.HiredisParser',
'PICKLE_VERSION': 2 # optional, default to 0
},
},
}
Expand All @@ -66,7 +67,8 @@ On Django >= 1.3::
'OPTIONS': {
'DB': 1,
'PASSWORD': 'yadayada',
'PARSER_CLASS': 'redis.connection.HiredisParser'
'PARSER_CLASS': 'redis.connection.HiredisParser',
'PICKLE_VERSION': 2 # optional, default to 0
},
},
}
Expand Down
23 changes: 22 additions & 1 deletion redis_cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def _init(self, server, params):
super(CacheClass, self).__init__(params)
self._server = server
self._params = params
self._pickle_version = None

unix_socket_path = None
if ':' in self.server:
Expand Down Expand Up @@ -142,6 +143,20 @@ def parser_class(self):
raise ImproperlyConfigured("Could not find parser class '%s'" % parser_class)
return parser_class

@property
def pickle_version(self):
"""
Get the pickle version from the settings and save it for future use
"""
if self._pickle_version is None:
_pickle_version = self.options.get('PICKLE_VERSION', 0)
try:
_pickle_version = int(_pickle_version)
except (ValueError, TypeError):
raise ImproperlyConfigured("pickle version value must be an integer")
self._pickle_version = _pickle_version
return self._pickle_version

def __getstate__(self):
return {'params': self._params, 'server': self._server}

Expand Down Expand Up @@ -207,7 +222,7 @@ def set(self, key, value, timeout=None, version=None, client=None):
if int(value) != value:
raise TypeError
except (ValueError, TypeError):
result = self._set(key, pickle.dumps(value), int(timeout), client)
result = self._set(key, self.pickle(value), int(timeout), client)
else:
result = self._set(key, int(value), int(timeout), client)
# result is a boolean
Expand Down Expand Up @@ -241,6 +256,12 @@ def unpickle(self, value):
value = smart_str(value)
return pickle.loads(value)

def pickle(self, value):
"""
Pickle the given value.
"""
return pickle.dumps(value, self.pickle_version)

def get_many(self, keys, version=None):
"""
Retrieve many keys.
Expand Down