mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
feat(cache): Add CacheKey methods for string representation, equality, and hashing
- Implement `String()` method to generate a unique string representation of CacheKey - Add `Equal()` method to compare CacheKey instances - Create `Hash()` method using FNV-64a hashing algorithm for efficient key comparison
This commit is contained in:
parent
c4cd99a827
commit
88726d9d8f
1
.gitignore
vendored
1
.gitignore
vendored
@ -25,3 +25,4 @@ web/node_modules/
|
|||||||
web/dist/
|
web/dist/
|
||||||
data/config.json
|
data/config.json
|
||||||
data/config.json
|
data/config.json
|
||||||
|
kaifa.md
|
||||||
|
40
internal/cache/manager.go
vendored
40
internal/cache/manager.go
vendored
@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash/fnv"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -24,6 +25,45 @@ type CacheKey struct {
|
|||||||
VaryHeadersMap map[string]string // 存储 Vary 头部的值
|
VaryHeadersMap map[string]string // 存储 Vary 头部的值
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String 实现 Stringer 接口,用于生成唯一的字符串表示
|
||||||
|
func (k CacheKey) String() string {
|
||||||
|
// 将 VaryHeadersMap 转换为有序的字符串
|
||||||
|
var varyPairs []string
|
||||||
|
for key, value := range k.VaryHeadersMap {
|
||||||
|
varyPairs = append(varyPairs, key+"="+value)
|
||||||
|
}
|
||||||
|
sort.Strings(varyPairs)
|
||||||
|
varyStr := strings.Join(varyPairs, "&")
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s|%s|%s|%s", k.URL, k.AcceptHeaders, k.UserAgent, varyStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equal 比较两个 CacheKey 是否相等
|
||||||
|
func (k CacheKey) Equal(other CacheKey) bool {
|
||||||
|
if k.URL != other.URL || k.AcceptHeaders != other.AcceptHeaders || k.UserAgent != other.UserAgent {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(k.VaryHeadersMap) != len(other.VaryHeadersMap) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range k.VaryHeadersMap {
|
||||||
|
if otherValue, ok := other.VaryHeadersMap[key]; !ok || value != otherValue {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash 生成 CacheKey 的哈希值
|
||||||
|
func (k CacheKey) Hash() uint64 {
|
||||||
|
h := fnv.New64a()
|
||||||
|
h.Write([]byte(k.String()))
|
||||||
|
return h.Sum64()
|
||||||
|
}
|
||||||
|
|
||||||
// CacheItem 表示一个缓存项
|
// CacheItem 表示一个缓存项
|
||||||
type CacheItem struct {
|
type CacheItem struct {
|
||||||
FilePath string
|
FilePath string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user