Skip to content

Commit a943ad6

Browse files
Add purge utility for query caching and add test.
1 parent c5ac58e commit a943ad6

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

datajoint/connection.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,14 @@ def set_query_cache(self, query_cache=None):
228228
"""
229229
self._query_cache = query_cache
230230

231-
def purge_query_cache(self, query_cache):
232-
"""
233-
Purges if query cache is available with the provided reference.
234-
235-
:param query_cache: a string associated with the hash for query results
236-
"""
237-
pass # wip
231+
def purge_query_cache(self):
232+
""" Purges all query cache. """
233+
if 'query_cache' in config and isinstance(config['query_cache'], str) and \
234+
pathlib.Path(config['query_cache']).is_dir():
235+
path_iter = pathlib.Path(config['query_cache']).glob('**/*')
236+
for path in path_iter:
237+
path.unlink()
238+
self._query_cache = None
238239

239240
def close(self):
240241
self._conn.close()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
.. code-block:: python
33
4-
# deactivate query caching
5-
conn.purge_query_cache(query_cache='main')
4+
# purged the cached queries
5+
conn.purge_query_cache()
66

tests/test_fetch.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import warnings
88
from . import schema
99
import datajoint as dj
10+
import os
1011

1112

1213
class TestFetch:
@@ -254,3 +255,34 @@ def test_same_secondary_attribute(self):
254255
children = (schema.Child * schema.Parent().proj()).fetch()['name']
255256
assert len(children) == 1
256257
assert children[0] == 'Dan'
258+
259+
def test_query_caching(self):
260+
# initialize cache directory
261+
os.mkdir(os.path.expanduser('~/dj_query_cache'))
262+
263+
with dj.config(query_cache=os.path.expanduser('~/dj_query_cache')):
264+
# insert sample data and load cache
265+
schema.TTest3.insert([dict(key=100+i, value=200+i) for i in range(2)])
266+
dj.conn().set_query_cache(query_cache='main')
267+
cached_res = schema.TTest3().fetch()
268+
# attempt to insert while caching enabled
269+
try:
270+
schema.TTest3.insert([dict(key=200+i, value=400+i) for i in range(2)])
271+
assert False, 'Insert allowed which query caching enabled'
272+
except dj.DataJointError:
273+
dj.conn().set_query_cache()
274+
# insert new data
275+
schema.TTest3.insert([dict(key=600+i, value=800+i) for i in range(2)])
276+
# re-enable cache to access old results
277+
dj.conn().set_query_cache(query_cache='main')
278+
previous_cache = schema.TTest3().fetch()
279+
# verify properly cached and how to refresh results
280+
assert all([c == p for c, p in zip(cached_res, previous_cache)])
281+
dj.conn().set_query_cache()
282+
uncached_res = schema.TTest3().fetch()
283+
assert len(uncached_res) > len(cached_res)
284+
# purge query cache
285+
dj.conn().purge_query_cache()
286+
287+
# reset cache directory state (will fail if purge was unsuccessful)
288+
os.rmdir(os.path.expanduser('~/dj_query_cache'))

0 commit comments

Comments
 (0)