Handler functions for person cache

master
Bob Mottram 2019-06-30 12:34:19 +01:00
parent cf96832a0b
commit d3e005050e
2 changed files with 19 additions and 10 deletions

View File

@ -15,6 +15,19 @@ from webfinger import createWebfingerEndpoint
from webfinger import storeWebfingerEndpoint
from posts import createOutbox
# cache of actor json
# If there are repeated lookups then this helps prevent a lot
# of needless network traffic
personCache = {}
def storePersonInCache(personUrl: str,personJson) -> None:
personCache[personUrl]=personJson
def getPersonFromCache(personUrl: str):
if personCache.get(personUrl):
return personCache[personUrl]
return None
def getPersonKey(username: str,domain: str,keyType='public'):
"""Returns the public or private key of a person
"""

View File

@ -16,17 +16,14 @@ from pprint import pprint
from random import randint
from session import getJson
from session import postJson
from person import getPersonFromCache
from person import storePersonInCache
from person import getPersonKey
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
# cache of actor json
# If there are repeated lookups then this helps prevent a lot
# of needless network traffic
personCache = {}
def permitted(url: str,federationList) -> bool:
"""Is a url from one of the permitted domains?
"""
@ -70,9 +67,8 @@ def getPersonBox(session,wfRequest,boxName='inbox'):
personUrl = getUserUrl(wfRequest)
if not personUrl:
return None
if personCache.get(personUrl):
personJson=personCache[personUrl]
else:
personJson=getPersonFromCache(personUrl)
if not personJson:
personJson = getJson(session,personUrl,asHeader,None)
if not personJson.get(boxName):
return personPosts
@ -84,7 +80,7 @@ def getPersonBox(session,wfRequest,boxName='inbox'):
if personJson['publicKey'].get('publicKeyPem'):
pubKey=personJson['publicKey']['publicKeyPem']
personCache[personUrl]=personJson
storePersonInCache(personUrl,personJson)
return personJson[boxName],pubKey,personId