spring-projects/spring-ai · error · CachedContentException
Cached content not found:
Error message
Cached content not found:
What it means
extendTtl(name, additionalTtl) first loads the cached content via get(name); if it returns null (name not found or already expired/removed) it throws CachedContentException directly with the name in the message. This is a pre-condition check, not a wrapped remote error.
Source
Thrown at models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/cache/GoogleGenAiCachedContentService.java:387
return CompletableFuture.completedFuture(false);
}
}
// Utility methods
/**
* Extends the TTL of cached content by the specified duration.
* @param name the cached content name
* @param additionalTtl the additional TTL to add
* @return the updated cached content
*/
public @Nullable GoogleGenAiCachedContent extendTtl(String name, Duration additionalTtl) {
Assert.hasText(name, "Name must not be empty");
Assert.notNull(additionalTtl, "Additional TTL must not be null");
GoogleGenAiCachedContent existing = get(name);
if (existing == null) {
throw new CachedContentException("Cached content not found: " + name);
}
Instant newExpireTime = existing.getExpireTime() != null ? existing.getExpireTime().plus(additionalTtl)
: Instant.now().plus(additionalTtl);
CachedContentUpdateRequest updateRequest = CachedContentUpdateRequest.builder()
.expireTime(newExpireTime)
.build();
return update(name, updateRequest);
}
/**
* Refreshes the expiration of cached content to the maximum TTL.
* @param name the cached content name
* @param maxTtl the maximum TTL to set
* @return the updated cached content
*/View on GitHub (pinned to 98a7beda4f)
Solutions
- Call get(name) before extendTtl and recreate the cached content if null
- Refresh TTLs at intervals shorter than the entry TTL
- Verify the resource name and project/region
- Catch CachedContentException and treat as recreation signal
Example fix
// before
service.extendTtl(name, Duration.ofHours(1));
// after
if (service.get(name) == null) {
service.create(buildCreateRequest(name)); // recreate instead
} else {
service.extendTtl(name, Duration.ofHours(1));
} Defensive patterns
Strategy: validation
Validate before calling
GoogleGenAiCachedContent existing = service.get(name);
if (existing == null) {
// recreate or skip; do not call extendTtl
} else {
service.extendTtl(name, additionalTtl);
} Try / catch
try {
service.extendTtl(name, additionalTtl);
} catch (CachedContentException e) {
logger.warn("cache entry {} gone; recreating", name);
recreateCache(name);
} Prevention
- Always check get(name) != null before extendTtl
- Run TTL refresh jobs more often than the shortest TTL
- Handle expired entries by recreating, not updating
- Verify the resource name and project/region
When it happens
Trigger: Calling extendTtl with a name that does not exist, was deleted, or whose TTL already lapsed so Google removed the resource.
Common situations: Background jobs refreshing TTL of cache entries that expired between runs, typos in fully-qualified resource names, or caches created in a different project/region.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- Failed to update cached content:
- Failed to create cached content
- Failed to list cached content
- Vertex AI mode requires both 'project-id' and 'location' to
- Incomplete Google GenAI configuration: Provide 'api-key' for
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/3a9dcad868b241b1.
Report an issue: GitHub.