Skip to content

Commit 2a739a0

Browse files
committed
One more fix for broken JalAsset (after cache change)
1 parent a408496 commit 2a739a0

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

jal/db/asset.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def add_symbol(self, symbol: str, currency_id: int=None, note: str='', data_sour
129129
if existing['quote_source'] == MarketDataFeed.NA:
130130
_ = self._exec("UPDATE asset_tickers SET quote_source=:data_source WHERE id=:id",
131131
[(":data_source", data_source), (":id", existing['id'])])
132+
self._data = self.db_cache.update_data(self._load_asset_data, (self._id,)) # Reload asset data from DB
132133

133134
# Returns country object for the asset
134135
def country(self) -> JalCountry:
@@ -254,7 +255,7 @@ def set_tag(self, tag_id: int) -> None:
254255
"VALUES(:asset_id, :datatype, :expiry)",
255256
[(":asset_id", self._id), (":datatype", AssetData.Tag), (":expiry", str(tag_id))])
256257
self._tag = JalTag(tag_id)
257-
self._fetch_data()
258+
self._data = self.db_cache.update_data(self._load_asset_data, (self._id,)) # Reload asset data from DB
258259

259260
# Updates relevant asset data fields with information provided in data dictionary
260261
def update_data(self, data: dict) -> None:
@@ -274,7 +275,7 @@ def update_data(self, data: dict) -> None:
274275
updaters[key](data[key])
275276
except KeyError: # No updater for this key is present
276277
continue
277-
self._fetch_data()
278+
self._data = self.db_cache.update_data(self._load_asset_data, (self._id,)) # Reload asset data from DB
278279

279280
def _update_isin(self, new_isin: str) -> None:
280281
if self._isin:
@@ -409,7 +410,8 @@ def get_assets(cls) -> list:
409410
assets = []
410411
query = cls._exec("SELECT id FROM assets")
411412
while query.next():
412-
assets.append(JalAsset(super(JalAsset, JalAsset)._read_record(query, cast=[int])))
413+
asset_id = cls._read_record(query, cast=[int])
414+
assets.append(JalAsset(asset_id))
413415
return assets
414416

415417
# Method returns a list of JalAsset objects that describe currencies defined in ledger

jal/universal_cache.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def get_data(
3434

3535
return self._cache[key]
3636

37+
def update_data(
38+
self,
39+
func: Callable,
40+
args: Tuple[Hashable, ...] = ()
41+
) -> Any:
42+
"""
43+
Invalidates old cache entry and executes function to have new data cached.
44+
45+
:param func: Function to cache/execute
46+
:param args: Function arguments (must be hashable)
47+
:return: Function result from new execution
48+
"""
49+
self.invalidate(func, args)
50+
return self.get_data(func, args)
51+
3752
def invalidate(self, func: Callable, args: Tuple[Hashable, ...] = ()) -> None:
3853
"""Delete a specific cache entry"""
3954
key = self._create_key(func, args)

0 commit comments

Comments
 (0)