epicyon/cache.py

49 lines
1.4 KiB
Python
Raw Normal View History

2019-06-30 15:03:26 +00:00
__filename__ = "cache.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "0.0.1"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-06-30 15:18:40 +00:00
import datetime
2019-06-30 15:03:26 +00:00
# cache of actor json
# If there are repeated lookups then this helps prevent a lot
# of needless network traffic
personCache = {}
# cached webfinger endpoints
cachedWebfingers = {}
def storePersonInCache(personUrl: str,personJson) -> None:
"""Store an actor in the cache
"""
2019-06-30 15:18:40 +00:00
currTime=datetime.datetime.utcnow()
personCache[personUrl]={ "actor": personJson, "timestamp": currTime.strftime("%Y-%m-%dT%H:%M:%SZ") }
2019-06-30 15:03:26 +00:00
def storeWebfingerInCache(handle: str,wf) -> None:
"""Store a webfinger endpoint in the cache
"""
cachedWebfingers[handle]=wf
def getPersonFromCache(personUrl: str):
"""Get an actor from the cache
"""
if personCache.get(personUrl):
2019-06-30 15:20:10 +00:00
# how old is the cached data?
2019-06-30 15:18:40 +00:00
currTime=datetime.datetime.utcnow()
cacheTime=datetime.strptime(personCache[personUrl]['timestamp'].replace('T',' '))
daysSinceCached=(currTime - cacheTime).days
2019-06-30 15:20:10 +00:00
# return cached value if it has not expired
2019-06-30 15:18:40 +00:00
if daysSinceCached <= 2:
return personCache[personUrl]['actor']
2019-06-30 15:03:26 +00:00
return None
def getWebfingerFromCache(handle: str):
"""Get webfinger endpoint from the cache
"""
if cachedWebfingers.get(handle):
return cachedWebfingers[handle]
return None