> ## Documentation Index
> Fetch the complete documentation index at: https://evolink.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini 显式上下文缓存 - 延长或缩短有效期

> 只能改有效期，不能改内容。请求体给 `ttl`（从**现在**起算的新有效期，如 `"7200s"`）或 `expireTime`（新的绝对到期时间）之一。

- 新到期时间距现在最短 60 秒、最长 168 小时。
- 只接受 `ttl` 或 `expireTime`；两者同时传入、或传入其它字段，返回 400。
- **延长**：按新增时长补收存储费（按秒计，不足 1 秒按 1 秒）。**缩短**：不退已收的存储费。
- 这是付费操作：Key 需要有该模型的使用权限，按该 Key 当前所在用户组的价格计费，余额不足返回 402。
- 同一条延长请求重试时可带 `Idempotency-Key` 请求头，避免重复扣费。

> [!NOTE]
>
> **BaseURL 说明**：默认 BaseURL 为 `https://direct.evolink.ai`，对文本模型支持更好，支持长连接；`https://api.evolink.ai` 是多模态主力地址，对文本模型作为备用地址使用。

> [!NOTE]
>
> 只能修改有效期，请求体传 `ttl` 或 `expireTime` 之一。延长按新增时长补收存储费，缩短不退费。


## OpenAPI

````yaml cn/api-manual/language-series/gemini/cached-contents/cached-contents.json PATCH /v1beta/cachedContents/{id}
openapi: 3.1.0
info:
  title: Gemini 显式上下文缓存（cachedContents）
  description: >-
    把反复使用的长前缀（长文档、系统指令、工具定义）一次创建成缓存，后续生成请求只带缓存名和新问题；缓存部分按「缓存命中」单价计费。接口格式与 Gemini
    原生 cachedContents 接口一致。
  license:
    name: MIT
  version: 1.0.0
servers:
  - url: https://direct.evolink.ai
    description: 生产环境（推荐）
  - url: https://api.evolink.ai
    description: 备用地址
security:
  - bearerAuth: []
tags:
  - name: 显式上下文缓存
    description: 创建、查询、延长、删除 Gemini 显式缓存
paths:
  /v1beta/cachedContents/{id}:
    patch:
      tags:
        - 显式上下文缓存
      summary: 延长或缩短有效期
      description: >-
        只能改有效期，不能改内容。请求体给 `ttl`（从**现在**起算的新有效期，如 `"7200s"`）或
        `expireTime`（新的绝对到期时间）之一。


        - 新到期时间距现在最短 60 秒、最长 168 小时。

        - 只接受 `ttl` 或 `expireTime`；两者同时传入、或传入其它字段，返回 400。

        - **延长**：按新增时长补收存储费（按秒计，不足 1 秒按 1 秒）。**缩短**：不退已收的存储费。

        - 这是付费操作：Key 需要有该模型的使用权限，按该 Key 当前所在用户组的价格计费，余额不足返回 402。

        - 同一条延长请求重试时可带 `Idempotency-Key` 请求头，避免重复扣费。
      operationId: geminiUpdateCachedContent
      parameters:
        - name: id
          in: path
          required: true
          description: >-
            缓存 id：创建接口返回的 `name` 去掉 `cachedContents/` 前缀后的部分。例如 `name` 为
            `cachedContents/20261103012026348092000s1oTuB96`，这里填
            `20261103012026348092000s1oTuB96`。
          schema:
            type: string
            example: 20261103012026348092000s1oTuB96
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateCachedContentRequest'
            examples:
              extend_by_ttl:
                summary: 从现在起再保留 2 小时
                value:
                  ttl: 7200s
              set_expire_time:
                summary: 改成固定到期时间
                value:
                  expireTime: '2026-11-03T06:00:00Z'
      responses:
        '200':
          description: 更新后的缓存元数据，`expireTime` 为新的到期时间
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CachedContent'
              example:
                name: cachedContents/20261103012026348092000s1oTuB96
                model: models/gemini-3.8-flash
                displayName: product-manual-v3
                createTime: '2026-11-03T01:20:27.311Z'
                updateTime: '2026-11-03T01:35:10.020Z'
                expireTime: '2026-11-03T03:35:10.000Z'
                usageMetadata:
                  totalTokenCount: 7525
        '400':
          description: >-
            没有给 ttl /
            expireTime，或两者同时传入；传入了其它字段；新到期时间超出范围；缓存在处理途中到期（CACHE_EXPIRED）
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 400
                  message: new expiry must be at least 60s in the future
                  status: INVALID_ARGUMENT
                  details:
                    - '@type': type.googleapis.com/google.rpc.ErrorInfo
                      reason: INVALID_ARGUMENT
                      domain: evolink.ai
        '401':
          description: API Key 无效或缺失
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 401
                  message: Invalid API key
                  status: UNAUTHENTICATED
        '402':
          description: 余额不足以补收延长部分的存储费
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 402
                  message: insufficient quota for cache storage extension
                  status: RESOURCE_EXHAUSTED
                  details:
                    - '@type': type.googleapis.com/google.rpc.ErrorInfo
                      reason: INSUFFICIENT_QUOTA
                      domain: evolink.ai
        '403':
          description: Key 无权使用该模型、来源 IP 不在 Key 的白名单内，或 Key 所在的用户组已不可用
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 403
                  message: This key is not allowed to use model gemini-3.8-flash
                  status: PERMISSION_DENIED
        '404':
          description: 缓存不存在、已过期、已删除或不属于当前账号；或显式缓存功能未开放
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 404
                  message: cached content not found
                  status: NOT_FOUND
                  details:
                    - '@type': type.googleapis.com/google.rpc.ErrorInfo
                      reason: CACHE_NOT_FOUND
                      domain: evolink.ai
        '429':
          description: 触发频率限制
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 429
                  message: Rate limit exceeded
                  status: RESOURCE_EXHAUSTED
        '502':
          description: 服务暂时不可用，本次不扣费，请稍后重试
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 502
                  message: >-
                    the request could not be completed due to a service
                    authorization problem; please retry later or contact support
                  status: INTERNAL
                  details:
                    - '@type': type.googleapis.com/google.rpc.ErrorInfo
                      reason: UPSTREAM_ERROR
                      domain: evolink.ai
        '503':
          description: 缓存暂时不可用，请稍后重试
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: 503
                  message: >-
                    cached content is temporarily unavailable; please retry
                    later
                  status: UNAVAILABLE
                  details:
                    - '@type': type.googleapis.com/google.rpc.ErrorInfo
                      reason: CACHE_CHANNEL_UNAVAILABLE
                      domain: evolink.ai
