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!")
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}'")
36 if isinstance(sub, tuple):
37 _cache[key][sub[0]] = value
39 print(f"WARNING: Unsupported type row[]='{type(row)}'")
41 # DEBUG: print("DEBUG: EXIT!")
43 def set_sub_key(key: str, sub: str, value: any):
45 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
46 elif type(sub) != str:
47 raise ValueError("Parameter sub[]='{type(sub)}' is not 'str'")
48 elif not key_exists(key):
49 print(f"WARNING: Bad method call, key='{key}' is not initialized yet.")
50 raise Exception(f"Cache for key='{key}' is not initialized, but function invoked")
52 _cache[key][sub] = value
54 def sub_key_exists(key: str, sub: str) -> bool:
56 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
57 elif type(sub) != str:
58 raise ValueError("Parameter sub[]='{type(sub)}' is not 'str'")
59 elif not key_exists(key):
60 print(f"WARNING: Bad method call, key='{key}' is not initialized yet.")
61 raise Exception(f"Cache for key='{key}' is not initialized, but function invoked")
63 return sub in _cache[key]