diff --git a/relay/database.py b/relay/database.py index 0d171a0..388fe08 100644 --- a/relay/database.py +++ b/relay/database.py @@ -20,6 +20,9 @@ for inbox in following: following.remove(inbox) DATABASE['relay-list'] = following +if 'actors' in DATABASE: + DATABASE.pop('actors') + async def database_save(): while True: with open(CONFIG['db'], 'w') as f: diff --git a/relay/remote_actor.py b/relay/remote_actor.py index a8b271f..a61a1d8 100644 --- a/relay/remote_actor.py +++ b/relay/remote_actor.py @@ -1,9 +1,16 @@ import aiohttp -from .database import DATABASE +from . import CONFIG from .http_debug import http_debug +from cachetools import TTLCache + + +CACHE_SIZE = CONFIG.get('cache-size', 16384) +CACHE_TTL = CONFIG.get('cache-ttl', 3600) + +ACTORS = TTLCache(CACHE_SIZE, CACHE_TTL) + -ACTORS = DATABASE.get("actors", {}) async def fetch_actor(uri, force=False): if uri in ACTORS and not force: return ACTORS[uri] @@ -11,5 +18,4 @@ async def fetch_actor(uri, force=False): async with aiohttp.ClientSession(trace_configs=[http_debug()]) as session: async with session.get(uri, headers={'Accept': 'application/activity+json'}) as resp: ACTORS[uri] = (await resp.json(encoding='utf-8', content_type=None)) - DATABASE["actors"] = ACTORS return ACTORS[uri]