From c9f9d9ee1c901ef54ae0511d69ab18be6087403c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= <i@sekai.icu>
Date: Thu, 14 Nov 2024 12:42:42 +0800
Subject: [PATCH] Add `dns.cache_capacity`

---
 docs/configuration/dns/index.md    | 18 ++++++++++++++++++
 docs/configuration/dns/index.zh.md | 19 ++++++++++++++++++-
 option/dns.go                      |  1 +
 route/router.go                    |  1 +
 4 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/docs/configuration/dns/index.md b/docs/configuration/dns/index.md
index f37c93a4..fc813334 100644
--- a/docs/configuration/dns/index.md
+++ b/docs/configuration/dns/index.md
@@ -1,3 +1,12 @@
+---
+icon: material/new
+---
+
+
+!!! quote "Changes in sing-box 1.11.0"
+
+    :material-plus: [cache_capacity](#cache_capacity)
+
 !!! quote "Changes in sing-box 1.9.0"
 
     :material-plus: [client_subnet](#client_subnet)
@@ -16,6 +25,7 @@
     "disable_cache": false,
     "disable_expire": false,
     "independent_cache": false,
+    "cache_capacity": 0,
     "reverse_mapping": false,
     "client_subnet": "",
     "fakeip": {}
@@ -58,6 +68,14 @@ Disable dns cache expire.
 
 Make each DNS server's cache independent for special purposes. If enabled, will slightly degrade performance.
 
+#### cache_capacity
+
+!!! quote "Since sing-box 1.11.0"
+
+LRU cache capacity.
+
+Value less than 1024 will be ignored.
+
 #### reverse_mapping
 
 Stores a reverse mapping of IP addresses after responding to a DNS query in order to provide domain names when routing.
diff --git a/docs/configuration/dns/index.zh.md b/docs/configuration/dns/index.zh.md
index c845ae0a..20fbc00d 100644
--- a/docs/configuration/dns/index.zh.md
+++ b/docs/configuration/dns/index.zh.md
@@ -1,4 +1,12 @@
-!!! quote "sing-box 1.9.0 中的更改"
+---
+icon: material/new
+---
+
+!!! quote "自 sing-box 1.11.0 起"
+
+    :material-plus: [cache_capacity](#cache_capacity)
+
+!!! quote "自 sing-box 1.9.0 起"
 
     :material-plus: [client_subnet](#client_subnet)
 
@@ -16,6 +24,7 @@
     "disable_cache": false,
     "disable_expire": false,
     "independent_cache": false,
+    "cache_capacity": 0,
     "reverse_mapping": false,
     "client_subnet": "",
     "fakeip": {}
@@ -57,6 +66,14 @@
 
 使每个 DNS 服务器的缓存独立,以满足特殊目的。如果启用,将轻微降低性能。
 
+#### cache_capacity
+
+!!! quote "自 sing-box 1.11.0 起"
+
+LRU 缓存容量。
+
+小于 1024 的值将被忽略。
+
 #### reverse_mapping
 
 在响应 DNS 查询后存储 IP 地址的反向映射以为路由目的提供域名。
diff --git a/option/dns.go b/option/dns.go
index 32c1ac2e..272c5180 100644
--- a/option/dns.go
+++ b/option/dns.go
@@ -31,6 +31,7 @@ type DNSClientOptions struct {
 	DisableCache     bool                  `json:"disable_cache,omitempty"`
 	DisableExpire    bool                  `json:"disable_expire,omitempty"`
 	IndependentCache bool                  `json:"independent_cache,omitempty"`
+	CacheCapacity    uint32                `json:"cache_capacity,omitempty"`
 	ClientSubnet     *badoption.Prefixable `json:"client_subnet,omitempty"`
 }
 
diff --git a/route/router.go b/route/router.go
index 1f760a86..f23604b3 100644
--- a/route/router.go
+++ b/route/router.go
@@ -96,6 +96,7 @@ func NewRouter(ctx context.Context, logFactory log.Factory, options option.Route
 		DisableCache:     dnsOptions.DNSClientOptions.DisableCache,
 		DisableExpire:    dnsOptions.DNSClientOptions.DisableExpire,
 		IndependentCache: dnsOptions.DNSClientOptions.IndependentCache,
+		CacheCapacity:    dnsOptions.DNSClientOptions.CacheCapacity,
 		RDRC: func() dns.RDRCStore {
 			cacheFile := service.FromContext[adapter.CacheFile](ctx)
 			if cacheFile == nil {