更新web

This commit is contained in:
Alex Yang
2026-01-21 09:46:49 +08:00
parent ac96c7c10b
commit 073f1961b1
80 changed files with 75919 additions and 12379 deletions

185
test/test_cache.go Normal file
View File

@@ -0,0 +1,185 @@
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"dns-server/dns"
miekdns "github.com/miekg/dns"
)
func main() {
// 测试1: 内存缓存模式
fmt.Println("=== 测试1: 内存缓存模式 ===")
memCache := dns.NewDNSCache(60*time.Second, "memory", 100, "test_cache.json", 10*time.Second, 3600*time.Second, 60*time.Second)
// 设置缓存项
msg := &miekdns.Msg{}
msg.SetQuestion("test.com.", miekdns.TypeA)
memCache.Set("test.com.", miekdns.TypeA, msg, 60*time.Second)
// 从缓存获取
_, found := memCache.Get("test.com.", miekdns.TypeA)
if found {
fmt.Println("✓ 内存缓存: 成功获取缓存项")
} else {
fmt.Println("✗ 内存缓存: 未能获取缓存项")
}
// 检查文件是否创建
if _, err := os.Stat("test_cache.json"); os.IsNotExist(err) {
fmt.Println("✓ 内存缓存: 没有创建缓存文件")
} else {
fmt.Println("✗ 内存缓存: 不应该创建缓存文件,但文件存在")
}
// 测试2: 文件缓存模式
fmt.Println("\n=== 测试2: 文件缓存模式 ===")
// 使用独立的缓存文件
test2CacheFile := "test_cache2.json"
// 先删除可能存在的测试文件
os.Remove(test2CacheFile)
fileCache := dns.NewDNSCache(60*time.Second, "file", 100, test2CacheFile, 1*time.Second, 3600*time.Second, 60*time.Second)
// 设置缓存项
fileCache.Set("test.com.", miekdns.TypeA, msg, 60*time.Second)
// 等待保存到文件
time.Sleep(2000 * time.Millisecond)
// 检查文件是否创建
if _, err := os.Stat(test2CacheFile); err == nil {
fmt.Println("✓ 文件缓存: 成功创建缓存文件")
} else {
fmt.Println("✗ 文件缓存: 未能创建缓存文件")
}
// 测试3: 从文件加载缓存
fmt.Println("\n=== 测试3: 从文件加载缓存 ===")
// 创建新的缓存实例,从文件加载
loadCache := dns.NewDNSCache(60*time.Second, "file", 100, test2CacheFile, 10*time.Second, 3600*time.Second, 60*time.Second)
// 从缓存获取
_, found = loadCache.Get("test.com.", miekdns.TypeA)
if found {
fmt.Println("✓ 文件缓存: 成功从文件加载缓存项")
} else {
fmt.Println("✗ 文件缓存: 未能从文件加载缓存项")
}
// 清理测试2的缓存文件
os.Remove(test2CacheFile)
// 测试4: 缓存模式切换
fmt.Println("\n=== 测试4: 缓存模式切换 ===")
// 使用独立的缓存文件
test4CacheFile := "test_cache4.json"
// 先删除可能存在的测试文件
os.Remove(test4CacheFile)
// 创建文件缓存
switchCache := dns.NewDNSCache(60*time.Second, "file", 100, test4CacheFile, 1*time.Second, 3600*time.Second, 60*time.Second)
// 设置缓存项
switchCache.Set("switch.com.", miekdns.TypeA, msg, 60*time.Second)
// 检查当前缓存大小
size := switchCache.Size()
fmt.Printf(" 设置缓存项后,缓存大小: %d\n", size)
// 直接调用SaveToFile方法保存缓存
switchCache.SaveToFile()
// 检查文件是否存在
if _, err := os.Stat(test4CacheFile); err == nil {
fmt.Println(" 切换前: 缓存文件已存在")
// 查看文件大小
fileInfo, _ := os.Stat(test4CacheFile)
fmt.Printf(" 切换前: 缓存文件大小: %d bytes\n", fileInfo.Size())
// 读取文件内容
content, _ := os.ReadFile(test4CacheFile)
fmt.Printf(" 切换前: 缓存文件内容: %s\n", content)
} else {
fmt.Println(" 切换前: 缓存文件不存在")
}
// 切换到内存模式
switchCache.SetCacheMode("memory")
// 再设置一个缓存项
switchCache.Set("switch2.com.", miekdns.TypeA, msg, 60*time.Second)
// 等待一段时间,检查是否继续保存
time.Sleep(2000 * time.Millisecond)
// 检查文件是否仍然存在
if _, err := os.Stat("test_cache.json"); err == nil {
fmt.Println(" 切换后: 缓存文件仍然存在")
// 查看文件大小
fileInfo, _ := os.Stat("test_cache.json")
fmt.Printf(" 切换后: 缓存文件大小: %d bytes\n", fileInfo.Size())
// 读取文件内容
content, _ := os.ReadFile("test_cache.json")
fmt.Printf(" 切换后: 缓存文件内容: %s\n", content)
} else {
fmt.Println(" 切换后: 缓存文件不存在")
}
// 查看当前时间
fmt.Printf(" 当前时间: %v\n", time.Now())
// 查看缓存文件中的过期时间
content, _ := os.ReadFile(test4CacheFile)
var serializableCache map[string]interface{}
json.Unmarshal(content, &serializableCache)
if items, ok := serializableCache["items"].(map[string]interface{}); ok {
if item, ok := items["switch.com.|A"].(map[string]interface{}); ok {
if expiry, ok := item["expiry"].(float64); ok {
expiryTime := time.Unix(0, int64(expiry))
fmt.Printf(" 缓存项过期时间: %v\n", expiryTime)
fmt.Printf(" 缓存项是否过期: %v\n", time.Now().After(expiryTime))
}
}
}
// 创建新的缓存实例,验证是否只保存了切换前的缓存项
verifyCache := dns.NewDNSCache(60*time.Second, "file", 100, test4CacheFile, 10*time.Second, 3600*time.Second, 60*time.Second)
// 检查verifyCache的缓存大小
verifySize := verifyCache.Size()
fmt.Printf(" 新缓存实例大小: %d\n", verifySize)
// 我们无法直接访问verifyCache的内部缓存所以我们尝试不同的域名格式
// 尝试带点和不带点的域名
_, found1 := verifyCache.Get("switch.com", miekdns.TypeA)
_, found2 := verifyCache.Get("switch.com.", miekdns.TypeA)
_, found3 := verifyCache.Get("switch.com.|A", miekdns.TypeA)
fmt.Printf(" 尝试获取switch.com: %v\n", found1)
fmt.Printf(" 尝试获取switch.com.: %v\n", found2)
fmt.Printf(" 尝试获取switch.com.|A: %v\n", found3)
// 检查切换前的缓存项
_, found = verifyCache.Get("switch.com.", miekdns.TypeA)
if found || found1 {
fmt.Println("✓ 模式切换: 切换前的缓存项已保存到文件")
} else {
fmt.Println("✗ 模式切换: 切换前的缓存项未保存到文件")
}
// 检查切换后的缓存项
_, found = verifyCache.Get("switch2.com.", miekdns.TypeA)
if !found {
fmt.Println("✓ 模式切换: 切换后的缓存项没有保存到文件")
} else {
fmt.Println("✗ 模式切换: 切换后的缓存项不应该保存到文件,但文件中存在")
}
// 清理测试文件
os.Remove("test_cache.json")
fmt.Println("\n=== 测试完成 ===")
}