--- title: "脚本缓存过期时间配置" date: 2026-03-18 lastmod: 2026-03-18 description: "控制脚本缓存过期时间的配置项说明" tags: ["脚本配置", "缓存管理", "内存优化"] summary: "配置项作用 # script.cache.expire 配置项控制已编译脚本缓存的过期时间。缓存的脚本在指定时间内没有被访问后将被标记为过期,可以被新的编译脚本替换。 配置项类型 # 该配置项为动态配置,可以在运行时通过集群设置 API 进行修改。 默认值 # 0ms(不自动过期) 是否必需 # 可选配置项(有默认值) 取值范围 # 0 ~ 正无穷(0 表示不自动过期) 配置格式 # # 默认配置(不自动过期) script.cache.expire: 0ms # 设置过期时间 script.cache.expire: 10m # 快速过期 script.cache.expire: 1m # 长时间保持 script.cache.expire: 1h 相关配置项 # 配置项 默认值 说明 script.cache.max_size 100 最大缓存数量 script.cache.expire 0ms 缓存过期时间 script.max_compilations_rate use-context 编译速率限制 工作原理 # 缓存过期机制:" --- ## 配置项作用 `script.cache.expire` 配置项控制已编译脚本缓存的过期时间。缓存的脚本在指定时间内没有被访问后将被标记为过期,可以被新的编译脚本替换。 ## 配置项类型 该配置项为**动态配置**,可以在运行时通过集群设置 API 进行修改。 ## 默认值 ``` 0ms(不自动过期) ``` ## 是否必需 **可选配置项**(有默认值) ## 取值范围 ``` 0 ~ 正无穷(0 表示不自动过期) ``` ## 配置格式 ```yaml # 默认配置(不自动过期) script.cache.expire: 0ms # 设置过期时间 script.cache.expire: 10m # 快速过期 script.cache.expire: 1m # 长时间保持 script.cache.expire: 1h ``` ## 相关配置项 | 配置项 | 默认值 | 说明 | |-------|-------|------| | `script.cache.max_size` | 100 | 最大缓存数量 | | `script.cache.expire` | 0ms | 缓存过期时间 | | `script.max_compilations_rate` | use-context | 编译速率限制 | ## 工作原理 缓存过期机制: ``` ┌─────────────────────────────────────────────────────────────────┐ │ 缓存过期机制 │ └─────────────────────────────────────────────────────────────────┘ 脚本编译并缓存 │ ├── 记录最后访问时间 │ └── 定期检查缓存 │ ├── 当前时间 - 最后访问时间 >= cache.expire │ │ │ └── 标记为可驱逐 │ │ │ └── 需要时编译的新脚本可占用缓存槽位 │ └── 当前时间 - 最后访问时间 < cache.expire │ └── 保持缓存 ``` ## 使用场景 ### 1. 默认配置(推荐) ```yaml script.cache.expire: 0ms ``` **适用场景:** - 大多数集群配置 - 脚本长期有效 - 不需要过期清理 ### 2定期刷新 ```yaml script.cache.expire: 1h ``` **适用场景:** - 脚本可能更新 - 定期重新编译 - 保持缓存新鲜度 ### 3. 快速过期 ```yaml script.cache.expire: 5m ``` **适用场景:** - 脚本频繁变化 - 需要快速更新 - 不关心编译开销 ### 4. 长期保持 ```yaml script.cache.expire: 24h ``` **适用场景:** - 脚本很少变化 - 最大化缓存效果 - 减少重新编译 ## 推荐设置建议 | 脚本变化频率 | 推荐过期时间 | max_size | 说明 | |------------|-------------|---------|------| | 很少变化 | 1h-永久 | 100-500 | 长期缓存 | | 偶尔变化 | 30m-1h | 100-200 | 定期刷新 | | 频繁变化 | 5-10m | 50-100 | 快速过期 | | 总是变化 | 0m | 0-50 | 不缓存 | ## 监控建议 ```bash # 查看当前配置 GET /_cluster/settings?filter_path=*.script.cache.expire # 查看脚本统计 GET /_nodes/stats/script?filter_path=**.cache ``` ## 动态配置示例 ```bash # 更新集群配置 PUT /_cluster/settings { "transient": { "script.cache.expire": "10m" } } ``` ## 注意事项 1. **动态更新**:此配置为动态配置,可在线修改 2. **默认不过期**:默认 0ms 表示不自动过期 3. **与 max_size 配合**:过期时间影响缓存利用率 4. **编译开销**:频繁过期会增加编译开销 5. **合理设置**:根据脚本变化频率调整