This is an automated email from the ASF dual-hosted git repository.

villebro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 172ddb47d5 fix(metastore-cache): prune before add (#29301)
172ddb47d5 is described below

commit 172ddb47d54749db781ea6443bd29a2d650efb99
Author: Ville Brofeldt <[email protected]>
AuthorDate: Thu Jun 20 14:59:32 2024 +0300

    fix(metastore-cache): prune before add (#29301)
---
 superset/extensions/metastore_cache.py                  |  2 +-
 .../extensions/metastore_cache_test.py                  | 17 +++++++++++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/superset/extensions/metastore_cache.py 
b/superset/extensions/metastore_cache.py
index 435c38ced8..1c89e84597 100644
--- a/superset/extensions/metastore_cache.py
+++ b/superset/extensions/metastore_cache.py
@@ -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
diff --git a/tests/integration_tests/extensions/metastore_cache_test.py 
b/tests/integration_tests/extensions/metastore_cache_test.py
index 2909e978f1..c69340a7a2 100644
--- a/tests/integration_tests/extensions/metastore_cache_test.py
+++ b/tests/integration_tests/extensions/metastore_cache_test.py
@@ -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",

Reply via email to