Don't write actors to cache while loading the timeline

main
Bob Mottram 2020-08-29 11:21:29 +01:00
parent 74046a9c3d
commit 77c5f810f9
6 changed files with 35 additions and 21 deletions

View File

@ -14,7 +14,8 @@ from utils import getFileCaseInsensitive
def storePersonInCache(baseDir: str, personUrl: str,
personJson: {}, personCache: {}) -> None:
personJson: {}, personCache: {},
allowWriteToFile: bool) -> None:
"""Store an actor in the cache
"""
currTime = datetime.datetime.utcnow()
@ -26,25 +27,29 @@ def storePersonInCache(baseDir: str, personUrl: str,
return
# store to file
if os.path.isdir(baseDir+'/cache/actors'):
cacheFilename = baseDir + '/cache/actors/' + \
personUrl.replace('/', '#')+'.json'
if not os.path.isfile(cacheFilename):
saveJson(personJson, cacheFilename)
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)
def getPersonFromCache(baseDir: str, personUrl: str, personCache: {}) -> {}:
def getPersonFromCache(baseDir: str, personUrl: str, personCache: {},
allowWriteToFile: bool) -> {}:
"""Get an actor from the cache
"""
# if the actor is not in memory then try to load it from file
loadedFromFile = False
if not personCache.get(personUrl):
# does the person exist as a cached file?
cacheFilename = baseDir + '/cache/actors/' + \
personUrl.replace('/', '#')+'.json'
if os.path.isfile(getFileCaseInsensitive(cacheFilename)):
personJson = loadJson(getFileCaseInsensitive(cacheFilename))
if personJson:
storePersonInCache(baseDir, personUrl, personJson, personCache)
storePersonInCache(baseDir, personUrl, personJson,
personCache, False)
loadedFromFile = True
if personCache.get(personUrl):

View File

@ -1768,7 +1768,8 @@ class PubServer(BaseHTTPRequestHandler):
emailAddress = None
actorJson = getPersonFromCache(self.server.baseDir,
optionsActor,
self.server.personCache)
self.server.personCache,
True)
if actorJson:
donateUrl = getDonationUrl(actorJson)
xmppAddress = getXmppAddress(actorJson)
@ -7652,7 +7653,8 @@ class PubServer(BaseHTTPRequestHandler):
# personCache in memory
storePersonInCache(self.server.baseDir,
actorJson['id'], actorJson,
self.server.personCache)
self.server.personCache,
True)
# clear any cached images for this actor
idStr = actorJson['id'].replace('/', '-')
removeAvatarFromCache(self.server.baseDir, idStr)

View File

@ -199,7 +199,8 @@ def getPersonPubKey(baseDir: str, session, personUrl: str,
if debug:
print('DEBUG: Obtaining public key for shared inbox')
personUrl = personUrl.replace('/users/inbox', '/inbox')
personJson = getPersonFromCache(baseDir, personUrl, personCache)
personJson = \
getPersonFromCache(baseDir, personUrl, personCache, True)
if not personJson:
if debug:
print('DEBUG: Obtaining public key for ' + personUrl)
@ -228,7 +229,7 @@ def getPersonPubKey(baseDir: str, session, personUrl: str,
if debug:
print('DEBUG: Public key not found for ' + personUrl)
storePersonInCache(baseDir, personUrl, personJson, personCache)
storePersonInCache(baseDir, personUrl, personJson, personCache, True)
return pubKey
@ -865,7 +866,8 @@ def personReceiveUpdate(baseDir: str,
'cached actor when updating')
return False
# save to cache in memory
storePersonInCache(baseDir, personJson['id'], personJson, personCache)
storePersonInCache(baseDir, personJson['id'], personJson,
personCache, True)
# save to cache on file
if saveJson(personJson, actorFilename):
print('actor updated for ' + personJson['id'])

View File

@ -209,7 +209,8 @@ def getPersonBox(baseDir: str, session, wfRequest: {},
personUrl = httpPrefix + '://' + domain + '/users/' + nickname
if not personUrl:
return None, None, None, None, None, None, None, None
personJson = getPersonFromCache(baseDir, personUrl, personCache)
personJson = \
getPersonFromCache(baseDir, personUrl, personCache, True)
if not personJson:
if '/channel/' in personUrl or '/accounts/' in personUrl:
asHeader = {
@ -265,7 +266,7 @@ def getPersonBox(baseDir: str, session, wfRequest: {},
if personJson.get('name'):
displayName = personJson['name']
storePersonInCache(baseDir, personUrl, personJson, personCache)
storePersonInCache(baseDir, personUrl, personJson, personCache, True)
return boxJson, pubKeyId, pubKey, personId, sharedInbox, \
capabilityAcquisition, avatarUrl, displayName

View File

@ -209,8 +209,8 @@ def testCache():
"test": "This is a test"
}
personCache = {}
storePersonInCache(None, personUrl, personJson, personCache)
result = getPersonFromCache(None, personUrl, personCache)
storePersonInCache(None, personUrl, personJson, personCache, True)
result = getPersonFromCache(None, personUrl, personCache, True)
assert result['id'] == 123456
assert result['test'] == 'This is a test'

View File

@ -284,7 +284,8 @@ def updateAvatarImageCache(session, baseDir: str, httpPrefix: str,
"public keys don't match when downloading actor for " +
actor)
return None
storePersonInCache(baseDir, actor, personJson, personCache)
storePersonInCache(baseDir, actor, personJson, personCache,
allowDownloads)
return getPersonAvatarUrl(baseDir, actor, personCache,
allowDownloads)
return None
@ -295,7 +296,8 @@ def getPersonAvatarUrl(baseDir: str, personUrl: str, personCache: {},
allowDownloads: bool) -> str:
"""Returns the avatar url for the person
"""
personJson = getPersonFromCache(baseDir, personUrl, personCache)
personJson = \
getPersonFromCache(baseDir, personUrl, personCache, allowDownloads)
if not personJson:
return None
@ -4359,7 +4361,8 @@ def individualPostAsHtml(allowDownloads: bool,
if announceNickname:
announceDomain, announcePort = \
getDomainFromActor(attributedTo)
getPersonFromCache(baseDir, attributedTo, personCache)
getPersonFromCache(baseDir, attributedTo,
personCache, allowDownloads)
announceDisplayName = \
getDisplayName(baseDir, attributedTo, personCache)
if announceDisplayName:
@ -4472,7 +4475,8 @@ def individualPostAsHtml(allowDownloads: bool,
getDomainFromActor(replyActor)
if replyNickname and replyDomain:
getPersonFromCache(baseDir, replyActor,
personCache)
personCache,
allowDownloads)
replyDisplayName = \
getDisplayName(baseDir, replyActor,
personCache)