Package com.tnsai.llm.cache
Class CacheableContent
java.lang.Object
com.tnsai.llm.cache.CacheableContent
Wrapper for content that can be cached.
CacheableContent wraps text content or structured content with optional cache control settings.
Usage Examples
Cacheable System Prompt
CacheableContent systemPrompt = CacheableContent.of(
"You are an expert Java developer...",
CacheControl.ephemeral()
);
Cacheable with Multiple Text Blocks
CacheableContent content = CacheableContent.builder()
.addText("Context information...")
.addText("More context...", CacheControl.ephemeral()) // Only this part cached
.build();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classA content block with optional cache control. -
Method Summary
Modifier and TypeMethodDescriptionstatic CacheableContent.Builderbuilder()Creates a builder for complex content with multiple blocks.Gets content blocks (for complex content).Gets the cache control (for simple content).getText()Gets the text content (for simple content).booleanChecks if this content has cache control enabled.booleanisSimple()Checks if this is simple text content.static CacheableContentCreates cacheable content from text.static CacheableContentof(String text, CacheControl cacheControl) Creates cacheable content from text with cache control.Converts to Anthropic API format.
-
Method Details
-
of
Creates cacheable content from text.- Parameters:
text- The text content- Returns:
- Cacheable content without cache control
-
of
Creates cacheable content from text with cache control.- Parameters:
text- The text contentcacheControl- The cache control settings- Returns:
- Cacheable content with cache control
-
builder
Creates a builder for complex content with multiple blocks. -
getText
Gets the text content (for simple content). -
getBlocks
Gets content blocks (for complex content). -
getCacheControl
Gets the cache control (for simple content). -
isCacheable
public boolean isCacheable()Checks if this content has cache control enabled. -
isSimple
public boolean isSimple()Checks if this is simple text content. -
toApiFormat
Converts to Anthropic API format.For simple text without cache control:
{"type": "text", "text": "..."}For text with cache control:
{"type": "text", "text": "...", "cache_control": {"type": "ephemeral"}}- Returns:
- Content in Anthropic API format
-