From 5871c4066716cab521edced84ff1367e1589e974 Mon Sep 17 00:00:00 2001 From: kaniini Date: Sun, 18 Nov 2018 14:25:04 +0000 Subject: [PATCH] database: no longer store remote actors indefinitely in JSON-LD database, use hot cache instead --- relay/database.py | 3 +++ relay/remote_actor.py | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) 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]