Skip to content

Commit 810d85f

Browse files
committed
enh: think more about caching in the new design
1 parent bffed66 commit 810d85f

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

dcoraid/dbmodel/db_api_cached.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,21 @@ def local_version_score(self):
5757
def close(self):
5858
self._mc.close()
5959

60-
def get_circles(self, refresh=False):
60+
def get_circles(self):
6161
"""Return the list of DCOR Circle names
6262
"""
63-
clist = self._cache_fleeting["circles"]
64-
if not clist or refresh:
63+
# TODO: Use self._mc to extract circles
64+
clist = self._cache_fleeting.setdefault("circles", [])
65+
if not clist:
6566
clist.clear()
6667
clist += self.ai.get_circles()
6768
return clist
6869

69-
def get_collections(self, refresh=False):
70+
def get_collections(self):
7071
"""Return the list of DCOR Collection names"""
71-
clist = self._cache_fleeting["collections"]
72-
if not clist or refresh:
72+
# TODO: Use self._mc to extract collections
73+
clist = self._cache_fleeting.setdefault("collections", [])
74+
if not clist:
7375
clist.clear()
7476
clist += self.ai.get_collections()
7577
return clist
@@ -80,7 +82,10 @@ def get_dataset_dict(self, ds_id):
8082
def get_datasets_user_following(self) -> DBExtract:
8183
"""Return datasets the user is following"""
8284
# TODO: Use datasets in self._mc
83-
return self.ai.get_datasets_user_following()
85+
ckey = "get_datasets_user_following"
86+
if self._cache_fleeting.get(ckey) is None:
87+
self._cache_fleeting[ckey] = self.ai.get_datasets_user_following()
88+
return self._cache_fleeting[ckey]
8489

8590
def get_datasets_user_owned(self) -> DBExtract:
8691
"""Return datasets the user created"""
@@ -92,7 +97,10 @@ def get_datasets_user_owned(self) -> DBExtract:
9297
def get_datasets_user_shared(self) -> DBExtract:
9398
"""Return datasets shared with the user"""
9499
# TODO: Use datasets in self._mc
95-
return self.ai.get_datasets_user_shared()
100+
ckey = "get_datasets_user_shared"
101+
if self._cache_fleeting.get(ckey) is None:
102+
self._cache_fleeting[ckey] = self.ai.get_datasets_user_shared()
103+
return self._cache_fleeting[ckey]
96104

97105
def get_users(self):
98106
"""Return the list of DCOR users"""
@@ -119,15 +127,18 @@ def search_dataset(self, query="", limit=100):
119127

120128
def update(self, reset=False, abort_event=None):
121129
"""Update the local metadata cache based on the last local timestamp"""
130+
# Clear the fleeting cache.
131+
self._cache_fleeting.clear()
122132
if self.remote_version_score != self.local_version_score:
123133
reset = True
124134

125135
if reset:
126136
self.local_timestamp = 0
127137
self._mc.reset()
128138

129-
self.get_circles(refresh=True)
130-
self.get_collections(refresh=True)
139+
# Call these methods now so they reflect the current database state.
140+
self.get_circles()
141+
self.get_collections()
131142

132143
new_timestamp = time.time()
133144

@@ -147,4 +158,5 @@ def update(self, reset=False, abort_event=None):
147158

148159
def update_dataset(self, ds_dict):
149160
"""Update a single dataset in the database without calling `update`"""
161+
self._cache_fleeting.clear()
150162
self._mc.upsert_dataset(ds_dict)

dcoraid/gui/panel_my_data/widget_my_data.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,21 @@ def __init__(self, *args, **kwargs):
4141
def on_added_datasets_to_collection(self, collection, dataset_ids):
4242
"""User manually added a bunch of datasets to a collection"""
4343
# Get the collection
44-
for col in self.database.get_collections(refresh=True):
44+
for col in self.database.get_collections():
4545
if col == collection["name"]:
4646
cid = collection["id"]
4747
break
4848
else:
49-
raise KeyError(f"Collection {collection['id']} not found!")
49+
# we have to reset the database and try again
50+
self.database.update(reset=True)
51+
for col in self.database.get_collections():
52+
if col == collection["name"]:
53+
cid = collection["id"]
54+
break
55+
else:
56+
raise ValueError(
57+
f"Could not, with the best will in the world, "
58+
f"find this collection: '{collection['name']}'")
5059
# Append it to each dataset
5160
for ds_id in dataset_ids:
5261
ds_dict = self.database.get_dataset_dict(ds_id)
@@ -57,6 +66,7 @@ def on_added_datasets_to_collection(self, collection, dataset_ids):
5766
else:
5867
ds_dict["groups"].append(collection)
5968
self.database.update_dataset(ds_dict)
69+
self.on_update_view()
6070

6171
@QtCore.pyqtSlot()
6272
def on_update_db(self):

tests/test_gui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_gui_mydata_dataset_add_to_collection(mw, qtbot):
140140
with mock.patch.object(
141141
QInputDialog, "getItem",
142142
return_value=(f"{ii}: {item['display_name']}", True)):
143-
qtbot.mouseClick(fw_datasets.toolButton_custom,
143+
qtbot.mouseClick(fw_datasets.pushButton_custom,
144144
QtCore.Qt.MouseButton.LeftButton)
145145

146146
# Check whether dataset is in collection in database

0 commit comments

Comments
 (0)