forked from indymedia/epicyon
flake8 style
parent
fa7c457150
commit
161cef7fff
21
cache.py
21
cache.py
|
@ -7,12 +7,12 @@ __email__="bob@freedombone.net"
|
||||||
__status__ = "Production"
|
__status__ = "Production"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
import datetime
|
import datetime
|
||||||
from utils import loadJson
|
from utils import loadJson
|
||||||
from utils import saveJson
|
from utils import saveJson
|
||||||
|
|
||||||
def storePersonInCache(baseDir: str,personUrl: str, \
|
|
||||||
|
def storePersonInCache(baseDir: str, personUrl: str,
|
||||||
personJson: {}, personCache: {}) -> None:
|
personJson: {}, personCache: {}) -> None:
|
||||||
"""Store an actor in the cache
|
"""Store an actor in the cache
|
||||||
"""
|
"""
|
||||||
|
@ -26,18 +26,20 @@ def storePersonInCache(baseDir: str,personUrl: str, \
|
||||||
|
|
||||||
# store to file
|
# store to file
|
||||||
if os.path.isdir(baseDir+'/cache/actors'):
|
if os.path.isdir(baseDir+'/cache/actors'):
|
||||||
cacheFilename= \
|
cacheFilename = baseDir + '/cache/actors/' + \
|
||||||
baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
|
personUrl.replace('/', '#')+'.json'
|
||||||
if not os.path.isfile(cacheFilename):
|
if not os.path.isfile(cacheFilename):
|
||||||
saveJson(personJson, cacheFilename)
|
saveJson(personJson, cacheFilename)
|
||||||
|
|
||||||
|
|
||||||
def getPersonFromCache(baseDir: str, personUrl: str, personCache: {}) -> {}:
|
def getPersonFromCache(baseDir: str, personUrl: str, personCache: {}) -> {}:
|
||||||
"""Get an actor from the cache
|
"""Get an actor from the cache
|
||||||
"""
|
"""
|
||||||
# if the actor is not in memory then try to load it from file
|
# if the actor is not in memory then try to load it from file
|
||||||
loadedFromFile = False
|
loadedFromFile = False
|
||||||
if not personCache.get(personUrl):
|
if not personCache.get(personUrl):
|
||||||
cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
|
cacheFilename = baseDir + '/cache/actors/' + \
|
||||||
|
personUrl.replace('/', '#')+'.json'
|
||||||
if os.path.isfile(cacheFilename):
|
if os.path.isfile(cacheFilename):
|
||||||
personJson = loadJson(cacheFilename)
|
personJson = loadJson(cacheFilename)
|
||||||
if personJson:
|
if personJson:
|
||||||
|
@ -48,18 +50,19 @@ def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}:
|
||||||
if not loadedFromFile:
|
if not loadedFromFile:
|
||||||
# update the timestamp for the last time the actor was retrieved
|
# update the timestamp for the last time the actor was retrieved
|
||||||
currTime = datetime.datetime.utcnow()
|
currTime = datetime.datetime.utcnow()
|
||||||
personCache[personUrl]['timestamp']=currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
|
currTimeStr = currTime.strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||||
|
personCache[personUrl]['timestamp'] = currTimeStr
|
||||||
return personCache[personUrl]['actor']
|
return personCache[personUrl]['actor']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def expirePersonCache(personCache: {}):
|
def expirePersonCache(personCache: {}):
|
||||||
"""Expires old entries from the cache in memory
|
"""Expires old entries from the cache in memory
|
||||||
"""
|
"""
|
||||||
currTime = datetime.datetime.utcnow()
|
currTime = datetime.datetime.utcnow()
|
||||||
removals = []
|
removals = []
|
||||||
for personUrl, cacheJson in personCache.items():
|
for personUrl, cacheJson in personCache.items():
|
||||||
cacheTime= \
|
cacheTime = datetime.datetime.strptime(cacheJson['timestamp'],
|
||||||
datetime.datetime.strptime(cacheJson['timestamp'], \
|
|
||||||
"%Y-%m-%dT%H:%M:%SZ")
|
"%Y-%m-%dT%H:%M:%SZ")
|
||||||
daysSinceCached = (currTime - cacheTime).days
|
daysSinceCached = (currTime - cacheTime).days
|
||||||
if daysSinceCached > 2:
|
if daysSinceCached > 2:
|
||||||
|
@ -69,11 +72,13 @@ def expirePersonCache(personCache: {}):
|
||||||
del personCache[personUrl]
|
del personCache[personUrl]
|
||||||
print(str(len(removals)) + ' actors were expired from the cache')
|
print(str(len(removals)) + ' actors were expired from the cache')
|
||||||
|
|
||||||
|
|
||||||
def storeWebfingerInCache(handle: str, wf, cachedWebfingers: {}) -> None:
|
def storeWebfingerInCache(handle: str, wf, cachedWebfingers: {}) -> None:
|
||||||
"""Store a webfinger endpoint in the cache
|
"""Store a webfinger endpoint in the cache
|
||||||
"""
|
"""
|
||||||
cachedWebfingers[handle] = wf
|
cachedWebfingers[handle] = wf
|
||||||
|
|
||||||
|
|
||||||
def getWebfingerFromCache(handle: str, cachedWebfingers: {}) -> {}:
|
def getWebfingerFromCache(handle: str, cachedWebfingers: {}) -> {}:
|
||||||
"""Get webfinger endpoint from the cache
|
"""Get webfinger endpoint from the cache
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue