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 webfinger import storeWebfingerEndpoint
from posts import createOutbox 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'): def getPersonKey(username: str,domain: str,keyType='public'):
"""Returns the public or private key of a person """Returns the public or private key of a person
""" """

View File

@ -16,17 +16,14 @@ from pprint import pprint
from random import randint from random import randint
from session import getJson from session import getJson
from session import postJson from session import postJson
from person import getPersonFromCache
from person import storePersonInCache
from person import getPersonKey from person import getPersonKey
try: try:
from BeautifulSoup import BeautifulSoup from BeautifulSoup import BeautifulSoup
except ImportError: except ImportError:
from bs4 import BeautifulSoup 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: def permitted(url: str,federationList) -> bool:
"""Is a url from one of the permitted domains? """Is a url from one of the permitted domains?
""" """
@ -70,9 +67,8 @@ def getPersonBox(session,wfRequest,boxName='inbox'):
personUrl = getUserUrl(wfRequest) personUrl = getUserUrl(wfRequest)
if not personUrl: if not personUrl:
return None return None
if personCache.get(personUrl): personJson=getPersonFromCache(personUrl)
personJson=personCache[personUrl] if not personJson:
else:
personJson = getJson(session,personUrl,asHeader,None) personJson = getJson(session,personUrl,asHeader,None)
if not personJson.get(boxName): if not personJson.get(boxName):
return personPosts return personPosts
@ -84,7 +80,7 @@ def getPersonBox(session,wfRequest,boxName='inbox'):
if personJson['publicKey'].get('publicKeyPem'): if personJson['publicKey'].get('publicKeyPem'):
pubKey=personJson['publicKey']['publicKeyPem'] pubKey=personJson['publicKey']['publicKeyPem']
personCache[personUrl]=personJson storePersonInCache(personUrl,personJson)
return personJson[boxName],pubKey,personId return personJson[boxName],pubKey,personId