Package com.redis.vl.extensions.cache
Class SemanticCache
java.lang.Object
com.redis.vl.extensions.cache.BaseCache
com.redis.vl.extensions.cache.SemanticCache
Semantic cache for LLM responses using vector similarity. Port of
redis-vl-python/redisvl/extensions/cache/llm/semantic.py
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class com.redis.vl.extensions.cache.BaseCache
name, prefix, redisClient, ttl
-
Method Summary
Modifier and TypeMethodDescriptionCheck for a semantically similar prompt in the cache.Check for a semantically similar prompt with filtering.checkBatch
(List<String> prompts) Batch check multiple prompts.Get top-k similar results.void
Clear cache with optional filter.float
Get the current distance threshold.long
Get the hit count.float
Get the cache hit rate.long
Get the miss count.void
Reset statistics counters.void
setDistanceThreshold
(float threshold) Set the distance threshold for similarity matching.void
Store a prompt-response pair in the cache.void
Store a prompt-response pair with metadata.void
storeBatch
(List<PromptResponsePair> pairs) Batch store multiple prompt-response pairs.void
Store with custom TTL.void
Update an existing cache entry.Methods inherited from class com.redis.vl.extensions.cache.BaseCache
clear, disconnect, expire, finalize, getName, getPrefix, getTtl, isConnected, makeKey, setTtl, setWithTtl, setWithTtl, size
-
Method Details
-
store
Store a prompt-response pair in the cache.- Parameters:
prompt
- The prompt textresponse
- The response text
-
store
Store a prompt-response pair with metadata.- Parameters:
prompt
- The prompt textresponse
- The response textmetadata
- Additional metadata
-
storeWithTTL
Store with custom TTL.- Parameters:
prompt
- The prompt textresponse
- The response textmetadata
- Additional metadatattl
- Time-to-live in seconds
-
check
Check for a semantically similar prompt in the cache.- Parameters:
prompt
- The prompt to check- Returns:
- Optional containing the cache hit if found
-
check
Check for a semantically similar prompt with filtering.- Parameters:
prompt
- The prompt to checkfilter
- Optional filter expression- Returns:
- Optional containing the cache hit if found
-
checkTopK
Get top-k similar results.- Parameters:
prompt
- The prompt to checkk
- Number of results to return- Returns:
- List of cache hits sorted by distance
-
update
Update an existing cache entry.- Parameters:
prompt
- The prompt to updatenewResponse
- The new response
-
storeBatch
Batch store multiple prompt-response pairs.- Parameters:
pairs
- List of prompt-response pairs
-
checkBatch
Batch check multiple prompts.- Parameters:
prompts
- List of prompts to check- Returns:
- List of optional cache hits
-
clear
Clear cache with optional filter.- Parameters:
filter
- Optional filter expression
-
getDistanceThreshold
public float getDistanceThreshold()Get the current distance threshold.- Returns:
- The distance threshold
-
setDistanceThreshold
public void setDistanceThreshold(float threshold) Set the distance threshold for similarity matching.- Parameters:
threshold
- The new threshold (0 = exact match, 1 = any match)
-
getHitCount
public long getHitCount()Get the hit count.- Returns:
- Number of cache hits
-
getMissCount
public long getMissCount()Get the miss count.- Returns:
- Number of cache misses
-
getHitRate
public float getHitRate()Get the cache hit rate.- Returns:
- Hit rate as a percentage (0.0 to 1.0)
-
resetStatistics
public void resetStatistics()Reset statistics counters.
-