1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 # Cache for redundant SQL queries
23 def is_cache_initialized(key: str) -> bool:
26 def set_all_cache_key(key: str, rows: list, value: any):
27 # NOISY-DEBUG: print(f"DEBUG: key='{key}',rows()={len(rows)},value[]={type(value)} - CALLED!")
29 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
30 elif not is_cache_initialized(key):
31 # NOISY-DEBUG: print(f"DEBUG: Cache for key='{key}' not initialized.")
35 # NOISY-DEBUG: print(f"DEBUG: Setting key='{key}',sub[{type(sub)}]='{sub}'")
37 if isinstance(sub, tuple):
38 cache[key][sub[0]] = value
40 print(f"WARNING: Unsupported type row[]='{type(row)}'")
42 # NOISY-DEBUG: print("DEBUG: EXIT!")
44 def set_cache_key(key: str, sub: str, value: any):
46 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
47 elif type(sub) != str:
48 raise ValueError("Parameter sub[]='{type(sub)}' is not 'str'")
49 elif not is_cache_initialized(key):
50 print(f"WARNING: Bad method call, key='{key}' is not initialized yet.")
51 raise Exception(f"Cache for key='{key}' is not initialized, but function called")
53 cache[key][sub] = value
55 def is_cache_key_set(key: str, sub: str) -> bool:
57 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
58 elif type(sub) != str:
59 raise ValueError("Parameter sub[]='{type(sub)}' is not 'str'")
60 elif not is_cache_initialized(key):
61 print(f"WARNING: Bad method call, key='{key}' is not initialized yet.")
62 raise Exception(f"Cache for key='{key}' is not initialized, but function called")
64 return sub in cache[key]