1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Refs #27480 -- Moved FileBasedCache content writing logic to a method.

This commit is contained in:
Simon Charette 2018-07-08 19:34:04 -04:00 committed by Tim Graham
parent 5bea8d256d
commit 2d75509bcb

View File

@ -13,11 +13,6 @@ from django.core.files import locks
from django.core.files.move import file_move_safe
def _write_content(f, expiry, value):
f.write(pickle.dumps(expiry, pickle.HIGHEST_PROTOCOL))
f.write(zlib.compress(pickle.dumps(value, pickle.HIGHEST_PROTOCOL)))
class FileBasedCache(BaseCache):
cache_suffix = '.djcache'
@ -42,6 +37,11 @@ class FileBasedCache(BaseCache):
pass
return default
def _write_content(self, file, timeout, value):
expiry = self.get_backend_timeout(timeout)
file.write(pickle.dumps(expiry, pickle.HIGHEST_PROTOCOL))
file.write(zlib.compress(pickle.dumps(value, pickle.HIGHEST_PROTOCOL)))
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
self._createdir() # Cache dir can be deleted at any time.
fname = self._key_to_file(key, version)
@ -50,8 +50,7 @@ class FileBasedCache(BaseCache):
renamed = False
try:
with open(fd, 'wb') as f:
expiry = self.get_backend_timeout(timeout)
_write_content(f, expiry, value)
self._write_content(f, timeout, value)
file_move_safe(tmp_path, fname, allow_overwrite=True)
renamed = True
finally:
@ -68,7 +67,7 @@ class FileBasedCache(BaseCache):
else:
previous_value = pickle.loads(zlib.decompress(f.read()))
f.seek(0)
_write_content(f, self.get_backend_timeout(timeout), previous_value)
self._write_content(f, timeout, previous_value)
return True
finally:
locks.unlock(f)