Dirk Alders il y a 2 semaines
Parent
révision
ad5b0acc73
1 fichiers modifiés avec 27 ajouts et 21 suppressions
  1. 27
    21
      __init__.py

+ 27
- 21
__init__.py Voir le fichier

@@ -142,28 +142,34 @@ class property_cache_pickle(object):
142 142
         :param default: value to be returned, if key does not exists.
143 143
         :returns: value for a given key or default value.
144 144
         """
145
-        if key in self._source_instance.keys() and key not in self._source_get_keys:
146
-            if self._cached_props is None:
147
-                self._init_cache()
148
-            if self._max_age is None:
149
-                cache_old = False
150
-            else:
151
-                cache_old = time.time() - self._cached_props[self.AGE_TAG].get(self._key_filter(key), 0) > self._max_age
152
-                if cache_old:
153
-                    logger.debug("The cached value is old, cached value will be ignored")
154
-            if self._key_filter(key) not in self._cached_props[self.DATA_TAG] or cache_old:
155
-                logger.debug("Loading property for key='%s' from source instance", key)
156
-                val = self._source_instance.get(key, None)
157
-                if self._store_on_get:
158
-                    tm = int(time.time())
159
-                    logger.debug("Adding key=%s, value=%s with timestamp=%d to chache", key, val, tm)
160
-                    self._cached_props[self.DATA_TAG][self._key_filter(key)] = val
161
-                    self._cached_props[self.AGE_TAG][self._key_filter(key)] = tm
162
-                    self._save_cache()
163
-                else:
164
-                    return val
145
+        # Init cache
146
+        if self._cached_props is None:
147
+            self._init_cache()
148
+        # Identify old cache
149
+        if self._max_age is None:
150
+            cache_old = False
151
+        else:
152
+            cache_old = time.time() - self._cached_props[self.AGE_TAG].get(self._key_filter(key), 0) > self._max_age
153
+            if cache_old:
154
+                logger.debug("The cached value is old, cached value will be ignored")
155
+        # Return cached value
156
+        if not cache_old and key not in self._source_get_keys and self._key_filter(key) in self._cached_props[self.DATA_TAG]:
157
+            logger.debug("Providing property for '%s' from cache", key)
158
+            rv = self._cached_props[self.DATA_TAG].get(self._key_filter(key), default)
159
+            if rv is not None or not self._return_source_on_none:
160
+                return rv
161
+        # Create cache and return value
162
+        if key in self._source_instance.keys():
163
+            logger.debug("Loading property for key='%s' from source instance", key)
164
+            val = self._source_instance.get(key, None)
165
+            if self._store_on_get:
166
+                tm = int(time.time())
167
+                logger.debug("Adding key=%s, value=%s with timestamp=%d to chache", key, val, tm)
168
+                self._cached_props[self.DATA_TAG][self._key_filter(key)] = val
169
+                self._cached_props[self.AGE_TAG][self._key_filter(key)] = tm
170
+                self._save_cache()
165 171
             else:
166
-                logger.debug("Providing property for '%s' from cache", key)
172
+                return val
167 173
             cached_data = self._cached_props[self.DATA_TAG].get(self._key_filter(key), default)
168 174
             if cached_data is None and self._return_source_on_none:
169 175
                 return self._source_instance.get(key, default)

Chargement…
Annuler
Enregistrer