epicyon/cache.py

101 lines
3.5 KiB
Python
Raw Normal View History

2019-06-30 15:03:26 +00:00
__filename__ = "cache.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2019-08-29 13:35:29 +00:00
__version__ = "1.0.0"
2019-06-30 15:03:26 +00:00
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-08-20 09:16:03 +00:00
import os
2019-08-20 10:28:48 +00:00
import time
2019-06-30 15:18:40 +00:00
import datetime
2019-08-20 09:16:03 +00:00
import commentjson
2019-09-14 17:12:03 +00:00
2019-08-20 09:16:03 +00:00
def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: {}) -> 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()
2019-07-06 17:00:22 +00:00
personCache[personUrl]={
"actor": personJson,
"timestamp": currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
}
2019-08-20 09:16:03 +00:00
if not baseDir:
return
# store to file
2019-08-20 21:26:24 +00:00
if os.path.isdir(baseDir+'/cache/actors'):
cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
if not os.path.isfile(cacheFilename):
2019-10-12 09:37:21 +00:00
tries=0
while tries<5:
try:
with open(cacheFilename, 'w') as fp:
2019-10-18 18:57:34 +00:00
commentjson.dump(personJson, fp, indent=2, sort_keys=False)
2019-10-12 09:37:21 +00:00
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
2019-06-30 15:03:26 +00:00
2019-08-20 09:37:09 +00:00
def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}:
2019-06-30 15:03:26 +00:00
"""Get an actor from the cache
"""
2019-08-20 09:37:09 +00:00
# if the actor is not in memory then try to load it from file
2019-08-20 09:50:27 +00:00
loadedFromFile=False
2019-08-20 09:37:09 +00:00
if not personCache.get(personUrl):
cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
if os.path.isfile(cacheFilename):
personJson=None
2019-10-11 18:03:58 +00:00
tries=0
while tries<5:
try:
with open(cacheFilename, 'r') as fp:
personJson=commentjson.load(fp)
break
except Exception as e:
2019-10-17 10:26:56 +00:00
print('WARN: commentjson exception getPersonFromCache - '+str(e))
2019-10-16 14:21:01 +00:00
print('ERROR: unable to load actor from cache '+cacheFilename+' try '+str(tries))
2019-10-11 18:03:58 +00:00
print(e)
2019-10-12 09:37:21 +00:00
time.sleep(1)
2019-10-11 18:03:58 +00:00
tries+=1
if personJson:
2019-08-20 09:37:09 +00:00
storePersonInCache(baseDir,personUrl,personJson,personCache)
2019-08-20 09:50:27 +00:00
loadedFromFile=True
2019-08-20 09:37:09 +00:00
2019-06-30 15:03:26 +00:00
if personCache.get(personUrl):
2019-08-20 09:50:27 +00:00
if not loadedFromFile:
# update the timestamp for the last time the actor was retrieved
currTime=datetime.datetime.utcnow()
personCache[personUrl]['timestamp']=currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
2019-08-20 09:37:09 +00:00
return personCache[personUrl]['actor']
return None
def expirePersonCache(personCache: {}):
"""Expires old entries from the cache in memory
"""
currTime=datetime.datetime.utcnow()
removals=[]
for personUrl,cacheJson in personCache.items():
2019-07-06 17:00:22 +00:00
cacheTime= \
2019-08-20 09:37:09 +00:00
datetime.datetime.strptime(cacheJson['timestamp'], \
2019-07-06 17:00:22 +00:00
"%Y-%m-%dT%H:%M:%SZ")
2019-06-30 15:18:40 +00:00
daysSinceCached=(currTime - cacheTime).days
2019-08-20 09:37:09 +00:00
if daysSinceCached > 2:
removals.append(personUrl)
if len(removals)>0:
for personUrl in removals:
del personCache[personUrl]
print(str(len(removals))+' actors were expired from the cache')
def storeWebfingerInCache(handle: str,wf,cachedWebfingers: {}) -> None:
"""Store a webfinger endpoint in the cache
"""
cachedWebfingers[handle]=wf
2019-06-30 15:03:26 +00:00
2019-07-01 14:30:48 +00:00
def getWebfingerFromCache(handle: str,cachedWebfingers: {}) -> {}:
2019-06-30 15:03:26 +00:00
"""Get webfinger endpoint from the cache
"""
if cachedWebfingers.get(handle):
return cachedWebfingers[handle]
return None