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:
이대희
2025-12-19 13:14:51 +09:00
parent 3275494fde
commit e04b02113a
2 changed files with 8 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ package cache
import ( import (
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"sort"
"sync" "sync"
"time" "time"
) )
@@ -89,20 +90,17 @@ func CacheSignature(sessionID, text, signature string) {
ts time.Time ts time.Time
}{key, entry.Timestamp}) }{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 toRemove := len(oldest) / 4
if toRemove < 1 { if toRemove < 1 {
toRemove = 1 toRemove = 1
} }
// Sort by timestamp (oldest first) - simple bubble for small N
for i := 0; i < toRemove; i++ { 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) delete(sc.entries, oldest[i].key)
} }
} }

View File

@@ -9,8 +9,6 @@ package claude
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/sha256"
"encoding/hex"
"fmt" "fmt"
"strings" "strings"
"sync/atomic" "sync/atomic"
@@ -46,27 +44,6 @@ type Params struct {
CurrentThinkingText strings.Builder // Accumulates thinking text for signature caching CurrentThinkingText strings.Builder // Accumulates thinking text for signature caching
} }
// deriveSessionIDFromRequest generates a stable session ID from the request JSON.
func deriveSessionIDFromRequest(rawJSON []byte) string {
messages := gjson.GetBytes(rawJSON, "messages")
if !messages.IsArray() {
return ""
}
for _, msg := range messages.Array() {
if msg.Get("role").String() == "user" {
content := msg.Get("content").String()
if content == "" {
content = msg.Get("content.0.text").String()
}
if content != "" {
h := sha256.Sum256([]byte(content))
return hex.EncodeToString(h[:16])
}
}
}
return ""
}
// toolUseIDCounter provides a process-wide unique counter for tool use identifiers. // toolUseIDCounter provides a process-wide unique counter for tool use identifiers.
var toolUseIDCounter uint64 var toolUseIDCounter uint64
@@ -92,7 +69,7 @@ func ConvertAntigravityResponseToClaude(_ context.Context, _ string, originalReq
HasFirstResponse: false, HasFirstResponse: false,
ResponseType: 0, ResponseType: 0,
ResponseIndex: 0, ResponseIndex: 0,
SessionID: deriveSessionIDFromRequest(originalRequestRawJSON), SessionID: deriveSessionID(originalRequestRawJSON),
} }
} }