components:
  schemas:
    UpdateCachedContentRequest:
      type: object
      description: ttl 与 expireTime 二选一，必须且只能给一个；传入其它字段返回 400
      properties:
        ttl:
          type: string
          description: 从现在起的新有效期，如 `"7200s"`。最短 `60s`，最长 `604800s`
          example: 7200s
        expireTime:
          type: string
          format: date-time
          description: 新的绝对到期时间（RFC 3339，UTC）
          example: '2026-11-03T06:00:00Z'
    CachedContent:
      type: object
      properties:
        name:
          type: string
          description: >-
            缓存名，形如 `cachedContents/<id>`。生成请求的 `cachedContent` 字段填它；`<id>`
            同时是创建请求的请求号
          example: cachedContents/20261103012026348092000s1oTuB96
        model:
          type: string
          description: 缓存绑定的模型，引用时生成请求的模型必须与之一致
          example: models/gemini-3.8-flash
        displayName:
          type: string
          description: 创建时给的名字，没给则不返回
        createTime:
          type: string
          format: date-time
          description: 创建时间（UTC）
        updateTime:
          type: string
          format: date-time
          description: 最近一次更新时间（UTC）
        expireTime:
          type: string
          format: date-time
          description: 到期时间（UTC），到期后自动删除
        usageMetadata:
          type: object
          properties:
            totalTokenCount:
              type: integer
              description: 缓存的 token 总数，缓存写入费与存储费按它计算
              example: 7525
    ErrorResponse:
      type: object
      description: >-
        Gemini 原生错误格式。`error.details[0].reason` 为原因码：`INVALID_ARGUMENT`
        参数错误；`EXPLICIT_CACHE_UNSUPPORTED`
        模型不支持显式缓存；`NO_AVAILABLE_CHANNEL`、`MODEL_PRICE_ERROR`
        当前用户组暂未开放；`CACHE_EXPIRED` 缓存已到期或剩余有效期过短；`CACHE_MODEL_MISMATCH`
        模型与缓存不一致；`INVALID_CACHED_CONTENT` `cachedContent`
        格式不正确；`INSUFFICIENT_QUOTA` 余额不足；`CACHE_NOT_FOUND`
        缓存不存在、已过期、已删除或不属于当前账号；`EXPLICIT_CACHE_DISABLED`
        功能暂未开放；`CACHE_CONTENT_TOO_LARGE` 请求体超过 10 MB；`UPSTREAM_ERROR` 请求未被接受，原因见
        `message`；`CACHE_CHANNEL_UNAVAILABLE`、`INTERNAL` 暂时不可用，请稍后重试。
      properties:
        error:
          type: object
          properties:
            code:
              type: integer
              description: HTTP 状态码
            message:
              type: string
            status:
              type: string
              description: 状态枚举，如 INVALID_ARGUMENT / NOT_FOUND / UNAVAILABLE
            details:
              type: array
              items:
                type: object
                properties:
                  '@type':
                    type: string
                    example: type.googleapis.com/google.rpc.ErrorInfo
                  reason:
                    type: string
                    example: CACHE_NOT_FOUND
                  domain:
                    type: string
                    example: evolink.ai
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        ##所有接口均需要使用 Bearer Token 进行认证##


        **获取 API Key：**


        访问 [API Key 管理页面](https://evolink.ai/dashboard/keys) 获取您的 API Key


        **使用时在请求头中添加：**

        ```

        Authorization: Bearer YOUR_API_KEY

        ```


        也接受 Gemini 原生写法：请求头 `x-goog-api-key: YOUR_API_KEY`，或 URL 参数
        `?key=YOUR_API_KEY`。

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.