Skip to content

Commit 042f7d8

Browse files
Improve caching ttl tests (#178)
Small improvement to the TTL tests for semantic cache. Based on feedback from an SA in the field.
1 parent 9ca93ef commit 042f7d8

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

redisvl/extensions/llmcache/semantic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ def delete(self) -> None:
185185

186186
def _refresh_ttl(self, key: str) -> None:
187187
"""Refresh the time-to-live for the specified key."""
188-
if self.ttl:
189-
self._index.client.expire(key, self.ttl) # type: ignore
188+
if self._ttl:
189+
self._index.client.expire(key, self._ttl) # type: ignore
190190

191191
def _vectorize_prompt(self, prompt: Optional[str]) -> List[float]:
192192
"""Converts a text prompt to its vector representation using the

tests/integration/test_llmcache.py

+33-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def cache(vectorizer, redis_url):
1919
vectorizer=vectorizer, distance_threshold=0.2, redis_url=redis_url
2020
)
2121
yield cache_instance
22-
cache_instance.clear() # Clear cache after each test
2322
cache_instance._index.delete(True) # Clean up index
2423

2524

@@ -37,7 +36,6 @@ def cache_with_ttl(vectorizer, redis_url):
3736
vectorizer=vectorizer, distance_threshold=0.2, ttl=2, redis_url=redis_url
3837
)
3938
yield cache_instance
40-
cache_instance.clear() # Clear cache after each test
4139
cache_instance._index.delete(True) # Clean up index
4240

4341

@@ -54,6 +52,24 @@ def cache_with_redis_client(vectorizer, client, redis_url):
5452
cache_instance._index.delete(True) # Clean up index
5553

5654

55+
# # Test handling invalid input for check method
56+
def test_bad_ttl(cache):
57+
with pytest.raises(ValueError):
58+
cache.set_ttl(2.5)
59+
60+
61+
def test_cache_ttl(cache_with_ttl):
62+
assert cache_with_ttl.ttl == 2
63+
cache_with_ttl.set_ttl(5)
64+
assert cache_with_ttl.ttl == 5
65+
66+
67+
def test_set_ttl(cache):
68+
assert cache.ttl == None
69+
cache.set_ttl(5)
70+
assert cache.ttl == 5
71+
72+
5773
# Test basic store and check functionality
5874
def test_store_and_check(cache, vectorizer):
5975
prompt = "This is a test prompt."
@@ -95,6 +111,21 @@ def test_ttl_expiration(cache_with_ttl, vectorizer):
95111
assert len(check_result) == 0
96112

97113

114+
def test_ttl_expiration_after_update(cache_with_ttl, vectorizer):
115+
prompt = "This is a test prompt."
116+
response = "This is a test response."
117+
vector = vectorizer.embed(prompt)
118+
cache_with_ttl.set_ttl(4)
119+
120+
assert cache_with_ttl.ttl == 4
121+
122+
cache_with_ttl.store(prompt, response, vector=vector)
123+
sleep(5)
124+
125+
check_result = cache_with_ttl.check(vector=vector)
126+
assert len(check_result) == 0
127+
128+
98129
# Test check behavior with no match
99130
def test_check_no_match(cache, vectorizer):
100131
vector = vectorizer.embed("Some random sentence.")
@@ -111,12 +142,6 @@ def test_check_invalid_input(cache):
111142
cache.check(prompt="test", return_fields="bad value")
112143

113144

114-
# Test handling invalid input for check method
115-
def test_bad_ttl(cache):
116-
with pytest.raises(ValueError):
117-
cache.set_ttl(2.5)
118-
119-
120145
# Test storing with metadata
121146
def test_store_with_metadata(cache, vectorizer):
122147
prompt = "This is another test prompt."

0 commit comments

Comments
 (0)