mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
refactor(cache): remove max entries logic and extend signature TTL to 3 hours
This commit is contained in:
60
internal/cache/signature_cache.go
vendored
60
internal/cache/signature_cache.go
vendored
@@ -3,7 +3,6 @@ package cache
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -16,10 +15,7 @@ type SignatureEntry struct {
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// SignatureCacheTTL is how long signatures are valid
|
// SignatureCacheTTL is how long signatures are valid
|
||||||
SignatureCacheTTL = 1 * time.Hour
|
SignatureCacheTTL = 3 * time.Hour
|
||||||
|
|
||||||
// MaxEntriesPerSession limits memory usage per session
|
|
||||||
MaxEntriesPerSession = 100
|
|
||||||
|
|
||||||
// SignatureTextHashLen is the length of the hash key (16 hex chars = 64-bit key space)
|
// SignatureTextHashLen is the length of the hash key (16 hex chars = 64-bit key space)
|
||||||
SignatureTextHashLen = 16
|
SignatureTextHashLen = 16
|
||||||
@@ -112,43 +108,6 @@ func CacheSignature(sessionID, text, signature string) {
|
|||||||
sc.mu.Lock()
|
sc.mu.Lock()
|
||||||
defer sc.mu.Unlock()
|
defer sc.mu.Unlock()
|
||||||
|
|
||||||
// Evict expired entries if at capacity
|
|
||||||
if len(sc.entries) >= MaxEntriesPerSession {
|
|
||||||
now := time.Now()
|
|
||||||
for key, entry := range sc.entries {
|
|
||||||
if now.Sub(entry.Timestamp) > SignatureCacheTTL {
|
|
||||||
delete(sc.entries, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If still at capacity, remove oldest entries
|
|
||||||
if len(sc.entries) >= MaxEntriesPerSession {
|
|
||||||
// Find and remove oldest quarter
|
|
||||||
oldest := make([]struct {
|
|
||||||
key string
|
|
||||||
ts time.Time
|
|
||||||
}, 0, len(sc.entries))
|
|
||||||
for key, entry := range sc.entries {
|
|
||||||
oldest = append(oldest, struct {
|
|
||||||
key string
|
|
||||||
ts time.Time
|
|
||||||
}{key, entry.Timestamp})
|
|
||||||
}
|
|
||||||
// Sort by timestamp (oldest first) using sort.Slice
|
|
||||||
sort.Slice(oldest, func(i, j int) bool {
|
|
||||||
return oldest[i].ts.Before(oldest[j].ts)
|
|
||||||
})
|
|
||||||
|
|
||||||
toRemove := len(oldest) / 4
|
|
||||||
if toRemove < 1 {
|
|
||||||
toRemove = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < toRemove; i++ {
|
|
||||||
delete(sc.entries, oldest[i].key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sc.entries[textHash] = SignatureEntry{
|
sc.entries[textHash] = SignatureEntry{
|
||||||
Signature: signature,
|
Signature: signature,
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
@@ -170,22 +129,25 @@ func GetCachedSignature(sessionID, text string) string {
|
|||||||
|
|
||||||
textHash := hashText(text)
|
textHash := hashText(text)
|
||||||
|
|
||||||
sc.mu.RLock()
|
now := time.Now()
|
||||||
entry, exists := sc.entries[textHash]
|
|
||||||
sc.mu.RUnlock()
|
|
||||||
|
|
||||||
|
sc.mu.Lock()
|
||||||
|
entry, exists := sc.entries[textHash]
|
||||||
if !exists {
|
if !exists {
|
||||||
|
sc.mu.Unlock()
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
if now.Sub(entry.Timestamp) > SignatureCacheTTL {
|
||||||
// Check if expired
|
|
||||||
if time.Since(entry.Timestamp) > SignatureCacheTTL {
|
|
||||||
sc.mu.Lock()
|
|
||||||
delete(sc.entries, textHash)
|
delete(sc.entries, textHash)
|
||||||
sc.mu.Unlock()
|
sc.mu.Unlock()
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Refresh TTL on access (sliding expiration).
|
||||||
|
entry.Timestamp = now
|
||||||
|
sc.entries[textHash] = entry
|
||||||
|
sc.mu.Unlock()
|
||||||
|
|
||||||
return entry.Signature
|
return entry.Signature
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user