Dirk Alders hace 2 meses
padre
commit
ed1082041f
Se han modificado 4 ficheros con 13369 adiciones y 2860 borrados
  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 Ver fichero

@@ -54,7 +54,7 @@ class property_cache_pickle(object):
54 54
                 * **keys():** returns a list of all available keys.
55 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 56
                 * **get(key, default):** returns the property for a key. If key does not exists, default will be returned.
57
-    
57
+
58 58
     :param source_instance: The source instance holding the data
59 59
     :type source_instance: instance
60 60
     :param cache_filename: File name, where the properties are stored as cache
@@ -97,13 +97,14 @@ class property_cache_pickle(object):
97 97
     #
98 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 101
         self._source_instance = source_instance
102 102
         self._cache_filename = cache_filename
103 103
         self._load_all_on_init = load_all_on_init
104 104
         self._callback_on_data_storage = callback_on_data_storage
105 105
         self._max_age = max_age
106 106
         self._store_on_get = store_on_get
107
+        self._return_source_on_none = return_source_on_none
107 108
         #
108 109
         self._source_get_keys = []
109 110
         self._cached_props = None
@@ -163,11 +164,14 @@ class property_cache_pickle(object):
163 164
                     return val
164 165
             else:
165 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 171
         else:
168 172
             if key not in self._source_instance.keys():
169 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 175
                 logger.debug("Key '%s' is excluded by .add_source_get_keys(). Uncached data will be returned.", key)
172 176
             return self._source_instance.get(key, default)
173 177
 
@@ -209,14 +213,15 @@ class property_cache_pickle(object):
209 213
             self._cached_props[self.UID_TAG] = self._source_instance.uid()
210 214
             self._cached_props[self.DATA_VERSION_TAG] = self._source_instance.data_version()
211 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 222
     def _load_cache(self):
216 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 225
             return True
221 226
         else:
222 227
             logger.debug('Cache file does not exists (yet).')
@@ -235,10 +240,13 @@ class property_cache_pickle(object):
235 240
                 self._cached_props[self.AGE_TAG][self._key_filter(key)] = int(time.time())
236 241
                 time.sleep(sleep_between_keys)
237 242
 
238
-    def _save_cache(self):
243
+    def _save_only(self):
239 244
         with open(self._cache_filename, 'wb') as fh:
240 245
             pickle.dump(self._cached_props, fh)
241 246
             logger.debug('cache-file stored (%s)', self._cache_filename)
247
+
248
+    def _save_cache(self):
249
+        self._save_only()
242 250
         if self._callback_on_data_storage is not None:
243 251
             self._callback_on_data_storage(self)
244 252
 
@@ -271,19 +279,13 @@ class property_cache_json(property_cache_pickle):
271 279
 
272 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 289
         with open(self._cache_filename, 'w') as fh:
286 290
             json.dump(self._cached_props, fh, sort_keys=True, indent=4)
287 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 Ver fichero


+ 13345
- 2838
_testresults_/unittest.json
La diferencia del archivo ha sido suprimido porque es demasiado grande
Ver fichero


BIN
_testresults_/unittest.pdf Ver fichero


Loading…
Cancelar
Guardar