epicyon/cache.py

50 lines
1.5 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 = {}
2019-07-01 11:09:09 +00:00
def storePersonInCache(personUrl: str,personJson: {}) -> None:
2019-06-30 15:03:26 +00:00
"""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
2019-07-01 11:09:09 +00:00
def getPersonFromCache(personUrl: str) -> {}:
2019-06-30 15:03:26 +00:00
"""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()
2019-06-30 16:29:53 +00:00
cacheTime=datetime.datetime.strptime(personCache[personUrl]['timestamp'],"%Y-%m-%dT%H:%M:%SZ")
2019-06-30 15:18:40 +00:00
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
2019-07-01 11:09:09 +00:00
def getWebfingerFromCache(handle: str) -> {}:
2019-06-30 15:03:26 +00:00
"""Get webfinger endpoint from the cache
"""
if cachedWebfingers.get(handle):
return cachedWebfingers[handle]
return None
2019-06-30 16:29:53 +00:00