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 <alekratz@gmail.com>
This commit is contained in:
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user