From 6bd3c6e3ab0151563c219c05e6f44619c0034112 Mon Sep 17 00:00:00 2001 From: Abijah Date: Thu, 4 Jun 2026 16:26:54 -0700 Subject: [PATCH] Dealing with cache expiration --- gemini/gemini.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/gemini/gemini.py b/gemini/gemini.py index ff21065..6abb089 100644 --- a/gemini/gemini.py +++ b/gemini/gemini.py @@ -106,7 +106,7 @@ def main(): json.dump(context_data, f, indent=4) # --------------------------------------------------------- - # CACHE CREATION LOGIC + # CACHE VALIDATION & CREATION LOGIC # --------------------------------------------------------- system_instruction = ( "You are a hybrid data extraction tool. If a specific format or file format output is requested " @@ -128,6 +128,8 @@ def main(): if model_cache_info: cached_files_set = set(model_cache_info.get("cached_file_ids", [])) + + # Check 1: Have the files changed? if current_files_set != cached_files_set: print(f"File list changed. Destroying stale {args.model} cache to rebuild...") try: @@ -135,6 +137,20 @@ def main(): except Exception as e: print(f"Warning: Could not delete stale cache. {e}", file=sys.stderr) rebuild_cache = True + else: + # Check 2: Does the cache still exist on the server? + print(f"Verifying existing cache for {args.model}: {model_cache_info['cache_id']}...") + try: + client.caches.get(name=model_cache_info['cache_id']) + print("Cache verified. Extending TTL by 60 minutes...") + client.caches.update( + name=model_cache_info['cache_id'], + config=types.UpdateCachedContentConfig(ttl="3600s") + ) + active_cache_id = model_cache_info['cache_id'] + except Exception as e: + print(f"Cache expired or not found. Flagging for rebuild. ({e})") + rebuild_cache = True else: rebuild_cache = True @@ -167,18 +183,6 @@ def main(): cache_too_small = True else: raise e - - elif not cache_too_small and model_cache_info: - active_cache_id = model_cache_info["cache_id"] - print(f"Loading existing cache for {args.model}: {active_cache_id}") - print("Extending cache TTL by 60 minutes...") - try: - client.caches.update( - name=active_cache_id, - config=types.UpdateCachedContentConfig(ttl="3600s") - ) - except Exception as e: - print(f"Warning: Failed to update cache TTL. {e}") # --------------------------------------------------------- # GENERATION LOGIC (WITH HISTORY)