-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathbless_cache.py
More file actions
57 lines (49 loc) · 1.74 KB
/
Copy pathbless_cache.py
File metadata and controls
57 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import absolute_import
import json
import logging
import os
class BlessCache(object):
CACHEMODE_DISABLED = 'disabled'
CACHEMODE_RECACHE = 'recache'
CACHEMODE_ENABLED = 'enabled'
def __init__(self, filepath, filename, cachemode=CACHEMODE_ENABLED):
self.filepath = filepath
self.filename = filename
self.mode = cachemode
self.cache = None
self.dirty = False
def get(self, key):
if self.mode != self.CACHEMODE_ENABLED:
logging.debug("Cache get disabled")
return None
value = None
if self.cache is None:
self.loadCache()
if key in self.cache.keys():
value = self.cache[key]
return value
def set(self, key, value):
if self.cache is None:
self.loadCache()
self.dirty = True
self.cache[key] = value
def save(self):
if self.dirty and self.mode != self.CACHEMODE_DISABLED:
self.saveCache()
def loadCache(self):
self.cache = {}
cache_file_path = os.path.join(self.filepath, self.filename)
if os.path.isfile(cache_file_path):
with open(cache_file_path, 'r') as cache:
try:
self.cache = json.load(cache)
except Exception:
logging.error("Corrupted cache, using empty cache")
logging.debug("Cache loaded: {}".format(self.cache))
def saveCache(self):
if not os.path.exists(self.filepath):
os.makedirs(self.filepath)
cache_file_path = os.path.join(self.filepath, self.filename)
with open(cache_file_path, 'w') as cache:
json.dump(self.cache, cache)
logging.debug("Cache saved")