epicyon/cache.py

94 lines
3.2 KiB
Python
Raw Normal View History

2020-04-02 09:02:33 +00:00
__filename__ = "cache.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-06-30 15:03:26 +00:00
2019-08-20 09:16:03 +00:00
import os
2019-06-30 15:18:40 +00:00
import datetime
2019-10-22 11:55:06 +00:00
from utils import loadJson
from utils import saveJson
from utils import getFileCaseInsensitive
2020-04-02 09:02:33 +00:00
2020-05-04 19:16:11 +00:00
2020-04-02 09:02:33 +00:00
def storePersonInCache(baseDir: str, personUrl: str,
personJson: {}, personCache: {},
allowWriteToFile: bool) -> None:
2019-06-30 15:03:26 +00:00
"""Store an actor in the cache
"""
2020-04-02 09:02:33 +00:00
currTime = datetime.datetime.utcnow()
personCache[personUrl] = {
2019-07-06 17:00:22 +00:00
"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
if allowWriteToFile:
if os.path.isdir(baseDir+'/cache/actors'):
cacheFilename = baseDir + '/cache/actors/' + \
personUrl.replace('/', '#')+'.json'
if not os.path.isfile(cacheFilename):
saveJson(personJson, cacheFilename)
2020-04-02 09:02:33 +00:00
2019-06-30 15:03:26 +00:00
def getPersonFromCache(baseDir: str, personUrl: str, personCache: {},
allowWriteToFile: bool) -> {}:
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
2020-04-02 09:02:33 +00:00
loadedFromFile = False
2019-08-20 09:37:09 +00:00
if not personCache.get(personUrl):
# does the person exist as a cached file?
2020-04-02 09:02:33 +00:00
cacheFilename = baseDir + '/cache/actors/' + \
personUrl.replace('/', '#')+'.json'
if os.path.isfile(getFileCaseInsensitive(cacheFilename)):
personJson = loadJson(getFileCaseInsensitive(cacheFilename))
if personJson:
storePersonInCache(baseDir, personUrl, personJson,
personCache, False)
2020-04-02 09:02:33 +00:00
loadedFromFile = True
2020-03-22 21:16:02 +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
2020-04-02 09:02:33 +00:00
currTime = datetime.datetime.utcnow()
currTimeStr = currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
personCache[personUrl]['timestamp'] = currTimeStr
2019-08-20 09:37:09 +00:00
return personCache[personUrl]['actor']
return None
2020-04-02 09:02:33 +00:00
2019-08-20 09:37:09 +00:00
def expirePersonCache(personCache: {}):
"""Expires old entries from the cache in memory
"""
2020-04-02 09:02:33 +00:00
currTime = datetime.datetime.utcnow()
removals = []
for personUrl, cacheJson in personCache.items():
cacheTime = datetime.datetime.strptime(cacheJson['timestamp'],
"%Y-%m-%dT%H:%M:%SZ")
daysSinceCached = (currTime - cacheTime).days
2019-08-20 09:37:09 +00:00
if daysSinceCached > 2:
removals.append(personUrl)
2020-04-02 09:02:33 +00:00
if len(removals) > 0:
2019-08-20 09:37:09 +00:00
for personUrl in removals:
del personCache[personUrl]
2020-04-02 09:02:33 +00:00
print(str(len(removals)) + ' actors were expired from the cache')
2019-08-20 09:37:09 +00:00
2020-04-02 09:02:33 +00:00
def storeWebfingerInCache(handle: str, wf, cachedWebfingers: {}) -> None:
2019-08-20 09:37:09 +00:00
"""Store a webfinger endpoint in the cache
"""
2020-04-02 09:02:33 +00:00
cachedWebfingers[handle] = wf
2019-06-30 15:03:26 +00:00
2020-04-02 09:02:33 +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