Class InMemorySessionStore<S>

java.lang.Object
com.google.genkit.ai.session.InMemorySessionStore<S>
Type Parameters:
S - the type of the custom session state
All Implemented Interfaces:
SessionStore<S>

public class InMemorySessionStore<S> extends Object implements SessionStore<S>
InMemorySessionStore is a simple in-memory implementation of SessionStore.

This implementation is suitable for:

  • Development and testing
  • Single-instance deployments
  • Prototyping

Note: Sessions are lost when the application restarts. For production use cases requiring persistence, implement a database-backed SessionStore.

  • Constructor Details

    • InMemorySessionStore

      public InMemorySessionStore()
      Creates a new InMemorySessionStore.
  • Method Details

    • get

      public CompletableFuture<SessionData<S>> get(String sessionId)
      Description copied from interface: SessionStore
      Retrieves a session by its ID.
      Specified by:
      get in interface SessionStore<S>
      Parameters:
      sessionId - the session ID
      Returns:
      a CompletableFuture containing the session data, or null if not found
    • save

      public CompletableFuture<Void> save(String sessionId, SessionData<S> sessionData)
      Description copied from interface: SessionStore
      Saves session data.
      Specified by:
      save in interface SessionStore<S>
      Parameters:
      sessionId - the session ID
      sessionData - the session data to save
      Returns:
      a CompletableFuture that completes when the save is done
    • delete

      public CompletableFuture<Void> delete(String sessionId)
      Description copied from interface: SessionStore
      Deletes a session by its ID.
      Specified by:
      delete in interface SessionStore<S>
      Parameters:
      sessionId - the session ID
      Returns:
      a CompletableFuture that completes when the deletion is done
    • exists

      public CompletableFuture<Boolean> exists(String sessionId)
      Description copied from interface: SessionStore
      Checks if a session exists.
      Specified by:
      exists in interface SessionStore<S>
      Parameters:
      sessionId - the session ID
      Returns:
      a CompletableFuture containing true if the session exists
    • size

      public int size()
      Returns the number of sessions currently stored.
      Returns:
      the session count
    • clear

      public void clear()
      Clears all sessions from the store.