Ver Fonte

make cache fixed size deleting oldest when full and pruning stale on add

Daniel Sheffield há 1 ano atrás
pai
commit
c1b6d511ed
2 ficheiros alterados com 11 adições e 2 exclusões
  1. 3 2
      app/rest/CachedLoadingPage.py
  2. 8 0
      app/rest/pyapi.py

+ 3 - 2
app/rest/CachedLoadingPage.py

@@ -18,7 +18,8 @@ class CachedLoadingPage():
         self.value = value
         self._lock = Lock()
 
-    def _age(self) -> float:
+    @property
+    def age(self) -> float:
         return time() - self._created
 
     @property
@@ -35,7 +36,7 @@ class CachedLoadingPage():
     
     @property
     def stale(self) -> bool:
-        return self._age() > 10*60
+        return self.age > 10*60
 
     def update(self) -> str:
         if not self._lock.acquire(blocking=True, timeout=0.5):

+ 8 - 0
app/rest/pyapi.py

@@ -127,6 +127,14 @@ def trend():
         
         try:
             page = CachedLoadingPage(template("loading", progress=[]))
+            for k in [k for k, v in CACHE.items() if v.stale]:
+                del CACHE[k]
+            
+            for idx, (_, k) in enumerate(sorted([
+                (v.age, k) for k, v in CACHE.items()
+            ])):
+                if idx > 10: del CACHE[k]
+            
             CACHE[request.query_string] = page
             thread = Thread(target=worker.trend, args=(page.queue, conn, path, request.query))
             thread.start()