From 090c67201cc7a50cc6f3f3d61569de2aaa9d3db6 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 30 Jun 2019 16:18:40 +0100 Subject: [PATCH] Refresh the actor cache --- cache.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cache.py b/cache.py index 9558817d..d4b89a75 100644 --- a/cache.py +++ b/cache.py @@ -6,6 +6,8 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" +import datetime + # cache of actor json # If there are repeated lookups then this helps prevent a lot # of needless network traffic @@ -17,7 +19,8 @@ cachedWebfingers = {} def storePersonInCache(personUrl: str,personJson) -> None: """Store an actor in the cache """ - personCache[personUrl]=personJson + currTime=datetime.datetime.utcnow() + personCache[personUrl]={ "actor": personJson, "timestamp": currTime.strftime("%Y-%m-%dT%H:%M:%SZ") } def storeWebfingerInCache(handle: str,wf) -> None: """Store a webfinger endpoint in the cache @@ -28,7 +31,11 @@ def getPersonFromCache(personUrl: str): """Get an actor from the cache """ if personCache.get(personUrl): - return personCache[personUrl] + currTime=datetime.datetime.utcnow() + cacheTime=datetime.strptime(personCache[personUrl]['timestamp'].replace('T',' ')) + daysSinceCached=(currTime - cacheTime).days + if daysSinceCached <= 2: + return personCache[personUrl]['actor'] return None def getWebfingerFromCache(handle: str):