From 2d75509bcb1db910da3c568f99b6ac778a79a98f Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 8 Jul 2018 19:34:04 -0400 Subject: [PATCH] Refs #27480 -- Moved FileBasedCache content writing logic to a method. --- django/core/cache/backends/filebased.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py index 287d6472a5..4ea7bca050 100644 --- a/django/core/cache/backends/filebased.py +++ b/django/core/cache/backends/filebased.py @@ -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)