From 77c5f810f9da01636831d0a64f710475530f6bbb Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 29 Aug 2020 11:21:29 +0100 Subject: [PATCH] Don't write actors to cache while loading the timeline --- cache.py | 21 +++++++++++++-------- daemon.py | 6 ++++-- inbox.py | 8 +++++--- posts.py | 5 +++-- tests.py | 4 ++-- webinterface.py | 12 ++++++++---- 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/cache.py b/cache.py index 37d1f427..0ac180f8 100644 --- a/cache.py +++ b/cache.py @@ -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): diff --git a/daemon.py b/daemon.py index c4a57b67..ab21dc40 100644 --- a/daemon.py +++ b/daemon.py @@ -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) diff --git a/inbox.py b/inbox.py index 2e467376..3fe84dc5 100644 --- a/inbox.py +++ b/inbox.py @@ -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']) diff --git a/posts.py b/posts.py index 448cde45..e9503e12 100644 --- a/posts.py +++ b/posts.py @@ -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 diff --git a/tests.py b/tests.py index 59b97c46..539349b9 100644 --- a/tests.py +++ b/tests.py @@ -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' diff --git a/webinterface.py b/webinterface.py index 330ad8a5..7b8b4791 100644 --- a/webinterface.py +++ b/webinterface.py @@ -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)