Class SemanticCache

java.lang.Object
com.redis.vl.extensions.cache.BaseCache
com.redis.vl.extensions.cache.SemanticCache

public class SemanticCache extends BaseCache
Semantic cache for LLM responses using vector similarity. Port of redis-vl-python/redisvl/extensions/cache/llm/semantic.py
  • Method Details

    • store

      public void store(String prompt, String response)
      Store a prompt-response pair in the cache.
      Parameters:
      prompt - The prompt text
      response - The response text
    • store

      public void store(String prompt, String response, Map<String,Object> metadata)
      Store a prompt-response pair with metadata.
      Parameters:
      prompt - The prompt text
      response - The response text
      metadata - Additional metadata
    • storeWithTTL

      public void storeWithTTL(String prompt, String response, Map<String,Object> metadata, int ttl)
      Store with custom TTL.
      Parameters:
      prompt - The prompt text
      response - The response text
      metadata - Additional metadata
      ttl - Time-to-live in seconds
    • check

      public Optional<CacheHit> check(String prompt)
      Check for a semantically similar prompt in the cache.
      Parameters:
      prompt - The prompt to check
      Returns:
      Optional containing the cache hit if found
    • check

      public Optional<CacheHit> check(String prompt, Filter filter)
      Check for a semantically similar prompt with filtering.
      Parameters:
      prompt - The prompt to check
      filter - Optional filter expression
      Returns:
      Optional containing the cache hit if found
    • checkTopK

      public List<CacheHit> checkTopK(String prompt, int k)
      Get top-k similar results.
      Parameters:
      prompt - The prompt to check
      k - Number of results to return
      Returns:
      List of cache hits sorted by distance
    • update

      public void update(String prompt, String newResponse)
      Update an existing cache entry.
      Parameters:
      prompt - The prompt to update
      newResponse - The new response
    • storeBatch

      public void storeBatch(List<PromptResponsePair> pairs)
      Batch store multiple prompt-response pairs.
      Parameters:
      pairs - List of prompt-response pairs
    • checkBatch

      public List<Optional<CacheHit>> checkBatch(List<String> prompts)
      Batch check multiple prompts.
      Parameters:
      prompts - List of prompts to check
      Returns:
      List of optional cache hits
    • clear

      public void clear(Filter filter)
      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.