Implementation:Googleapis Python genai Caches CRUD Operations
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Resource_Management |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for managing cached content lifecycle (get, list, update, delete) provided by the google-genai caches module.
Description
The Caches class provides four management operations: get retrieves a cache by name, list returns all caches with pagination, update modifies cache properties (TTL, expiry), and delete removes a cache. Each method operates on cache resource names and returns the appropriate response type. The async counterparts are available via client.aio.caches.
Usage
Use caches.get to inspect a specific cache. Use caches.list to discover all active caches. Use caches.update to extend TTL. Use caches.delete to remove unused caches. All methods require the cache resource name from CachedContent.name.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/caches.py
- Lines: L915-920 (get), L1187-1189 (list), L1057-1062 (update), L980-985 (delete)
Signature
class Caches:
def get(
self,
*,
name: str,
config: Optional[types.GetCachedContentConfigOrDict] = None,
) -> types.CachedContent:
"""Gets a cached content by name."""
def list(
self,
*,
config: Optional[types.ListCachedContentsConfigOrDict] = None,
) -> Pager[types.CachedContent]:
"""Lists all cached contents."""
def update(
self,
*,
name: str,
config: Optional[types.UpdateCachedContentConfigOrDict] = None,
) -> types.CachedContent:
"""Updates a cached content (e.g., extend TTL)."""
def delete(
self,
*,
name: str,
config: Optional[types.DeleteCachedContentConfigOrDict] = None,
) -> types.DeleteCachedContentResponse:
"""Deletes a cached content."""
Import
from google import genai
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes (get/update/delete) | Cache resource name |
| config (update) | Optional[UpdateCachedContentConfigOrDict] | No | Update config (new TTL, expiry) |
| config (list) | Optional[ListCachedContentsConfigOrDict] | No | Pagination config |
Outputs
| Name | Type | Description |
|---|---|---|
| get returns | CachedContent | Cache with metadata, contents, expiry info |
| list returns | Pager[CachedContent] | Paginated list of all cached contents |
| update returns | CachedContent | Updated cache with new TTL/expiry |
| delete returns | DeleteCachedContentResponse | Deletion confirmation |
Usage Examples
Get Cache Info
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
cache = client.caches.get(name="cachedContents/abc123")
print(f"Name: {cache.name}")
print(f"Display name: {cache.display_name}")
print(f"Expire time: {cache.expire_time}")
List All Caches
for cache in client.caches.list():
print(f"{cache.name}: expires {cache.expire_time}")
Extend Cache TTL
from google.genai import types
updated = client.caches.update(
name="cachedContents/abc123",
config=types.UpdateCachedContentConfig(
ttl="7200s" # Extend to 2 hours
),
)
print(f"New expiry: {updated.expire_time}")
Delete a Cache
client.caches.delete(name="cachedContents/abc123")
print("Cache deleted.")