From 76800df03b23125b63a2d43b87ad9551e142a436 Mon Sep 17 00:00:00 2001 From: Am K Date: Sat, 28 Sep 2024 10:00:54 +0700 Subject: [PATCH] fix FcntlStorage --- zcache/Storage/FcntlStorage.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/zcache/Storage/FcntlStorage.py b/zcache/Storage/FcntlStorage.py index 91c1eb1..9d269ee 100644 --- a/zcache/Storage/FcntlStorage.py +++ b/zcache/Storage/FcntlStorage.py @@ -31,12 +31,13 @@ class FileLock: - def __init__(self, filename): + def __init__(self, filename, mode): self.filename = filename self.file_handle = None + self.mode = mode def __enter__(self): - self.file_handle = open(self.filename, "a+") + self.file_handle = open(self.filename, self.mode) fcntl.flock(self.file_handle, fcntl.LOCK_EX) self.file_handle.seek(0) return self.file_handle @@ -53,9 +54,9 @@ class FcntlStorage(Storage): def __init__(self, path): if not isinstance(path, str): raise TypeError + self.path = path if not os.path.exists(path): self.create(path) - self.path = path def create(self, path): data = {} @@ -64,14 +65,13 @@ def create(self, path): data["url"] = "https://github.com/guangrei/zcache" data["data"] = {} data["limit"] = 0 - with FileLock(path) as f: - f.write(json.dumps(data)) + self.save(data) def load(self): - with FileLock(self.path) as f: + with FileLock(self.path, mode="r") as f: return json.loads(f.read()) def save(self, data): data = json.dumps(data) - with FileLock(self.path) as f: + with FileLock(self.path, mode="w") as f: f.write(data)