mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
refactor: Improve cache eviction ordering and clean up session ID usage
Improve the cache eviction routine to sort entries by timestamp using the standard library sort routine (stable, clearer and faster than the prior manual selection/bubble logic), and remove a redundant request-derived session ID helper in favor of the centralized session ID function. Also drop now-unused crypto/encoding imports. This yields clearer, more maintainable eviction logic and removes duplicated/unused code and imports to reduce surface area and potential inconsistencies.
This commit is contained in:
16
internal/cache/signature_cache.go
vendored
16
internal/cache/signature_cache.go
vendored
@@ -3,6 +3,7 @@ package cache
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -89,20 +90,17 @@ func CacheSignature(sessionID, text, signature string) {
|
||||
ts time.Time
|
||||
}{key, entry.Timestamp})
|
||||
}
|
||||
// Simple approach: remove first quarter of entries
|
||||
// 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
|
||||
}
|
||||
// Sort by timestamp (oldest first) - simple bubble for small N
|
||||
|
||||
for i := 0; i < toRemove; i++ {
|
||||
minIdx := i
|
||||
for j := i + 1; j < len(oldest); j++ {
|
||||
if oldest[j].ts.Before(oldest[minIdx].ts) {
|
||||
minIdx = j
|
||||
}
|
||||
}
|
||||
oldest[i], oldest[minIdx] = oldest[minIdx], oldest[i]
|
||||
delete(sc.entries, oldest[i].key)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user