database: no longer store remote actors indefinitely in JSON-LD database, use hot...
authorkaniini <kaniini@dereferenced.org>
Sun, 18 Nov 2018 14:25:04 +0000 (14:25 +0000)
committerkaniini <kaniini@dereferenced.org>
Sun, 18 Nov 2018 14:25:04 +0000 (14:25 +0000)
relay/database.py
relay/remote_actor.py

index 0d171a0..388fe08 100644 (file)
@@ -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:
index a8b271f..a61a1d8 100644 (file)
@@ -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]