Dirk Alders 2 months ago
parent
commit
ed1082041f
4 changed files with 13369 additions and 2860 deletions
  1. 24
    22
      __init__.py
  2. BIN
      _requirements_/specification.pdf
  3. 13345
    2838
      _testresults_/unittest.json
  4. BIN
      _testresults_/unittest.pdf

+ 24
- 22
__init__.py View File

54
                 * **keys():** returns a list of all available keys.
54
                 * **keys():** returns a list of all available keys.
55
                 * **data_version():** returns a version number of the current data (it should be increased, if the get method of the source instance returns improved values or the data structure had been changed).
55
                 * **data_version():** returns a version number of the current data (it should be increased, if the get method of the source instance returns improved values or the data structure had been changed).
56
                 * **get(key, default):** returns the property for a key. If key does not exists, default will be returned.
56
                 * **get(key, default):** returns the property for a key. If key does not exists, default will be returned.
57
-    
57
+
58
     :param source_instance: The source instance holding the data
58
     :param source_instance: The source instance holding the data
59
     :type source_instance: instance
59
     :type source_instance: instance
60
     :param cache_filename: File name, where the properties are stored as cache
60
     :param cache_filename: File name, where the properties are stored as cache
97
     #
97
     #
98
     STORAGE_VERSION = 1
98
     STORAGE_VERSION = 1
99
 
99
 
100
-    def __init__(self, source_instance, cache_filename, load_all_on_init=False, callback_on_data_storage=None, max_age=None, store_on_get=True):
100
+    def __init__(self, source_instance, cache_filename, load_all_on_init=False, callback_on_data_storage=None, max_age=None, store_on_get=True, return_source_on_none=False):
101
         self._source_instance = source_instance
101
         self._source_instance = source_instance
102
         self._cache_filename = cache_filename
102
         self._cache_filename = cache_filename
103
         self._load_all_on_init = load_all_on_init
103
         self._load_all_on_init = load_all_on_init
104
         self._callback_on_data_storage = callback_on_data_storage
104
         self._callback_on_data_storage = callback_on_data_storage
105
         self._max_age = max_age
105
         self._max_age = max_age
106
         self._store_on_get = store_on_get
106
         self._store_on_get = store_on_get
107
+        self._return_source_on_none = return_source_on_none
107
         #
108
         #
108
         self._source_get_keys = []
109
         self._source_get_keys = []
109
         self._cached_props = None
110
         self._cached_props = None
163
                     return val
164
                     return val
164
             else:
165
             else:
165
                 logger.debug("Providing property for '%s' from cache", key)
166
                 logger.debug("Providing property for '%s' from cache", key)
166
-            return self._cached_props[self.DATA_TAG].get(self._key_filter(key), default)
167
+            cached_data = self._cached_props[self.DATA_TAG].get(self._key_filter(key), default)
168
+            if cached_data is None and self._return_source_on_none:
169
+                return self._source_instance.get(key, default)
170
+            return cached_data
167
         else:
171
         else:
168
             if key not in self._source_instance.keys():
172
             if key not in self._source_instance.keys():
169
                 logger.debug("Key '%s' is not in cached_keys. Uncached data will be returned.", key)
173
                 logger.debug("Key '%s' is not in cached_keys. Uncached data will be returned.", key)
170
-            elif key in self._source_get_keys:
174
+            else:
171
                 logger.debug("Key '%s' is excluded by .add_source_get_keys(). Uncached data will be returned.", key)
175
                 logger.debug("Key '%s' is excluded by .add_source_get_keys(). Uncached data will be returned.", key)
172
             return self._source_instance.get(key, default)
176
             return self._source_instance.get(key, default)
173
 
177
 
209
             self._cached_props[self.UID_TAG] = self._source_instance.uid()
213
             self._cached_props[self.UID_TAG] = self._source_instance.uid()
210
             self._cached_props[self.DATA_VERSION_TAG] = self._source_instance.data_version()
