Interface MemoryStore
- All Known Implementing Classes:
InMemoryStore
Provides persistent and volatile storage for agent memories with support for:
- Short-term (volatile) and long-term (persistent) memory scopes
- Full-text search and metadata filtering
- Shared memory across multiple agents
- Automatic expiration of time-limited memories
All operations are asynchronous and return CompletableFuture for
non-blocking execution.
Thread Safety: All implementations must be thread-safe.
Implementation Notes:
- SHORT_TERM memories may be stored in-memory for fast access
- LONG_TERM memories should be persisted to durable storage
- Expired entries should be automatically removed during queries
- Search operations should support both exact and fuzzy matching
Example usage:
MemoryStore store = new InMemoryStore();
// Store a memory
MemoryEntry entry = MemoryEntry.builder("User prefers email notifications")
.ownerId("notification-agent")
.metadata("preference", "email")
.build();
store.store("pref:user123:notification", entry, MemoryScope.LONG_TERM)
.thenRun(() -> System.out.println("Memory stored"));
// Retrieve a memory
store.retrieve("pref:user123:notification", MemoryScope.LONG_TERM)
.thenAccept(opt -> opt.ifPresent(e ->
System.out.println("Found: " + e.content())));
// Search memories
MemoryQuery query = MemoryQuery.builder()
.text("notification")
.scope(MemoryScope.LONG_TERM)
.ownerId("notification-agent")
.limit(10)
.build();
store.search(query)
.thenAccept(results ->
System.out.println("Found " + results.size() + " memories"));
- Since:
- 0.6.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionclear(MemoryScope scope) Clears all memory entries in the specified scope.delete(String key, MemoryScope scope) Deletes a memory entry.default CompletableFuture<Boolean> exists(String key, MemoryScope scope) Checks if a memory entry exists.default MemoryStatsgetStats()Gets statistics about memory usage.default StringGets the name of this memory store implementation.listKeys(MemoryScope scope) Lists all memory keys in the specified scope.retrieve(String key, MemoryScope scope) Retrieves a memory entry by key.search(MemoryQuery query) Searches for memory entries matching the query criteria.store(String key, MemoryEntry entry, MemoryScope scope) Stores a memory entry.
-
Method Details
-
store
Stores a memory entry.If an entry with the same key and scope already exists, it will be replaced.
- Parameters:
key- unique identifier for this memoryentry- the memory entry to storescope- the memory scope (SHORT_TERM or LONG_TERM)- Returns:
- a future that completes when the entry is stored
- Throws:
MemoryException- if the storage operation failsIllegalArgumentException- if key is null or blank
-
retrieve
Retrieves a memory entry by key.Expired entries are automatically filtered out and return empty.
- Parameters:
key- the memory keyscope- the memory scope to search in- Returns:
- a future containing the memory entry, or empty if not found
- Throws:
MemoryException- if the retrieval operation failsIllegalArgumentException- if key is null or blank
-
search
Searches for memory entries matching the query criteria.Results are filtered by:
- Text content (if specified)
- Owner ID (if specified)
- Metadata filters (if specified)
- Non-expired entries only
Results may be returned in any order unless the implementation specifies otherwise.
- Parameters:
query- the search query parameters- Returns:
- a future containing matching memory entries (may be empty)
- Throws:
MemoryException- if the search operation failsIllegalArgumentException- if query is null
-
delete
Deletes a memory entry.If the entry does not exist, this operation completes successfully without error (idempotent).
- Parameters:
key- the memory key to deletescope- the memory scope- Returns:
- a future that completes when the entry is deleted
- Throws:
MemoryException- if the deletion operation failsIllegalArgumentException- if key is null or blank
-
clear
Clears all memory entries in the specified scope.Warning: This operation cannot be undone. Use with caution.
- Parameters:
scope- the memory scope to clear- Returns:
- a future that completes when all entries are cleared
- Throws:
MemoryException- if the clear operation fails
-
listKeys
Lists all memory keys in the specified scope.This operation may be expensive for large memory stores. Consider using
search(MemoryQuery)with pagination instead.- Parameters:
scope- the memory scope- Returns:
- a future containing all keys (may be empty)
- Throws:
MemoryException- if the list operation fails
-
getStats
Gets statistics about memory usage.Statistics may be cached and not reflect the absolute current state.
- Returns:
- memory usage statistics
-
exists
Checks if a memory entry exists.This is more efficient than
retrieve(String, MemoryScope)when only existence needs to be checked.- Parameters:
key- the memory keyscope- the memory scope- Returns:
- a future containing true if the entry exists and is not expired
- Throws:
MemoryException- if the check operation fails
-
getStoreName
Gets the name of this memory store implementation.Used for logging and monitoring.
- Returns:
- the store name (e.g., "InMemoryStore", "DatabaseStore")
-