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/>.
17 # Cache for redundant SQL queries
22 def key_exists(key: str) -> bool:
25 def set_all(key: str, rows: list, value: any):
26 # DEBUG: print(f"DEBUG: key='{key}',rows()={len(rows)},value[]='{type(value)}' - CALLED!")
27 if not isinstance(key, str):
28 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
29 elif not key_exists(key):
30 # DEBUG: print(f"DEBUG: Cache for key='{key}' not initialized.")
34 # DEBUG: print(f"DEBUG: Setting key='{key}',sub[{type(sub)}]='{sub}'")
35 if isinstance(sub, tuple):
36 # DEBUG: print(f"DEBUG: Setting key='{key}',sub[{type(sub)}]='{sub}',value[]='{type(value)}'")
37 _cache[key][sub[0]] = value
39 print(f"WARNING: Unsupported type sub[]='{type(sub)}'")
41 # DEBUG: print("DEBUG: EXIT!")
43 def set_sub_key(key: str, sub: str, value: any):
44 # DEBUG: print(f"DEBUG: key='{key}',sub='{sub}',value[]='{type(value)}' - CALLED!")
45 if not isinstance(key, str):
46 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
47 elif not isinstance(sub, str):
48 raise ValueError("Parameter sub[]='{type(sub)}' is not 'str'")
49 elif not key_exists(key):
50 raise Exception(f"Cache for key='{key}' is not initialized, but function invoked")
52 # DEBUG: print(f"DEBUG: Setting key='{key}',sub='{sub}',value[]='{type(value)}' ...")
53 _cache[key][sub] = value
55 # DEBUG: print("DEBUG: EXIT!")
57 def sub_key_exists(key: str, sub: str) -> bool:
58 # DEBUG: print(f"DEBUG: key='{key}',sub='{sub}' - CALLED!")
59 if not isinstance(key, str):
60 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
61 elif not isinstance(sub, str):
62 raise ValueError("Parameter sub[]='{type(sub)}' is not 'str'")
63 elif not key_exists(key):
64 raise Exception(f"Cache for key='{key}' is not initialized, but function invoked")
66 exists = sub in _cache[key]
68 # DEBUG: print(f"DEBUG: exists='{exists}' - EXIT!")