Package com.tnsai.llm.cache
Class InMemorySemanticCache
java.lang.Object
com.tnsai.llm.cache.InMemorySemanticCache
- All Implemented Interfaces:
SemanticCache
In-memory implementation of SemanticCache.
InMemorySemanticCache stores cached entries in a ConcurrentHashMap and performs linear search for similarity matching. For production with large caches, consider using a vector database.
Features
- Thread-safe concurrent access
- Configurable TTL (time-to-live)
- LRU eviction when max size exceeded
- Detailed statistics tracking
Usage Example
SemanticCache cache = InMemorySemanticCache.builder()
.embeddingProvider(new HashEmbeddingProvider())
.ttlSeconds(3600)
.maxEntries(1000)
.build();
// Store
cache.put("What is Java?", "Java is a programming language...");
// Retrieve
Optional<CacheEntry> hit = cache.findSimilar("Java programming?", 0.8);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for InMemorySemanticCache.Nested classes/interfaces inherited from interface com.tnsai.llm.cache.SemanticCache
SemanticCache.CacheStats, SemanticCache.SimilarityResult -
Method Summary
Modifier and TypeMethodDescriptionbuilder()voidclear()Clears all entries from the cache.findAllSimilar(String prompt, double minSimilarity, int maxResults) Finds all similar entries above the threshold.findSimilar(String prompt, double minSimilarity) Finds the most similar cached entry above the threshold.findSimilar(String prompt, Optional<String> systemPrompt, double minSimilarity) Finds the most similar cached entry with system prompt consideration.Gets an entry by exact prompt hash.getStats()Returns cache statistics.Stores a prompt-response pair in the cache.Stores a prompt-response pair with system prompt.booleanRemoves an entry from the cache.voidResets cache statistics.intsize()Returns the current number of entries.
-
Method Details
-
builder
-
put
Description copied from interface:SemanticCacheStores a prompt-response pair in the cache.- Specified by:
putin interfaceSemanticCache- Parameters:
prompt- The original promptresponse- The LLM response- Returns:
- The created cache entry
-
put
Description copied from interface:SemanticCacheStores a prompt-response pair with system prompt.- Specified by:
putin interfaceSemanticCache- Parameters:
prompt- The user promptsystemPrompt- The system promptresponse- The LLM response- Returns:
- The created cache entry
-
findSimilar
Description copied from interface:SemanticCacheFinds the most similar cached entry above the threshold.- Specified by:
findSimilarin interfaceSemanticCache- Parameters:
prompt- The prompt to search forminSimilarity- Minimum similarity threshold (0-1)- Returns:
- The most similar entry if found, empty otherwise
-
findSimilar
public Optional<CacheEntry> findSimilar(String prompt, Optional<String> systemPrompt, double minSimilarity) Description copied from interface:SemanticCacheFinds the most similar cached entry with system prompt consideration.- Specified by:
findSimilarin interfaceSemanticCache- Parameters:
prompt- The user promptsystemPrompt- The system promptminSimilarity- Minimum similarity threshold- Returns:
- The most similar entry if found
-
findAllSimilar
public List<SemanticCache.SimilarityResult> findAllSimilar(String prompt, double minSimilarity, int maxResults) Description copied from interface:SemanticCacheFinds all similar entries above the threshold.- Specified by:
findAllSimilarin interfaceSemanticCache- Parameters:
prompt- The prompt to search forminSimilarity- Minimum similarity thresholdmaxResults- Maximum number of results- Returns:
- List of similar entries sorted by similarity (descending)
-
getByHash
Description copied from interface:SemanticCacheGets an entry by exact prompt hash.- Specified by:
getByHashin interfaceSemanticCache- Parameters:
promptHash- The prompt hash- Returns:
- The entry if found
-
remove
Description copied from interface:SemanticCacheRemoves an entry from the cache.- Specified by:
removein interfaceSemanticCache- Parameters:
id- The entry ID- Returns:
- true if entry was removed
-
clear
public void clear()Description copied from interface:SemanticCacheClears all entries from the cache.- Specified by:
clearin interfaceSemanticCache
-
size
public int size()Description copied from interface:SemanticCacheReturns the current number of entries.- Specified by:
sizein interfaceSemanticCache
-
getStats
Description copied from interface:SemanticCacheReturns cache statistics.- Specified by:
getStatsin interfaceSemanticCache
-
resetStats
public void resetStats()Description copied from interface:SemanticCacheResets cache statistics.- Specified by:
resetStatsin interfaceSemanticCache
-