|
22 | 22 | query_log_max_length = 300 |
23 | 23 |
|
24 | 24 |
|
| 25 | +cache_key = "query_cache" # the key to lookup the query_cache folder in dj.config |
| 26 | + |
| 27 | + |
25 | 28 | def get_host_hook(host_input): |
26 | 29 | if "://" in host_input: |
27 | 30 | plugin_name = host_input.split("://")[0] |
@@ -255,11 +258,13 @@ def set_query_cache(self, query_cache=None): |
255 | 258 | def purge_query_cache(self): |
256 | 259 | """Purges all query cache.""" |
257 | 260 | if ( |
258 | | - "query_cache" in config |
259 | | - and isinstance(config["query_cache"], str) |
260 | | - and pathlib.Path(config["query_cache"]).is_dir() |
| 261 | + cache_key in config |
| 262 | + and isinstance(config[cache_key], str) |
| 263 | + and pathlib.Path(config[cache_key]).is_dir() |
261 | 264 | ): |
262 | | - shutil.rmtree() |
| 265 | + for path in pathlib.Path(config[cache_key].iterdir()): |
| 266 | + if not path.is_dir(): |
| 267 | + path.unlink() |
263 | 268 |
|
264 | 269 | def close(self): |
265 | 270 | self._conn.close() |
@@ -312,15 +317,15 @@ def query( |
312 | 317 | "Only SELECT queries are allowed when query caching is on." |
313 | 318 | ) |
314 | 319 | if use_query_cache: |
315 | | - if not config["query_cache"]: |
| 320 | + if not config[cache_key]: |
316 | 321 | raise errors.DataJointError( |
317 | | - "Provide filepath dj.config['query_cache'] when using query caching." |
| 322 | + f"Provide filepath dj.config['{cache_key}'] when using query caching." |
318 | 323 | ) |
319 | 324 | hash_ = uuid_from_buffer( |
320 | 325 | (str(self._query_cache) + re.sub(r"`\$\w+`", "", query)).encode() |
321 | 326 | + pack(args) |
322 | 327 | ) |
323 | | - cache_path = pathlib.Path(config["query_cache"]) / str(hash_) |
| 328 | + cache_path = pathlib.Path(config[cache_key]) / str(hash_) |
324 | 329 | try: |
325 | 330 | buffer = cache_path.read_bytes() |
326 | 331 | except FileNotFoundError: |
|
0 commit comments