fix(metastore-cache): prune before add (#29301)

This commit is contained in:
Ville Brofeldt 2024-06-20 14:59:32 +03:00 committed by GitHub
parent 99fc04b9a6
commit 172ddb47d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View File

@ -101,6 +101,7 @@ class SupersetMetastoreCache(BaseCache):
from superset.commands.key_value.create import CreateKeyValueCommand
try:
self._prune()
CreateKeyValueCommand(
resource=RESOURCE,
value=value,
@ -108,7 +109,6 @@ class SupersetMetastoreCache(BaseCache):
key=self.get_key(key),
expires_on=self._get_expiry(timeout),
).run()
self._prune()
return True
except KeyValueCreateFailedError:
return False

View File

@ -77,16 +77,29 @@ def test_caching_flow(app_context: AppContext, cache: SupersetMetastoreCache) ->
def test_expiry(app_context: AppContext, cache: SupersetMetastoreCache) -> None:
delta = timedelta(days=90)
dttm = datetime(2022, 3, 18, 0, 0, 0)
# 1. initialize cached values, ensure they're found
with freeze_time(dttm):
cache.set(FIRST_KEY, FIRST_KEY_INITIAL_VALUE, int(delta.total_seconds()))
assert (
cache.set(FIRST_KEY, FIRST_KEY_INITIAL_VALUE, int(delta.total_seconds()))
is True
)
assert cache.get(FIRST_KEY) == FIRST_KEY_INITIAL_VALUE
# 2. ensure cached values are available a moment before expiration
with freeze_time(dttm + delta - timedelta(seconds=1)):
assert cache.has(FIRST_KEY)
assert cache.has(FIRST_KEY) is True
assert cache.get(FIRST_KEY) == FIRST_KEY_INITIAL_VALUE
# 3. ensure cached entries expire
with freeze_time(dttm + delta + timedelta(seconds=1)):
assert cache.has(FIRST_KEY) is False
assert cache.get(FIRST_KEY) is None
# adding a value with the same key as an expired entry works
assert cache.add(FIRST_KEY, SECOND_VALUE, int(delta.total_seconds())) is True
assert cache.get(FIRST_KEY) == SECOND_VALUE
@pytest.mark.parametrize(
"input_,codec,expected_result",