Skip to content

Commit a6ffcc0

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

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#
1414
# You should have received a copy of the GNU Lesser General Public
1515
# License along with this library.
16+
17+
import typing
1618
import octobot_commons.databases.implementations.cache_database as cache_database
1719
import octobot_commons.enums as commons_enums
1820
import octobot_commons.errors as errors
@@ -175,6 +177,27 @@ async def _bulk_update_values(self, timestamps, to_bulk_update):
175177
f"Error on the {key} values"
176178
)
177179

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

0 commit comments

Comments
 (0)