Skip to content

缓存

Sweety 提供两种缓存机制:FastCGI 响应缓存(PHP)和反代响应缓存(HTTP 上游)。

FastCGI 缓存

等价 Nginx fastcgi_cache,缓存 PHP-FPM 的响应。

toml
[sites.fastcgi.cache]
path               = "/tmp/sweety-fcgi-cache"  # 磁盘缓存目录(不填则纯内存)
max_entries        = 1000                       # 内存缓存最大条数(默认 1000)
ttl                = 60                         # 缓存有效期(秒,默认 60)
cacheable_statuses = [200, 301, 302]            # 可缓存的状态码
cacheable_methods  = ["GET", "HEAD"]            # 可缓存的方法

# 存在这些请求头时跳过缓存(不读也不写)
bypass_headers = []

# 忽略这些响应头对缓存决策的影响
# WordPress 带 Cache-Control: no-store 和 Set-Cookie,需配置此项
ignore_headers = ["Cache-Control", "Set-Cookie"]

WordPress 推荐缓存配置

toml
[sites.fastcgi.cache]
max_entries    = 2000
ttl            = 300           # 缓存 5 分钟
ignore_headers = ["Cache-Control", "Set-Cookie"]

反代缓存(proxy_cache)

等价 Nginx proxy_cache,缓存 HTTP 上游的响应。

toml
[sites.proxy_cache]
path               = "/tmp/sweety-proxy-cache"
max_entries        = 1000
ttl                = 60
cacheable_statuses = [200, 301, 302]
cacheable_methods  = ["GET", "HEAD"]
bypass_headers     = ["Authorization", "Cookie"]
ignore_headers     = []

静态文件缓存

静态文件自动缓存,无需手动开启。内置三级缓存:

层级条件说明
内容缓存文件 ≤ 64KB全量缓存在内存,含预压缩 gzip/brotli/zstd 变体,命中时零 syscall
fd 缓存文件 > 64KB缓存文件描述符 + stat 元数据,避免重复 open
sendfile(2)Linux/macOS H1 非 TLS内核零拷贝直传

配置([global]

toml
[global]
open_file_cache_max      = 200000  # 最大缓存条目数(默认 200000)
open_file_cache_inactive = 60      # 不活跃淘汰时间(秒,默认 60)
open_file_cache_total_mb = 512     # 内容缓存内存上限(MB,默认 512)

行为

  • min_uses = 2:文件被访问 2 次后才写入内容缓存,防止缓存污染
  • inotify 实时淘汰:文件变更立即淘汰对应缓存条目
  • 预压缩:首次缓存时自动生成 gzip/brotli/zstd 压缩版本
  • ETag / Last-Modified:命中时返回 304 Not Modified
  • Range 请求:内存缓存命中时直接 slice,零磁盘 I/O

CDN / 多站点场景建议调大 open_file_cache_maxopen_file_cache_total_mb


缓存字段说明

字段默认值说明
pathNone磁盘缓存目录,不设则纯内存
max_entries1000内存中最多缓存多少条响应
ttl60缓存有效期(秒)
cacheable_statuses[200, 301, 302]哪些状态码可以被缓存
cacheable_methods["GET", "HEAD"]哪些 HTTP 方法可以被缓存
bypass_headers[]请求中存在这些头时,跳过缓存
ignore_headers[]忽略响应中这些头对缓存写入的阻止

bypass_headers vs ignore_headers

bypass_headersignore_headers
检查时机请求到来时响应返回时
作用请求头存在 → 不读缓存、不写缓存响应头存在 → 仍然写入缓存
典型用途Authorization(登录用户不缓存)Cache-Control: no-store(WordPress 强制缓存)

按 Location 设置缓存规则

[[sites.locations.cache_rules]] 按文件扩展名设置 Cache-Control 响应头:

toml
[[sites.locations]]
path    = "/"
handler = "static"

[[sites.locations.cache_rules]]
pattern       = "\\.(js|css|woff2?)$"
cache_control = "public, max-age=2592000, immutable"

[[sites.locations.cache_rules]]
pattern       = "\\.(png|jpg|gif|webp|ico)$"
cache_control = "public, max-age=2592000"

[[sites.locations.cache_rules]]
pattern       = "\\.html$"
cache_control = "public, max-age=3600"

也可以在 location 级别覆盖单个 cache_control

toml
[[sites.locations]]
path          = "^~ /static/"
handler       = "static"
cache_control = "public, max-age=86400"

基于 Apache License 2.0 发布