Files
smartdns/docs/config/cache.md
2023-03-11 17:52:57 +08:00

2.9 KiB
Raw Blame History

hide
hide
toc

缓存

SmartDNS提供了缓存能力开启缓存的情况下能提供更好的DNS查询请求smartdns提供的过期缓存功能会将体验更进一步提升。

缓存配置

SmartDNS可以通过cache-size配置缓存的条数,同时也可以通过cache-persist来配置是否持久化缓存,也可以通过cache-file来指定缓存文件存储路径

cache-size 32768
cache-persist yes
cache-file /path/to/cache/file

注意:

  1. smartdns默认自动根据磁盘空间是否启用缓存。
  2. 缓存文件只有在进程正常退出的时候才会保存供下次使用。

缓存预获取

Smartdns可以设置缓存预获取避免缓存超时配置预先获取后smartdns将在域名超时前的5s内重新进行域名查询。并对域名的热度进行排序。

prefetch-domain yes

注意:

  1. 此功能将会导致smaratdns消耗更多的CPU。

过期缓存

过期缓存也要乐观缓存其功能指定是当DNS域名的TTL到0时其结果仍然存储在缓存中等下次查询时仍然将缓存的结果返回给客户端避免客户端等待。乐观缓存的前提时DNS的对应的IP地址不会频繁变化。

具体乐观缓存的原理可以参考(RFC 8767) (https://www.rfc-editor.org/rfc/rfc8767)

smartdns过期缓存处理流程


%%{init: {'theme':'forest'}}%%
sequenceDiagram
    autonumber
    participant client as 客户端
    participant smartdns as SmartDNS
    participant Server as 上游DNS服务器
    client->>smartdns: DNS查询
    alt 域名不在缓存中
        smartdns->>+Server: 上游查询DNS
        Server-->>-smartdns: 返回查询结果
        smartdns->smartdns: 测速,并缓存结果
        smartdns->client: 返回结果
    else 域名在过期缓存中
        rect rgb(160, 250, 140)
        smartdns->smartdns: 域名在缓存中,且已经过期。
        smartdns->client: 返回结果过期的域名结果TTL为3
        smartdns->>+Server: 上游查询DNS
        Server->-smartdns: 返回查询结果
        smartdns->smartdns: 测速,并缓存结果
        client->smartdns: 3s后客户端再次查询获取最佳结果。
        end
    end

配置步骤

  1. 开启过期缓存
serve-expired yes
  1. 配置过期缓存超时时间

此时间表示过期缓存多长时间未访问,则从缓存中释放。

serve-expired-ttl 259200
  1. 配置过期缓存响应TTL

此时间表示当缓存中域名TTL超时时返回给客户端的TTL时间让客户端在下列TTL时间后再次查询。

serve-expired-reply-ttl 3
  1. 过期缓存预获取时间

此时间表示过期缓存在多长时间未访问主动进行预先获取以避免IP无效开启过期缓存后prefetch的功能将和未开启不同。

prefetch-domain yes
serve-expired-prefetch-time 21600