remove expired cache with config.cfgimpl_reset_cache(True)
authorEmmanuel Garette <egarette@cadoles.com>
Fri, 19 Apr 2013 20:42:33 +0000 (22:42 +0200)
committerEmmanuel Garette <egarette@cadoles.com>
Fri, 19 Apr 2013 20:42:33 +0000 (22:42 +0200)
tiramisu/config.py
tiramisu/setting.py
tiramisu/value.py

index 9543f1d..84e16e4 100644 (file)
@@ -72,8 +72,8 @@ class SubConfig(object):
             return
         self._setattr(name, value)
 
-    def cfgimpl_clean_cache(self):
-        self.cfgimpl_get_context().cfgimpl_clean_cache()
+    def cfgimpl_reset_cache(self):
+        self.cfgimpl_get_context().cfgimpl_reset_cache()
 
     def _setattr(self, name, value, force_permissive=False):
         if '.' in name:
@@ -361,9 +361,9 @@ class Config(SubConfig):
     def _cfgimpl_build_all_paths(self):
         self._cfgimpl_descr.build_cache()
 
-    def cfgimpl_clean_cache(self):
-        self.cfgimpl_get_values().reset_cache()
-        self.cfgimpl_get_settings().reset_cache()
+    def cfgimpl_reset_cache(self, only_expired=False):
+        self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
+        self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
 
     def unwrap_from_path(self, path):
         """convenience method to extract and Option() object from the Config()
@@ -502,11 +502,11 @@ def mandatory_warnings(config):
 
     :returns: generator of mandatory Option's path
     """
-    config.cfgimpl_clean_cache()
+    config.cfgimpl_reset_cache()
     for path in config.cfgimpl_get_description().getpaths(include_groups=True):
         try:
             config._getattr(path, force_properties=('mandatory',))
         except PropertiesOptionError, err:
             if err.proptype == ['mandatory']:
                 yield path
-    config.cfgimpl_clean_cache()
+    config.cfgimpl_reset_cache()
index 38cd487..2f51706 100644 (file)
@@ -181,7 +181,7 @@ class Setting(object):
         if propname not in props:
             props.append(propname)
         self.set_properties(props)
-        self.context.cfgimpl_clean_cache()
+        self.context.cfgimpl_reset_cache()
 
     def disable_property(self, propname):
         "deletes property propname in the Config's properties attribute"
@@ -189,7 +189,7 @@ class Setting(object):
         if propname in props:
             props.remove(propname)
         self.set_properties(props)
-        self.context.cfgimpl_clean_cache()
+        self.context.cfgimpl_reset_cache()
 
     def set_properties(self, properties, opt=None):
         """save properties for specified opt
@@ -209,14 +209,14 @@ class Setting(object):
         if not propname in properties:
             properties.append(propname)
             self.set_properties(properties, opt)
-        self.context.cfgimpl_clean_cache()
+        self.context.cfgimpl_reset_cache()
 
     def del_property(self, propname, opt, is_apply_req=True):
         properties = self.get_properties(opt, is_apply_req)
         if propname in properties:
             properties.remove(propname)
             self.set_properties(properties, opt)
-        self.context.cfgimpl_clean_cache()
+        self.context.cfgimpl_reset_cache()
 
     def _validate_mandatory(self, opt, value, force_properties=None):
         set_mandatory = self.has_property('mandatory')
@@ -248,9 +248,9 @@ class Setting(object):
                             force_properties=None):
         is_cached = False
         if opt_or_descr in self._cache:
-            t = time()
+            exp = time()
             props, raise_text, created = self._cache[opt_or_descr]
-            if t < created:
+            if exp < created:
                 properties = props
                 is_cached = True
         if not is_cached:
@@ -320,8 +320,16 @@ class Setting(object):
         if self.has_property('expire'):
             self._cache[opt] = (props, raise_text, time() + expires_time)
 
-    def reset_cache(self):
-        self._cache = {}
+    def reset_cache(self, only_expired):
+        if only_expired:
+            exp = time()
+            keys = self._cache.keys()
+            for key in keys:
+                props, raise_text, created = self._cache[key]
+                if exp > created:
+                    del(self._cache[key])
+        else:
+            self._cache.clear()
 
 
 def apply_requires(opt, config):
index e35cbcb..5b0aeae 100644 (file)
@@ -56,7 +56,7 @@ class Values(object):
 
     def _reset(self, opt):
         if opt in self._values:
-            self.context.cfgimpl_clean_cache()
+            self.context.cfgimpl_reset_cache()
             del(self._values[opt])
 
     def _is_empty(self, opt, value):
@@ -84,9 +84,9 @@ class Values(object):
     def getitem(self, opt, validate=True, force_permissive=False,
                 force_properties=None):
         if opt in self._cache:
-            t = time()
+            exp = time()
             value, created = self._cache[opt]
-            if t < created:
+            if exp < created:
                 return value
         val = self._getitem(opt, validate, force_permissive, force_properties)
         self._set_cache(opt, val)
@@ -145,7 +145,7 @@ class Values(object):
                        force_properties=force_properties)
 
     def _setvalue(self, opt, value, force_permissive=False, force_properties=None):
-        self.context.cfgimpl_clean_cache()
+        self.context.cfgimpl_reset_cache()
         setting = self.context.cfgimpl_get_settings()
         setting.validate_properties(opt, False, True,
                                     value=value,
@@ -175,8 +175,16 @@ class Values(object):
         if self.context.cfgimpl_get_settings().has_property('expire'):
             self._cache[opt] = (val, time() + expires_time)
 
-    def reset_cache(self):
-        self._cache = {}
+    def reset_cache(self, only_expired):
+        if only_expired:
+            exp = time()
+            keys = self._cache.keys()
+            for key in keys:
+                val, created = self._cache[key]
+                if exp > created:
+                    del(self._cache[key])
+        else:
+            self._cache.clear()
 
     def __contains__(self, opt):
         return opt in self._values