Skip to content
Merged
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
7 changes: 4 additions & 3 deletions flask_caching/backends/filesystemcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get(self, key):
pickle_time = pickle.load(f)
expired = pickle_time != 0 and pickle_time < time()
if expired:
os.remove(filename)
self.delete(key)
else:
hit_or_miss = "hit"
result = pickle.load(f)
Expand Down Expand Up @@ -207,6 +207,7 @@ def set(self, key, value, timeout=None, mgmt_element=False):
with os.fdopen(fd, "wb") as f:
pickle.dump(timeout, f, 1)
pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
is_new_file = not os.path.exists(filename)
os.replace(tmp, filename)
os.chmod(filename, self._mode)
except (IOError, OSError) as exc:
Expand All @@ -215,7 +216,7 @@ def set(self, key, value, timeout=None, mgmt_element=False):
result = True
logger.debug("set key %r", key)
# Management elements should not count towards threshold
if not mgmt_element:
if not mgmt_element and is_new_file:
self._update_count(delta=1)
return result

Expand Down Expand Up @@ -244,7 +245,7 @@ def has(self, key):
pickle_time = pickle.load(f)
expired = pickle_time != 0 and pickle_time < time()
if expired:
os.remove(filename)
self.delete(key)
else:
result = True
except FileNotFoundError:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_backend_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ def test_no_threshold(self, make_cache):
nof_cache_files = c.get(c._fs_count_file)
assert nof_cache_files is None

def test_filecount_caching_none(self, make_cache):
c = make_cache()
for i in range(3):
assert c.set("a", None)
assert c.get(c._fs_count_file) == 1

def test_filecount_after_deletion_in_has(self, make_cache):
c = make_cache()
assert c.set("foo", "bar", timeout=0.01)
assert c.get(c._fs_count_file) == 1
time.sleep(0.1)
assert c.has("foo") in (False, 0)
assert c.get(c._fs_count_file) == 0

def test_filecount_after_deletion_in_get(self, make_cache):
c = make_cache()
assert c.set("foo", "bar", timeout=0.01)
assert c.get(c._fs_count_file) == 1
time.sleep(0.1)
assert c.get("foo") is None
assert c.get(c._fs_count_file) == 0

def test_count_file_accuracy(self, c):
assert c.set("foo", "bar")
assert c.set("moo", "car")
Expand Down