From da7d1501e22f9b9696f8e0dfd28ff3cc45f0bb85 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Fri, 3 Jun 2022 18:18:18 -0700 Subject: [PATCH] markov: Add utility method for last access time update * Chain.__touch() is a new function that updates the last time a markov chain was accessed * Fix a bug that would not reliably update the last access time of the chain during Chain.add() Signed-off-by: Alek Ratzloff --- plugins/markov.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plugins/markov.py b/plugins/markov.py index 3a71d4c..a70ccea 100644 --- a/plugins/markov.py +++ b/plugins/markov.py @@ -44,11 +44,15 @@ class Chain: self.__last_access = 0.0 self.__dirty = False + def __touch(self): + self.__last_access = asyncio.get_running_loop().time() + @property def last_access(self) -> float: return self.__last_access def add(self, text: str): + self.__touch() parts: List[Any] = text.strip().split() if not parts: return @@ -60,7 +64,7 @@ class Chain: self.__dirty = True def get(self, key: str) -> dict[str | None, int]: - self.__last_access = asyncio.get_running_loop().time() + self.__touch() if self.__cache: if key in self.__cache: return self.__cache[key] @@ -72,7 +76,7 @@ class Chain: return self.__cache[key] def set(self, key: str, value: Mapping[str | None, int]): - self.__last_access = asyncio.get_running_loop().time() + self.__touch() if not self.__cache: # Attempt the cache before writing to it self.__load() @@ -80,7 +84,7 @@ class Chain: self.__dirty = True def __load(self): - self.__last_access = asyncio.get_running_loop().time() + self.__touch() if self.__cache: return if not self.path.exists():