|
@@ -3,6 +3,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
+import cherrypy
|
|
|
import os
|
|
|
from time import time
|
|
|
from typing import Dict
|
|
@@ -24,6 +25,7 @@ def delete_page(name: str, root: str = 'app/rest/static/files'):
|
|
|
|
|
|
def save_page(name: str, content: bytes, tool: str, root='app/rest/static/files') -> str:
|
|
|
directory = f'{root}/{name}'
|
|
|
+ cherrypy.log(f"saving file {directory}/{name}")
|
|
|
try:
|
|
|
os.mkdir(directory, mode=0o700, dir_fd=None)
|
|
|
except FileExistsError:
|
|
@@ -36,11 +38,17 @@ def save_page(name: str, content: bytes, tool: str, root='app/rest/static/files'
|
|
|
def get_page(name: str, root: str = 'app/rest/static/files') -> str:
|
|
|
directory = f'{root}/{name}'
|
|
|
|
|
|
+ cherrypy.log(f"getting {name} from file")
|
|
|
try:
|
|
|
mtime = os.stat(f'{directory}/{name}.file').st_mtime
|
|
|
except:
|
|
|
mtime = None
|
|
|
|
|
|
+ if mtime is None:
|
|
|
+ cherrypy.log(f"no file")
|
|
|
+ return None
|
|
|
+ cherrypy.log(f"mtime: {mtime}")
|
|
|
+ cherrypy.log(f"mtime stale: {mtime and time() - mtime > STALE}")
|
|
|
if mtime and time() - mtime > STALE:
|
|
|
delete_page(name)
|
|
|
return None
|
|
@@ -97,10 +105,14 @@ class Cache:
|
|
|
def get(self, key: str) -> CachedLoadingPage:
|
|
|
key = key_to_hash(key)
|
|
|
|
|
|
+ cherrypy.log(f"getting {key} from cache")
|
|
|
if key not in self._cache:
|
|
|
+ cherrypy.log(f"getting {key} from file")
|
|
|
try:
|
|
|
existing = get_page(hash_to_base32(key))
|
|
|
except:
|
|
|
+ existing = None
|
|
|
+ if existing is None:
|
|
|
return None
|
|
|
|
|
|
return self.add(key, CachedLoadingPage(existing, lambda q: q.put(None), incremental=True))
|
|
@@ -113,13 +125,13 @@ class Cache:
|
|
|
|
|
|
if not page.loaded:
|
|
|
page.update()
|
|
|
- if not page.loaded:
|
|
|
- return page
|
|
|
|
|
|
if page.loaded:
|
|
|
try:
|
|
|
existing = get_page(hash_to_base32(key))
|
|
|
except:
|
|
|
+ existing = None
|
|
|
+ if existing is None:
|
|
|
content = ''.join(page.value) if isinstance(page.value, list) else page.value
|
|
|
save_page(hash_to_base32(key), content.encode('utf-8'), tool='grocery')
|
|
|
|