Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from typing import Generator
Expand Down
13 changes: 10 additions & 3 deletions generative_ai/context_caching/update_context_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
def update_context_cache(cache_id: str) -> str:
# [START generativeaionvertexai_gemini_update_context_cache]
import vertexai
import datetime
from datetime import datetime as dt
from datetime import timezone as tz
from datetime import timedelta

from vertexai.preview import caching

Expand All @@ -31,8 +33,13 @@ def update_context_cache(cache_id: str) -> str:

cached_content = caching.CachedContent(cached_content_name=cache_id)

# Update the expiration time by 1 hour
cached_content.update(ttl=datetime.timedelta(hours=1))
# Option1: Update the context cache using TTL (Time to live)
cached_content.update(ttl=timedelta(hours=3))
cached_content.refresh()

# Option2: Update the context cache using specific time
next_week_utc = dt.now(tz.utc) + timedelta(days=7)
cached_content.update(expire_time=next_week_utc)
cached_content.refresh()

print(cached_content.expire_time)
Expand Down