in javascript/cache.js [270:316]
export async function cacheUpdate() {
// [START cache_update]
// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const filePath = path.join(media, "a11.txt");
const document = await ai.files.upload({
file: filePath,
config: { mimeType: "text/plain" },
});
console.log("Uploaded file name:", document.name);
const modelName = "gemini-1.5-flash-001";
const contents = [
createUserContent(createPartFromUri(document.uri, document.mimeType)),
];
let cache = await ai.caches.create({
model: modelName,
config: {
contents: contents,
systemInstruction: "You are an expert analyzing transcripts.",
},
});
// Update the cache's time-to-live (ttl)
const ttl = `${2 * 3600}s`; // 2 hours in seconds
cache = await ai.caches.update({
name: cache.name,
config: { ttl },
});
console.log("After update (TTL):", cache);
// Alternatively, update the expire_time directly (in RFC 3339 format with a "Z" suffix)
const expireTime = new Date(Date.now() + 15 * 60000)
.toISOString()
.replace(/\.\d{3}Z$/, "Z");
cache = await ai.caches.update({
name: cache.name,
config: { expireTime: expireTime },
});
console.log("After update (expire_time):", cache);
// [END cache_update]
await ai.caches.delete({ name: cache.name });
return cache;
}