Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/cygwin-apps/calm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Turney <jon.turney@dronecode.org.uk>2022-12-09 17:35:37 +0300
committerJon Turney <jon.turney@dronecode.org.uk>2022-12-09 18:33:49 +0300
commit56f16534dd1a0a63f3a5069ca13fe5d5aa7df691 (patch)
tree53c8403c5031471925dee216987da8fb1bb4a114
parente9a1fac4b4a36a292516b26ea2f8f44e5fd4614f (diff)
Add a simple wrapper for caching a parsed file until mtime changes
-rw-r--r--calm/utils.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/calm/utils.py b/calm/utils.py
index fb95cc6..9f75813 100644
--- a/calm/utils.py
+++ b/calm/utils.py
@@ -125,3 +125,36 @@ def system(args):
else:
for l in output.decode().splitlines():
logging.info(l)
+
+
+#
+# This provides a simple wrapper around a function which takes a pathname as
+# it's only parameter. The result is cached as long as the mtime of the
+# pathname is unchanged.
+#
+def mtime_cache(user_function):
+ sentinel = object() # unique object used to signal cache misses
+ cache = {}
+
+ def wrapper(key):
+ # make sure path is absolute
+ key = os.path.abspath(key)
+
+ (result, mtime) = cache.get(key, (sentinel, 0))
+
+ new_mtime = os.path.getmtime(key)
+
+ # cache hit
+ if result is not sentinel:
+ # cache valid
+ if new_mtime == mtime:
+ return result
+ else:
+ logging.debug('%s cache invalidated by mtime change' % key)
+
+ # cache miss
+ result = user_function(key)
+ cache[key] = (result, new_mtime)
+ return result
+
+ return wrapper