Skip to content

Commit

Permalink
[CacheClient] add reset_cached_values
Browse files Browse the repository at this point in the history
tmp


tmp


tmp


tmp


tmp
  • Loading branch information
techfreaque committed Mar 9, 2023
1 parent 4376f40 commit a6ffcc0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
25 changes: 25 additions & 0 deletions octobot_commons/databases/cache_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,31 @@ async def set_cached_values(
if flush_if_necessary and self._flush_cache_when_necessary and cache:
await cache.flush()

async def reset_cached_values(
self,
value_keys: list,
flush_if_necessary=False,
tentacle_name=None,
config_name=None,
):
"""
Reset value_keys on the current cache
:param value_keys: identifiers of the values
:param flush_if_necessary: flush the cache after set (write into database)
:param tentacle_name: name of the tentacle to get cache from
:param config_name: name of the tentacle configuration as used in nested tentacle calls
:return: None
"""
cache = None
try:
cache = self.get_cache(tentacle_name=tentacle_name, config_name=config_name)
await cache.reset_values(
value_keys=value_keys,
)
finally:
if flush_if_necessary and self._flush_cache_when_necessary and cache:
await cache.flush()

def ensure_no_missing_cached_value(self, is_missing):
"""
Raises NoCacheValue when is_missing is True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.

import typing
import octobot_commons.databases.implementations.cache_database as cache_database
import octobot_commons.enums as commons_enums
import octobot_commons.errors as errors
Expand Down Expand Up @@ -175,6 +177,27 @@ async def _bulk_update_values(self, timestamps, to_bulk_update):
f"Error on the {key} values"
)

async def reset_values(self, value_keys: typing.List[str]) -> None:
"""
Reset value_keys on the current cache
:param value_keys: identifiers of the values
:return: None
"""
await self._ensure_metadata()
await self._ensure_local_cache(
commons_enums.CacheDatabaseColumns.TIMESTAMP.value
)
try:
for row in self._local_cache.values():
for value_key in value_keys:
if value_key in row:
del row[value_key]
await self._update_full_database()
except Exception as error:
raise RuntimeError(
f"Failed to clear cache for value keys: {value_keys}"
) from error

async def _update_full_database(self):
# to be called to avoid multiple upsert / update which can be very slow: take full advantage of multiple inserts
# 1. recreate all database elements from self._local_cache
Expand Down

0 comments on commit a6ffcc0

Please sign in to comment.