Class InMemoryStore

java.lang.Object
dev.agenor.runtime.memory.InMemoryStore
All Implemented Interfaces:
MemoryStore

public class InMemoryStore extends Object implements MemoryStore
In-memory implementation of MemoryStore.

This implementation:

  • Stores memories in concurrent hash maps (thread-safe)
  • Automatically cleans up expired entries
  • Provides fast lookups and searches
  • Does NOT persist to disk (volatile storage)

Suitable for:

  • Development and testing
  • Short-lived processes
  • Caching layer
  • Single-instance deployments

Example usage:


 MemoryStore store = new InMemoryStore();

 MemoryEntry entry = MemoryEntry.builder("Important fact")
     .ownerId("my-agent")
     .build();

 store.store("fact-1", entry, MemoryScope.SHORT_TERM).join();
 
Since:
0.6.0
  • Constructor Details

    • InMemoryStore

      public InMemoryStore()
      Creates an InMemoryStore with default configuration.
    • InMemoryStore

      public InMemoryStore(int maxEntriesPerScope, long cleanupIntervalSeconds)
      Creates an InMemoryStore with custom configuration.
      Parameters:
      maxEntriesPerScope - maximum entries per scope (0 = unlimited)
      cleanupIntervalSeconds - interval for cleanup task in seconds
  • Method Details

    • store

      public CompletableFuture<Void> store(String key, MemoryEntry entry, MemoryScope scope)
      Description copied from interface: MemoryStore
      Stores a memory entry.

      If an entry with the same key and scope already exists, it will be replaced.

      Specified by:
      store in interface MemoryStore
      Parameters:
      key - unique identifier for this memory
      entry - the memory entry to store
      scope - the memory scope (SHORT_TERM or LONG_TERM)
      Returns:
      a future that completes when the entry is stored
    • retrieve

      public CompletableFuture<Optional<MemoryEntry>> retrieve(String key, MemoryScope scope)
      Description copied from interface: MemoryStore
      Retrieves a memory entry by key.

      Expired entries are automatically filtered out and return empty.

      Specified by:
      retrieve in interface MemoryStore
      Parameters:
      key - the memory key
      scope - the memory scope to search in
      Returns:
      a future containing the memory entry, or empty if not found
    • search

      public CompletableFuture<List<MemoryEntry>> search(MemoryQuery query)
      Description copied from interface: MemoryStore
      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.

      Specified by:
      search in interface MemoryStore
      Parameters:
      query - the search query parameters
      Returns:
      a future containing matching memory entries (may be empty)
    • delete

      public CompletableFuture<Void> delete(String key, MemoryScope scope)
      Description copied from interface: MemoryStore
      Deletes a memory entry.

      If the entry does not exist, this operation completes successfully without error (idempotent).

      Specified by:
      delete in interface MemoryStore
      Parameters:
      key - the memory key to delete
      scope - the memory scope
      Returns:
      a future that completes when the entry is deleted
    • clear

      public CompletableFuture<Void> clear(MemoryScope scope)
      Description copied from interface: MemoryStore
      Clears all memory entries in the specified scope.

      Warning: This operation cannot be undone. Use with caution.

      Specified by:
      clear in interface MemoryStore
      Parameters:
      scope - the memory scope to clear
      Returns:
      a future that completes when all entries are cleared
    • listKeys

      public CompletableFuture<List<String>> listKeys(MemoryScope scope)
      Description copied from interface: MemoryStore
      Lists all memory keys in the specified scope.

      This operation may be expensive for large memory stores. Consider using MemoryStore.search(MemoryQuery) with pagination instead.

      Specified by:
      listKeys in interface MemoryStore
      Parameters:
      scope - the memory scope
      Returns:
      a future containing all keys (may be empty)
    • getStats

      public MemoryStats getStats()
      Description copied from interface: MemoryStore
      Gets statistics about memory usage.

      Statistics may be cached and not reflect the absolute current state.

      Specified by:
      getStats in interface MemoryStore
      Returns:
      memory usage statistics
    • shutdown

      public void shutdown()
      Shuts down the cleanup executor. Should be called when the store is no longer needed.