瀏覽代碼

store on get implemented

master
Dirk Alders 3 月之前
父節點
當前提交
06cf512f8f
共有 1 個檔案被更改,包括 17 行新增11 行删除
  1. 17
    11
      __init__.py

+ 17
- 11
__init__.py 查看文件

@@ -87,13 +87,13 @@ class property_cache_pickle(object):
87 87
     #
88 88
     STORAGE_VERSION = 1
89 89
 
90
-    def __init__(self, source_instance, cache_filename, load_all_on_init=False, callback_on_data_storage=None, max_age=None, store_none_value=False):
90
+    def __init__(self, source_instance, cache_filename, load_all_on_init=False, callback_on_data_storage=None, max_age=None, store_on_get=True):
91 91
         self._source_instance = source_instance
92 92
         self._cache_filename = cache_filename
93 93
         self._load_all_on_init = load_all_on_init
94 94
         self._callback_on_data_storage = callback_on_data_storage
95 95
         self._max_age = max_age
96
-        self._store_none_value = store_none_value
96
+        self._store_on_get = store_on_get
97 97
         self._cached_props = None
98 98
 
99 99
     def get(self, key, default=None):
@@ -116,7 +116,7 @@ class property_cache_pickle(object):
116 116
             if self._key_filter(key) not in self._cached_props[self.DATA_TAG] or cache_old:
117 117
                 val = self._source_instance.get(key, None)
118 118
                 logger.debug("%s Loading property for '%s' from source instance (%s)", self.LOG_PREFIX, key, repr(val))
119
-                if val or self._store_none_value:
119
+                if self._store_on_get:
120 120
                     tm = int(time.time())
121 121
                     logger.debug("Storing value=%s with timestamp=%d to chache", val, tm)
122 122
                     self._cached_props[self.DATA_TAG][self._key_filter(key)] = val
@@ -132,6 +132,10 @@ class property_cache_pickle(object):
132 132
             logger.info("%s Key '%s' is not in cached_keys. Uncached data will be returned.", self.LOG_PREFIX, key)
133 133
             return self._source_instance.get(key, default)
134 134
 
135
+    def full_update(self):
136
+        self._load_source()
137
+        self._save_cache()
138
+
135 139
     def keys(self):
136 140
         """
137 141
         Method to get the available keys (from :data:`source_instance`).
@@ -163,12 +167,13 @@ class property_cache_pickle(object):
163 167
             storage_version = True
164 168
         #
165 169
         if not load_cache or uid or data_version or storage_version:
166
-            if self._uid() is not None and uid:
167
-                logger.debug("%s Source uid changed, ignoring previous cache data", self.LOG_PREFIX)
168
-            if self._data_version() is not None and data_version:
169
-                logger.debug("%s Data version increased, ignoring previous cache data", self.LOG_PREFIX)
170
-            if storage_version:
171
-                logger.debug("%s Storage version changed, ignoring previous cache data", self.LOG_PREFIX)
170
+            if load_cache:
171
+                if self._uid() is not None and uid:
172
+                    logger.debug("%s Source uid changed, ignoring previous cache data", self.LOG_PREFIX)
173
+                if self._data_version() is not None and data_version:
174
+                    logger.debug("%s Data version increased, ignoring previous cache data", self.LOG_PREFIX)
175
+                if storage_version:
176
+                    logger.debug("%s Storage version changed, ignoring previous cache data", self.LOG_PREFIX)
172 177
             self._cached_props = {self.AGE_TAG: {}, self.DATA_TAG: {}}
173 178
             if self._load_all_on_init:
174 179
                 self._load_source()
@@ -191,10 +196,11 @@ class property_cache_pickle(object):
191 196
         return key
192 197
 
193 198
     def _load_source(self):
199
+        if self._cached_props is None:
200
+            self._init_cache()
194 201
         logger.debug('%s Loading all data from source - %s', self.LOG_PREFIX, repr(self._source_instance.keys()))
195 202
         for key in self._source_instance.keys():
196
-            val = self._source_instance.get(key)
197
-            self._cached_props[self.DATA_TAG][self._key_filter(key)] = val
203
+            self._cached_props[self.DATA_TAG][self._key_filter(key)] = self._source_instance.get(key)
198 204
             self._cached_props[self.AGE_TAG][self._key_filter(key)] = int(time.time())
199 205
 
200 206
     def _save_cache(self):

Loading…
取消
儲存