Skip to content

Commit ac464f3

Browse files
query cache is deleted non-recursively
1 parent 1924bb4 commit ac464f3

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

datajoint/connection.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
query_log_max_length = 300
2323

2424

25+
cache_key = "query_cache" # the key to lookup the query_cache folder in dj.config
26+
27+
2528
def get_host_hook(host_input):
2629
if "://" in host_input:
2730
plugin_name = host_input.split("://")[0]
@@ -255,11 +258,13 @@ def set_query_cache(self, query_cache=None):
255258
def purge_query_cache(self):
256259
"""Purges all query cache."""
257260
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()
261264
):
262-
shutil.rmtree()
265+
for path in pathlib.Path(config[cache_key].iterdir()):
266+
if not path.is_dir():
267+
path.unlink()
263268

264269
def close(self):
265270
self._conn.close()
@@ -312,15 +317,15 @@ def query(
312317
"Only SELECT queries are allowed when query caching is on."
313318
)
314319
if use_query_cache:
315-
if not config["query_cache"]:
320+
if not config[cache_key]:
316321
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."
318323
)
319324
hash_ = uuid_from_buffer(
320325
(str(self._query_cache) + re.sub(r"`\$\w+`", "", query)).encode()
321326
+ pack(args)
322327
)
323-
cache_path = pathlib.Path(config["query_cache"]) / str(hash_)
328+
cache_path = pathlib.Path(config[cache_key]) / str(hash_)
324329
try:
325330
buffer = cache_path.read_bytes()
326331
except FileNotFoundError:

0 commit comments

Comments
 (0)