Class LangCacheSemanticCache

java.lang.Object
com.redis.vl.extensions.cache.LangCacheSemanticCache

public class LangCacheSemanticCache extends Object
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?");
 
  • Method Details

    • getName

      public String getName()
      Get the cache name.
      Returns:
      The cache name
    • getTtl

      public Integer getTtl()
      Get the TTL for cache entries.
      Returns:
      Time-to-live in seconds (null if no expiration)
    • store

      public String store(String prompt, String response, Map<String,Object> metadata) throws IOException
      Store a prompt-response pair in the cache.
      Parameters:
      prompt - The user prompt to cache
      response - The LLM response to cache
      metadata - 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 IOException
      Store 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 cache
      response - The LLM response to cache
      metadata - 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 IOException
      Check the cache for semantically similar prompts.
      Parameters:
      prompt - The text prompt to search for
      attributes - LangCache attributes to filter by
      numResults - Number of results to return
      returnFields - Not used (for compatibility)
      filterExpression - Not supported by LangCache
      distanceThreshold - Maximum distance threshold (converted based on distance_scale)
      Returns:
      List of matching cache entries
      Throws:
      IOException - If the API request fails
    • delete

      public void delete() throws IOException
      Delete the entire cache.

      This deletes all entries in the cache by calling the flush API.

      Throws:
      IOException - If the API request fails
    • flush

      public void flush() throws IOException
      Flush the entire cache.

      This deletes all entries in the cache using the dedicated flush endpoint.

      Throws:
      IOException - If the API request fails
    • clear

      public void clear() throws IOException
      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

      public void deleteById(String entryId) throws IOException
      Delete a single cache entry by ID.
      Parameters:
      entryId - The ID of the entry to delete
      Throws:
      IOException - If the API request fails
    • deleteByAttributes

      public Map<String,Object> deleteByAttributes(Map<String,Object> attributes) throws IOException
      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

      public void update(String key, String field, Object value)
      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 update
      field - The field to update
      value - The new value
      Throws:
      UnsupportedOperationException - LangCache does not support entry updates