Package com.redis.vl.extensions.cache
Class LangCacheSemanticCache
java.lang.Object
com.redis.vl.extensions.cache.LangCacheSemanticCache
LLM Cache implementation using the LangCache managed service.
This cache uses the LangCache API service for semantic caching of LLM responses. It requires a LangCache account and API key.
Port of redis-vl-python/redisvl/extensions/cache/llm/langcache.py
Example:
LangCacheSemanticCache cache = new LangCacheSemanticCache.Builder()
.name("my_cache")
.serverUrl("https://aws-us-east-1.langcache.redis.io")
.cacheId("your-cache-id")
.apiKey("your-api-key")
.ttl(3600)
.build();
// Store a response
cache.store(
"What is the capital of France?",
"Paris",
Map.of("topic", "geography")
);
// Check for cached responses
List<Map<String, Object>> results = cache.check("What is the capital of France?");
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for LangCacheSemanticCache. -
Method Summary
Modifier and TypeMethodDescriptioncheck(String prompt, Map<String, Object> attributes, int numResults, List<String> returnFields, Object filterExpression, Float distanceThreshold) Check the cache for semantically similar prompts.voidclear()Clear the cache of all entries.voiddelete()Delete the entire cache.deleteByAttributes(Map<String, Object> attributes) Delete cache entries matching the given attributes.voiddeleteById(String entryId) Delete a single cache entry by ID.voidflush()Flush the entire cache.getName()Get the cache name.getTtl()Get the TTL for cache entries.Store a prompt-response pair in the cache.Store a prompt-response pair in the cache with a per-entry TTL.voidUpdate specific fields within an existing cache entry.
-
Method Details
-
getName
Get the cache name.- Returns:
- The cache name
-
getTtl
Get the TTL for cache entries.- Returns:
- Time-to-live in seconds (null if no expiration)
-
store
Store a prompt-response pair in the cache.- Parameters:
prompt- The user prompt to cacheresponse- The LLM response to cachemetadata- Optional metadata (stored as attributes in LangCache)- Returns:
- The entry ID for the cached entry
- Throws:
IOException- If the API request fails
-
store
public String store(String prompt, String response, Map<String, Object> metadata, Integer ttl) throws IOExceptionStore a prompt-response pair in the cache with a per-entry TTL.Port of redis-vl-python PR #442
- Parameters:
prompt- The user prompt to cacheresponse- The LLM response to cachemetadata- Optional metadata (stored as attributes in LangCache)ttl- Time-to-live in seconds (null for default TTL)- Returns:
- The entry ID for the cached entry
- Throws:
IOException- If the API request fails
-
check
public List<Map<String,Object>> check(String prompt, Map<String, Object> attributes, int numResults, List<String> returnFields, Object filterExpression, Float distanceThreshold) throws IOExceptionCheck the cache for semantically similar prompts.- Parameters:
prompt- The text prompt to search forattributes- LangCache attributes to filter bynumResults- Number of results to returnreturnFields- Not used (for compatibility)filterExpression- Not supported by LangCachedistanceThreshold- Maximum distance threshold (converted based on distance_scale)- Returns:
- List of matching cache entries
- Throws:
IOException- If the API request fails
-
delete
Delete the entire cache.This deletes all entries in the cache by calling the flush API.
- Throws:
IOException- If the API request fails
-
flush
Flush the entire cache.This deletes all entries in the cache using the dedicated flush endpoint.
- Throws:
IOException- If the API request fails
-
clear
Clear the cache of all entries.This is an alias for delete() to match the BaseCache interface.
- Throws:
IOException- If the API request fails
-
deleteById
Delete a single cache entry by ID.- Parameters:
entryId- The ID of the entry to delete- Throws:
IOException- If the API request fails
-
deleteByAttributes
Delete cache entries matching the given attributes.- Parameters:
attributes- Attributes to match for deletion- Returns:
- Result of the deletion operation
- Throws:
IOException- If the API request fails
-
update
Update specific fields within an existing cache entry.Note: LangCache API does not support updating individual entries. This method will throw UnsupportedOperationException.
- Parameters:
key- The key of the document to updatefield- The field to updatevalue- The new value- Throws:
UnsupportedOperationException- LangCache does not support entry updates
-