Răsfoiți Sursa

revert using person for hash segregation

Daniel Sheffield 1 an în urmă
părinte
comite
73a7f378cd
4 a modificat fișierele cu 24 adăugiri și 26 ștergeri
  1. 10 11
      app/rest/PageCache.py
  2. 9 10
      app/rest/QueryCache.py
  3. 4 4
      app/rest/pyapi.py
  4. 1 1
      app/rest/route_decorators.py

+ 10 - 11
app/rest/PageCache.py

@@ -55,7 +55,7 @@ def get_page(name: str, root: str = 'app/rest/static/files') -> str:
     return page.decode('utf-8')
 
 
-def key_to_hash(key, person):
+def key_to_hash(key):
 
     if isinstance(key, tuple):
         orig, _hash = key
@@ -66,7 +66,7 @@ def key_to_hash(key, person):
             orig, _hash = key, None
 
     if None not in (orig, _hash):
-        if get_hash(orig, person) != _hash:
+        if get_hash(orig) != _hash:
             raise KeyError(f"Invalid key: {key}")
 
         return _hash
@@ -74,22 +74,21 @@ def key_to_hash(key, person):
     if (_hash, orig) is (None, None):
         raise KeyError(f"Invalid key: {key}")
     
-    return get_hash(orig, person) if _hash is None else _hash
+    return get_hash(orig) if _hash is None else _hash
 
 
-def get_hash(key, person):
-    _bytes = blake(key.encode('utf-8'), person=f'grocery-{person}'.encode('utf-8'))
+def get_hash(key):
+    _bytes = blake(key.encode('utf-8'), person='grocery'.encode('utf-8'))
     return bytes_to_hash(_bytes)
 
 
 class PageCache:
-    def __init__(self, limit, person) -> None:
+    def __init__(self, limit) -> None:
         self._cache: Dict[str, CachedLoadingPage] = dict()
         self._limit = limit
-        self._person = person
 
     def __delitem__(self, key):
-        key = key_to_hash(key, self._person)
+        key = key_to_hash(key)
         self._cache.pop(key, None)
         delete_page(hash_to_base32(key))
 
@@ -97,10 +96,10 @@ class PageCache:
         return self.get(key)
 
     def __setitem__(self, key, value):
-        self._cache[key_to_hash(key, self._person)] = value
+        self._cache[key_to_hash(key)] = value
 
     def get(self, key: str) -> CachedLoadingPage:
-        key = key_to_hash(key, self._person)
+        key = key_to_hash(key)
 
         if key not in self._cache:
             try:
@@ -145,7 +144,7 @@ class PageCache:
     def add(self, key: str, page: CachedLoadingPage) -> CachedLoadingPage:
         self._clear_stale()
         self._enforce_limit(self._limit)
-        self._cache[key_to_hash(key, self._person)] = page
+        self._cache[key_to_hash(key)] = page
         return page
     
     def remove(self, key: str):

+ 9 - 10
app/rest/QueryCache.py

@@ -51,7 +51,7 @@ def get_query(name: str, root: str = 'app/rest/static/files') -> str:
     return page.decode('utf-8')
 
 
-def norm(key, person):
+def norm(key):
 
     if isinstance(key, tuple):
         query, _hash = key
@@ -67,7 +67,7 @@ def norm(key, person):
         #_hash = base32_to_hash(query.hash)
 
     if None not in (query, _hash):
-        if get_hash(query, person) != _hash:
+        if get_hash(query) != _hash:
             raise KeyError(f"Invalid key: {key}")
 
         return query, _hash
@@ -75,10 +75,10 @@ def norm(key, person):
     if (_hash, query) is (None, None):
         raise KeyError(f"Invalid key: {key}")
     
-    return query, _hash if _hash else get_hash(query, person)
+    return query, _hash if _hash else get_hash(query)
 
-def get_hash(key, person):
-    _bytes = blake(key.encode('utf-8'), person=f'grocery-{person}'.encode('utf-8'))
+def get_hash(key):
+    _bytes = blake(key.encode('utf-8'), person='grocery'.encode('utf-8'))
     return bytes_to_hash(_bytes)
 
 
@@ -98,10 +98,9 @@ def normalize_query(query: FormsDict, allow: Iterable[str] = None) -> Tuple[str,
 
 
 class QueryCache:
-    def __init__(self, limit, person) -> None:
+    def __init__(self, limit) -> None:
         self._cache: Dict[int, str] = dict()
         self._limit = limit
-        self._person = person
 
     def __delitem__(self, key):
         return self.remove(key)
@@ -113,7 +112,7 @@ class QueryCache:
         return self.add(key, value)
 
     def get(self, key: str) -> str:
-        query, _hash = norm(key, self._person)
+        query, _hash = norm(key)
         if _hash not in self._cache:
             if query:
                 return self.add(_hash, query)
@@ -148,7 +147,7 @@ class QueryCache:
     def add(self, key: str, value: str) -> str:
         #self._clear_stale()
         #self._enforce_limit(self._limit)
-        query, _hash = norm(key, self._person)
+        query, _hash = norm(key)
         value = value or query
         
         if not value:
@@ -158,6 +157,6 @@ class QueryCache:
         return value
     
     def remove(self, key: str):
-        key = norm(key, self._person)
+        key = norm(key)
         self._cache.pop(key, None)
         delete_query(hash_to_base32(key))

+ 4 - 4
app/rest/pyapi.py

@@ -41,11 +41,11 @@ def new_thread(target, conn, path, forms):
         )).start()
     return cb
 
-PAGE_CACHE = PageCache(100, 's')
-QUERY_CACHE = QueryCache(None, 's')
+PAGE_CACHE = PageCache(100)
+QUERY_CACHE = QueryCache(None)
 
 @route('/grocery/volume', method=['GET', 'POST'])
-@cache(query_cache=QueryCache(None, 'volume'), page_cache=PageCache(100, 'volume'))
+@cache(query_cache=QUERY_CACHE, page_cache=PAGE_CACHE)
 def volume(key: Tuple[str, int], cache: PageCache):
     _, _, path, *_ = request.urlparts
 
@@ -59,7 +59,7 @@ def volume(key: Tuple[str, int], cache: PageCache):
 
 
 @route('/grocery/trend', method=['GET', 'POST'])
-@cache(query_cache=QueryCache(None, 'trend'), page_cache=PageCache(100, 'trend'))
+@cache(query_cache=QUERY_CACHE, page_cache=PAGE_CACHE)
 def trend(key: Tuple[str, int], cache: PageCache):
     _, _, path, *_ = request.urlparts
 

+ 1 - 1
app/rest/route_decorators.py

@@ -39,7 +39,7 @@ def _cache_decorator(func: Callable, query_cache: QueryCache = None, page_cache:
         
         query, _hash = normalize_query(request.params)
         if not _hash:
-            _hashInt = get_hash(query, query_cache._person)
+            _hashInt = get_hash(query)
             _hash = hash_to_base32(_hashInt)
             key = (query, _hashInt)
         else: