Interface SemanticCacheScopeResolver

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface SemanticCacheScopeResolver
Strategy that resolves the cache scope of a request handled by the SemanticCacheAdvisor. Cached responses are only shared between requests that resolve to the same scope, so the scope must identify the tenant, user or any other boundary across which responses must not leak.

The scope is typically derived from the authenticated principal or from a value the application put into the request context, for example through .advisors(a -> a.param("tenantId", tenantId)). Applications that deliberately share a single cache across all users can return a constant.

The scope is the only isolation boundary: requests with different system prompts share cached responses unless the resolver includes the system prompt in the scope, for example tenantId + "|" + request.prompt().getSystemMessage().getText().

For streaming requests, the resolver may be invoked on a different thread than the one that issued the request, so thread-bound state such as a security context holder backed by a ThreadLocal may not be available. Prefer the request context in that case.

Since:
2.0.2
Author:
Dimitar Proynov
  • Method Summary

    Modifier and Type
    Method
    Description
    @Nullable String
    Resolves the cache scope of the given request.
  • Method Details

    • resolveScope

      @Nullable String resolveScope(ChatClientRequest request)
      Resolves the cache scope of the given request.
      Parameters:
      request - the chat client request
      Returns:
      the cache scope, or null or a blank string to bypass the cache for this request