Skip to content

Commit 54543e3

Browse files
committed
[CacheClient] add reset_cached_values
tmp tmp tmp
1 parent 4376f40 commit 54543e3

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

octobot_commons/databases/cache_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,31 @@ async def set_cached_values(
231231
if flush_if_necessary and self._flush_cache_when_necessary and cache:
232232
await cache.flush()
233233

234+
async def reset_cached_values(
235+
self,
236+
value_keys: list,
237+
flush_if_necessary=False,
238+
tentacle_name=None,
239+
config_name=None,
240+
):
241+
"""
242+
Reset value_keys on the current cache
243+
:param value_keys: identifiers of the values
244+
:param flush_if_necessary: flush the cache after set (write into database)
245+
:param tentacle_name: name of the tentacle to get cache from
246+
:param config_name: name of the tentacle configuration as used in nested tentacle calls
247+
:return: None
248+
"""
249+
cache = None
250+
try:
251+
cache = self.get_cache(tentacle_name=tentacle_name, config_name=config_name)
252+
await cache.reset_values(
253+
value_keys=value_keys,
254+
)
255+
finally:
256+
if flush_if_necessary and self._flush_cache_when_necessary and cache:
257+
await cache.flush()
258+
234259
def ensure_no_missing_cached_value(self, is_missing):
235260
"""
236261
Raises NoCacheValue when is_missing is True

octobot_commons/databases/implementations/cache_timestamp_database.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,27 @@ async def _bulk_update_values(self, timestamps, to_bulk_update):
175175
f"Error on the {key} values"
176176
)
177177

178+
async def reset_values(self, value_keys: list[str]) -> None:
179+
"""
180+
Reset value_keys on the current cache
181+
:param value_keys: identifiers of the values
182+
:return: None
183+
"""
184+
await self._ensure_metadata()
185+
await self._ensure_local_cache(
186+
commons_enums.CacheDatabaseColumns.TIMESTAMP.value
187+
)
188+
try:
189+
for row in self._local_cache.values():
190+
for value_key in value_keys:
191+
if value_key in row:
192+
del row[value_key]
193+
await self._update_full_database()
194+
except Exception as error:
195+
raise RuntimeError(
196+
f"Failed to clear cache for value keys: {value_keys}"
197+
) from error
198+
178199
async def _update_full_database(self):
179200
# to be called to avoid multiple upsert / update which can be very slow: take full advantage of multiple inserts
180201
# 1. recreate all database elements from self._local_cache

0 commit comments

Comments
 (0)