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

View File

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

View File

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

View File

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

View File

@ -209,8 +209,8 @@ def testCache():
"test": "This is a test" "test": "This is a test"
} }
personCache = {} personCache = {}
storePersonInCache(None, personUrl, personJson, personCache) storePersonInCache(None, personUrl, personJson, personCache, True)
result = getPersonFromCache(None, personUrl, personCache) result = getPersonFromCache(None, personUrl, personCache, True)
assert result['id'] == 123456 assert result['id'] == 123456
assert result['test'] == 'This is a test' 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 " + "public keys don't match when downloading actor for " +
actor) actor)
return None return None
storePersonInCache(baseDir, actor, personJson, personCache) storePersonInCache(baseDir, actor, personJson, personCache,
allowDownloads)
return getPersonAvatarUrl(baseDir, actor, personCache, return getPersonAvatarUrl(baseDir, actor, personCache,
allowDownloads) allowDownloads)
return None return None
@ -295,7 +296,8 @@ def getPersonAvatarUrl(baseDir: str, personUrl: str, personCache: {},
allowDownloads: bool) -> str: allowDownloads: bool) -> str:
"""Returns the avatar url for the person """Returns the avatar url for the person
""" """
personJson = getPersonFromCache(baseDir, personUrl, personCache) personJson = \
getPersonFromCache(baseDir, personUrl, personCache, allowDownloads)
if not personJson: if not personJson:
return None return None
@ -4359,7 +4361,8 @@ def individualPostAsHtml(allowDownloads: bool,
if announceNickname: if announceNickname:
announceDomain, announcePort = \ announceDomain, announcePort = \
getDomainFromActor(attributedTo) getDomainFromActor(attributedTo)
getPersonFromCache(baseDir, attributedTo, personCache) getPersonFromCache(baseDir, attributedTo,
personCache, allowDownloads)
announceDisplayName = \ announceDisplayName = \
getDisplayName(baseDir, attributedTo, personCache) getDisplayName(baseDir, attributedTo, personCache)
if announceDisplayName: if announceDisplayName:
@ -4472,7 +4475,8 @@ def individualPostAsHtml(allowDownloads: bool,
getDomainFromActor(replyActor) getDomainFromActor(replyActor)
if replyNickname and replyDomain: if replyNickname and replyDomain:
getPersonFromCache(baseDir, replyActor, getPersonFromCache(baseDir, replyActor,
personCache) personCache,
allowDownloads)
replyDisplayName = \ replyDisplayName = \
getDisplayName(baseDir, replyActor, getDisplayName(baseDir, replyActor,
personCache) personCache)