forked from indymedia/epicyon
Function to expire actor cache entries
parent
8bd42b9d59
commit
848d438767
50
cache.py
50
cache.py
|
@ -31,26 +31,46 @@ def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: {
|
||||||
with open(cacheFilename, 'w') as fp:
|
with open(cacheFilename, 'w') as fp:
|
||||||
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
|
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
|
||||||
|
|
||||||
|
def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}:
|
||||||
|
"""Get an actor from the cache
|
||||||
|
"""
|
||||||
|
# if the actor is not in memory then try to load it from file
|
||||||
|
if not personCache.get(personUrl):
|
||||||
|
cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
|
||||||
|
if os.path.isfile(cacheFilename):
|
||||||
|
with open(cacheFilename, 'r') as fp:
|
||||||
|
personJson=commentjson.load(fp)
|
||||||
|
storePersonInCache(baseDir,personUrl,personJson,personCache)
|
||||||
|
|
||||||
|
if personCache.get(personUrl):
|
||||||
|
# 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")
|
||||||
|
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():
|
||||||
|
cacheTime= \
|
||||||
|
datetime.datetime.strptime(cacheJson['timestamp'], \
|
||||||
|
"%Y-%m-%dT%H:%M:%SZ")
|
||||||
|
daysSinceCached=(currTime - cacheTime).days
|
||||||
|
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:
|
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 getPersonFromCache(personUrl: str,personCache: {}) -> {}:
|
|
||||||
"""Get an actor from the cache
|
|
||||||
"""
|
|
||||||
if personCache.get(personUrl):
|
|
||||||
# how old is the cached data?
|
|
||||||
currTime=datetime.datetime.utcnow()
|
|
||||||
cacheTime= \
|
|
||||||
datetime.datetime.strptime(personCache[personUrl]['timestamp'], \
|
|
||||||
"%Y-%m-%dT%H:%M:%SZ")
|
|
||||||
daysSinceCached=(currTime - cacheTime).days
|
|
||||||
# return cached value if it has not expired
|
|
||||||
if daysSinceCached <= 2:
|
|
||||||
return personCache[personUrl]['actor']
|
|
||||||
return None
|
|
||||||
|
|
||||||
def getWebfingerFromCache(handle: str,cachedWebfingers: {}) -> {}:
|
def getWebfingerFromCache(handle: str,cachedWebfingers: {}) -> {}:
|
||||||
"""Get webfinger endpoint from the cache
|
"""Get webfinger endpoint from the cache
|
||||||
"""
|
"""
|
||||||
|
|
2
inbox.py
2
inbox.py
|
@ -95,7 +95,7 @@ def getPersonPubKey(baseDir: str,session,personUrl: str, \
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: Obtaining public key for shared inbox')
|
print('DEBUG: Obtaining public key for shared inbox')
|
||||||
personUrl=personUrl.replace('/users/inbox','/inbox')
|
personUrl=personUrl.replace('/users/inbox','/inbox')
|
||||||
personJson = getPersonFromCache(personUrl,personCache)
|
personJson = getPersonFromCache(baseDir,personUrl,personCache)
|
||||||
if not personJson:
|
if not personJson:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: Obtaining public key for '+personUrl)
|
print('DEBUG: Obtaining public key for '+personUrl)
|
||||||
|
|
2
posts.py
2
posts.py
|
@ -146,7 +146,7 @@ def getPersonBox(baseDir: str,session,wfRequest: {},personCache: {}, \
|
||||||
personUrl = getUserUrl(wfRequest)
|
personUrl = getUserUrl(wfRequest)
|
||||||
if not personUrl:
|
if not personUrl:
|
||||||
return None,None,None,None,None,None,None,None
|
return None,None,None,None,None,None,None,None
|
||||||
personJson = getPersonFromCache(personUrl,personCache)
|
personJson = getPersonFromCache(baseDir,personUrl,personCache)
|
||||||
if not personJson:
|
if not personJson:
|
||||||
personJson = getJson(session,personUrl,asHeader,None, \
|
personJson = getJson(session,personUrl,asHeader,None, \
|
||||||
projectVersion,httpPrefix,domain)
|
projectVersion,httpPrefix,domain)
|
||||||
|
|
2
tests.py
2
tests.py
|
@ -147,7 +147,7 @@ def testCache():
|
||||||
personJson={ "id": 123456, "test": "This is a test" }
|
personJson={ "id": 123456, "test": "This is a test" }
|
||||||
personCache={}
|
personCache={}
|
||||||
storePersonInCache(None,personUrl,personJson,personCache)
|
storePersonInCache(None,personUrl,personJson,personCache)
|
||||||
result=getPersonFromCache(personUrl,personCache)
|
result=getPersonFromCache(None,personUrl,personCache)
|
||||||
assert result['id']==123456
|
assert result['id']==123456
|
||||||
assert result['test']=='This is a test'
|
assert result['test']=='This is a test'
|
||||||
|
|
||||||
|
|
|
@ -36,10 +36,10 @@ from config import getConfigParam
|
||||||
from skills import getSkills
|
from skills import getSkills
|
||||||
from cache import getPersonFromCache
|
from cache import getPersonFromCache
|
||||||
|
|
||||||
def getPersonAvatarUrl(personUrl: str,personCache: {}) -> str:
|
def getPersonAvatarUrl(baseDir: str,personUrl: str,personCache: {}) -> str:
|
||||||
"""Returns the avatar url for the person
|
"""Returns the avatar url for the person
|
||||||
"""
|
"""
|
||||||
personJson = getPersonFromCache(personUrl,personCache)
|
personJson = getPersonFromCache(baseDir,personUrl,personCache)
|
||||||
if personJson:
|
if personJson:
|
||||||
if personJson.get('icon'):
|
if personJson.get('icon'):
|
||||||
if personJson['icon'].get('url'):
|
if personJson['icon'].get('url'):
|
||||||
|
@ -947,7 +947,7 @@ def individualFollowAsHtml(baseDir: str,session,wfRequest: {}, \
|
||||||
nickname=getNicknameFromActor(followUrl)
|
nickname=getNicknameFromActor(followUrl)
|
||||||
domain,port=getDomainFromActor(followUrl)
|
domain,port=getDomainFromActor(followUrl)
|
||||||
titleStr='@'+nickname+'@'+domain
|
titleStr='@'+nickname+'@'+domain
|
||||||
avatarUrl=getPersonAvatarUrl(followUrl,personCache)
|
avatarUrl=getPersonAvatarUrl(baseDir,followUrl,personCache)
|
||||||
if not avatarUrl:
|
if not avatarUrl:
|
||||||
avatarUrl=followUrl+'/avatar.png'
|
avatarUrl=followUrl+'/avatar.png'
|
||||||
if domain not in followUrl:
|
if domain not in followUrl:
|
||||||
|
@ -1093,7 +1093,7 @@ def individualPostAsHtml(baseDir: str, \
|
||||||
attachmentCtr+=1
|
attachmentCtr+=1
|
||||||
|
|
||||||
if not avatarUrl:
|
if not avatarUrl:
|
||||||
avatarUrl=getPersonAvatarUrl(postJsonObject['actor'],personCache)
|
avatarUrl=getPersonAvatarUrl(baseDir,postJsonObject['actor'],personCache)
|
||||||
if not avatarUrl:
|
if not avatarUrl:
|
||||||
avatarUrl=postJsonObject['actor']+'/avatar.png'
|
avatarUrl=postJsonObject['actor']+'/avatar.png'
|
||||||
|
|
||||||
|
@ -1321,7 +1321,7 @@ def htmlTimeline(pageNumber: int,itemsPerPage: int,session,baseDir: str, \
|
||||||
for item in timelineJson['orderedItems']:
|
for item in timelineJson['orderedItems']:
|
||||||
if item['type']=='Create' or item['type']=='Announce':
|
if item['type']=='Create' or item['type']=='Announce':
|
||||||
itemCtr+=1
|
itemCtr+=1
|
||||||
avatarUrl=getPersonAvatarUrl(item['actor'],personCache)
|
avatarUrl=getPersonAvatarUrl(baseDir,item['actor'],personCache)
|
||||||
tlStr+=individualPostAsHtml(baseDir,session,wfRequest,personCache, \
|
tlStr+=individualPostAsHtml(baseDir,session,wfRequest,personCache, \
|
||||||
nickname,domain,port,item,avatarUrl,True, \
|
nickname,domain,port,item,avatarUrl,True, \
|
||||||
allowDeletion, \
|
allowDeletion, \
|
||||||
|
@ -1668,7 +1668,7 @@ def htmlProfileAfterSearch(baseDir: str,path: str,httpPrefix: str, \
|
||||||
if profileJson['icon'].get('url'):
|
if profileJson['icon'].get('url'):
|
||||||
avatarUrl=profileJson['icon']['url']
|
avatarUrl=profileJson['icon']['url']
|
||||||
if not avatarUrl:
|
if not avatarUrl:
|
||||||
avatarUrl=getPersonAvatarUrl(personUrl,personCache)
|
avatarUrl=getPersonAvatarUrl(baseDir,personUrl,personCache)
|
||||||
preferredName=searchNickname
|
preferredName=searchNickname
|
||||||
if profileJson.get('preferredUsername'):
|
if profileJson.get('preferredUsername'):
|
||||||
preferredName=profileJson['preferredUsername']
|
preferredName=profileJson['preferredUsername']
|
||||||
|
|
Loading…
Reference in New Issue