214
             self._cached_props[self.DATA_VERSION_TAG] = self._source_instance.data_version()
211
             self._cached_props[self.STORAGE_VERSION_TAG] = self.STORAGE_VERSION
215
             self._cached_props[self.STORAGE_VERSION_TAG] = self.STORAGE_VERSION
212
-            if self._store_on_get:
213
-                self._save_cache()
216
+
217
+    def _load_only(self):
218
+        with open(self._cache_filename, 'rb') as fh:
219
+            self._cached_props = pickle.load(fh)
220
+        logger.debug('Loading properties from cache (%s)', self._cache_filename)
214
 
221
 
215
     def _load_cache(self):
222
     def _load_cache(self):
216
         if os.path.exists(self._cache_filename):
223
         if os.path.exists(self._cache_filename):
217
-            with open(self._cache_filename, 'rb') as fh:
218
-                self._cached_props = pickle.load(fh)
219
-            logger.debug('Loading properties from cache (%s)', self._cache_filename)
224
+            self._load_only()
220
             return True
225
             return True
221
         else:
226
         else:
222
             logger.debug('Cache file does not exists (yet).')
227
             logger.debug('Cache file does not exists (yet).')
235
                 self._cached_props[self.AGE_TAG][self._key_filter(key)] = int(time.time())
240
                 self._cached_props[self.AGE_TAG][self._key_filter(key)] = int(time.time())
236
                 time.sleep(sleep_between_keys)
241
                 time.sleep(sleep_between_keys)
237
 
242
 
238
-    def _save_cache(self):
243
+    def _save_only(self):
239
         with open(self._cache_filename, 'wb') as fh:
244
         with open(self._cache_filename, 'wb') as fh:
240
             pickle.dump(self._cached_props, fh)
245
             pickle.dump(self._cached_props, fh)
241
             logger.debug('cache-file stored (%s)', self._cache_filename)
246
             logger.debug('cache-file stored (%s)', self._cache_filename)
247
+
248
+    def _save_cache(self):
249
+        self._save_only()
242
         if self._callback_on_data_storage is not None:
250
         if self._callback_on_data_storage is not None:
243
             self._callback_on_data_storage(self)
251
             self._callback_on_data_storage(self)
244
 
252
 
271
 
279
 
272
     .. literalinclude:: caching/_examples_/property_cache_json_2.log
280
     .. literalinclude:: caching/_examples_/property_cache_json_2.log
273
     """
281
     """
274
-    def _load_cache(self):
275
-        if os.path.exists(self._cache_filename):
276
-            with open(self._cache_filename, 'r') as fh:
277
-                self._cached_props = json.load(fh)
278
-            logger.debug('Loading properties from cache (%s)', self._cache_filename)
279
-            return True
280
-        else:
281
-            logger.debug('Cache file does not exists (yet).')
282
-        return False
283
 
282
 
284
-    def _save_cache(self):
283
+    def _load_only(self):
284
+        with open(self._cache_filename, 'r') as fh:
285
+            self._cached_props = json.load(fh)
286
+        logger.debug('Loading properties from cache (%s)', self._cache_filename)
287
+
288
+    def _save_only(self):
285
         with open(self._cache_filename, 'w') as fh:
289
         with open(self._cache_filename, 'w') as fh:
286
             json.dump(self._cached_props, fh, sort_keys=True, indent=4)
290
             json.dump(self._cached_props, fh, sort_keys=True, indent=4)
287
             logger.debug('cache-file stored (%s)', self._cache_filename)
291
             logger.debug('cache-file stored (%s)', self._cache_filename)
288
-        if self._callback_on_data_storage is not None:
289
-            self._callback_on_data_storage(self)

BIN
_requirements_/specification.pdf View File


+ 13345
- 2838
_testresults_/unittest.json
File diff suppressed because it is too large
View File


BIN
_testresults_/unittest.pdf View File


Loading…
Cancel
Save