2121query_log_max_length = 300
2222
2323
24+ cache_key = "query_cache" # the key to lookup the query_cache folder in dj.config
25+
26+
2427def get_host_hook (host_input ):
2528 if "://" in host_input :
2629 plugin_name = host_input .split ("://" )[0 ]
@@ -220,7 +223,7 @@ def connect(self):
220223 k : v
221224 for k , v in self .conn_info .items ()
222225 if k not in ["ssl_input" , "host_input" ]
223- }
226+ },
224227 )
225228 except client .err .InternalError :
226229 self ._conn = client .connect (
@@ -236,7 +239,7 @@ def connect(self):
236239 or k == "ssl"
237240 and self .conn_info ["ssl_input" ] is None
238241 )
239- }
242+ },
240243 )
241244 self ._conn .autocommit (True )
242245
@@ -254,13 +257,12 @@ def set_query_cache(self, query_cache=None):
254257 def purge_query_cache (self ):
255258 """Purges all query cache."""
256259 if (
257- "query_cache" in config
258- and isinstance (config ["query_cache" ], str )
259- and pathlib .Path (config ["query_cache" ]).is_dir ()
260+ isinstance (config .get (cache_key ), str )
261+ and pathlib .Path (config [cache_key ]).is_dir ()
260262 ):
261- path_iter = pathlib .Path (config ["query_cache" ]).glob ( "**/*" )
262- for path in path_iter :
263- path .unlink ()
263+ for path in pathlib .Path (config [cache_key ]).iterdir ():
264+ if not path . is_dir () :
265+ path .unlink ()
264266
265267 def close (self ):
266268 self ._conn .close ()
@@ -313,15 +315,15 @@ def query(
313315 "Only SELECT queries are allowed when query caching is on."
314316 )
315317 if use_query_cache :
316- if not config ["query_cache" ]:
318+ if not config [cache_key ]:
317319 raise errors .DataJointError (
318- "Provide filepath dj.config['query_cache '] when using query caching."
320+ f "Provide filepath dj.config['{ cache_key } '] when using query caching."
319321 )
320322 hash_ = uuid_from_buffer (
321323 (str (self ._query_cache ) + re .sub (r"`\$\w+`" , "" , query )).encode ()
322324 + pack (args )
323325 )
324- cache_path = pathlib .Path (config ["query_cache" ]) / str (hash_ )
326+ cache_path = pathlib .Path (config [cache_key ]) / str (hash_ )
325327 try :
326328 buffer = cache_path .read_bytes ()
327329 except FileNotFoundError :
0 commit comments