[Git][NTPsec/ntpsec][master] Extended Cache() class to allow custom time-to-live

Ian Bruene gitlab at mg.gitlab.com
Mon Nov 20 19:41:34 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
7ed91da7 by Ian Bruene at 2017-11-20T13:39:12-06:00
Extended Cache() class to allow custom time-to-live

Cache() now allows a custom default TTL, as well as per-item custom TTL

- - - - -


2 changed files:

- pylib/util.py
- tests/pylib/test_util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -522,21 +522,21 @@ def monoclock():
 
 class Cache:
     "Simple time-based cache"
-    ttl = 300
-
-    def __init__(self):
+    def __init__(self, defaultTimeout=300):  # 5 min default TTL
+        self.defaultTimeout = defaultTimeout
         self._cache = {}
 
     def get(self, key):
         if key in self._cache:
-            value, settime = self._cache[key]
-            if settime >= monoclock() - self.ttl:
+            value, settime, ttl = self._cache[key]
+            if settime >= monoclock() - ttl:
                 return value
             else:  # key expired, delete it
                 del self._cache[key]
 
-    def set(self, key, value):
-        self._cache[key] = (value, monoclock())
+    def set(self, key, value, customTTL=None):
+        ttl = customTTL if customTTL is not None else self.defaultTimeout
+        self._cache[key] = (value, monoclock(), ttl)
 
 
 # A hack to avoid repeatedly hammering on DNS when ntpmon runs.


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -454,19 +454,36 @@ class TestPylibUtilMethods(unittest.TestCase):
             monodata = [5, 10, 315, 20]
             cls.set("foo", 42)
             cls.set("bar", 23)
-            self.assertEqual(cls._cache, {"foo": (42, 5),
-                                          "bar": (23, 10)})
+            self.assertEqual(cls._cache, {"foo": (42, 5, 300),
+                                          "bar": (23, 10, 300)})
             self.assertEqual(monodata, [315, 20])
             # Test get, expired
             result = cls.get("foo")
             self.assertEqual(result, None)
             self.assertEqual(monodata, [20])
-            self.assertEqual(cls._cache, {"bar": (23, 10)})
+            self.assertEqual(cls._cache, {"bar": (23, 10, 300)})
             # Test get, valid
             result = cls.get("bar")
             self.assertEqual(result, 23)
             self.assertEqual(monodata, [])
-            self.assertEqual(cls._cache, {"bar": (23, 10)})
+            self.assertEqual(cls._cache, {"bar": (23, 10, 300)})
+            # Test set, custom TTL
+            monodata = [0, 0, 11, 15]
+            cls.set("foo", 42, 10)
+            cls.set("bar", 23, 20)
+            self.assertEqual(cls._cache, {"foo": (42, 0, 10),
+                                          "bar": (23, 0, 20)})
+            self.assertEqual(monodata, [11, 15])
+            # Test get, expired, custom TTL
+            result = cls.get("foo")
+            self.assertEqual(result, None)
+            self.assertEqual(monodata, [15])
+            self.assertEqual(cls._cache, {"bar": (23, 0, 20)})
+            # Test get, valid, custom TTL
+            result = cls.get("bar")
+            self.assertEqual(result, 23)
+            self.assertEqual(monodata, [])
+            self.assertEqual(cls._cache, {"bar": (23, 0, 20)})
         finally:
             ntp.util.monoclock = monotemp
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/7ed91da73f64e09ea08acfe44c365b53626295f8

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/7ed91da73f64e09ea08acfe44c365b53626295f8
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20171120/4b9860c3/attachment.html>


More information about the vc mailing list