-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcache.go
More file actions
268 lines (233 loc) · 7.49 KB
/
Copy pathcache.go
File metadata and controls
268 lines (233 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/gin-gonic/gin"
)
// CachedResponse represents the data stored in Redis
type CachedResponse struct {
Result string `json:"result"`
CachedAt int64 `json:"cached_at"`
}
func CacheMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// Only cache if Redis is available
if redisClient == nil {
c.Next()
return
}
// Check for payment headers (Signature/Nonce)
signature := c.GetHeader("X-402-Signature")
nonce := c.GetHeader("X-402-Nonce")
// If no signature, we can't verify payment, so bypass cache
// (Handler will reject it anyway)
if signature == "" || nonce == "" {
c.Next()
return
}
// Read request body to generate cache key
// Check Content-Length first to reject oversized requests immediately
const maxBodySize = 10 * 1024 * 1024
// ContentLength == -1 means unknown (chunked encoding or no header), proceed to MaxBytesReader
if c.Request.ContentLength > maxBodySize {
c.Header("Connection", "close")
c.JSON(413, gin.H{"error": "Payload too large", "max_size": "10MB"})
c.Abort()
return
}
var requestBody []byte
var err error
if c.Request.Body != nil {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxBodySize))
requestBody, err = io.ReadAll(c.Request.Body)
if err != nil {
// If body too large, MaxBytesReader returns error
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
c.Header("Connection", "close")
c.JSON(413, gin.H{"error": "Payload too large", "max_size": "10MB"})
c.Abort()
return
}
// Other read errors - don't continue to handler since body is corrupted
respondError(c, 500, "request_body_read_failed", err)
c.Abort()
return
}
// Store body in context for handler reuse
c.Set("request_body", requestBody)
// Restore body for any code path (cache hit abort or handler)
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
}
// Parse body to get text for cache key
// Note: Cache key is based on text+model at request time. If model env var changes
// between cache key generation and AI call, there could be a mismatch, but this
// is acceptable since the model should not change during normal operation.
var req SummarizeRequest
if err := json.Unmarshal(requestBody, &req); err != nil {
// Invalid JSON - reject immediately to prevent cache bypass attacks
log.Printf("[DEBUG] Invalid JSON in request: %v", err)
c.JSON(400, gin.H{"error": "Invalid request body", "message": "Request must be valid JSON"})
c.Abort()
return
}
// Validate text is not empty
if req.Text == "" {
c.JSON(400, gin.H{"error": "Invalid request", "message": "text field cannot be empty"})
c.Abort()
return
}
// Generate Cache Key (include model to prevent cache collisions)
// Get the model from the active provider configuration
model := os.Getenv("OPENROUTER_MODEL")
if os.Getenv("AI_PROVIDER") == "ollama" {
model = os.Getenv("OLLAMA_MODEL")
if model == "" {
model = "llama2"
}
} else if model == "" {
model = "z-ai/glm-4.5-air:free"
}
cacheKey := getCacheKey(req.Text, model)
// Check Cache
if cached, err := getFromCache(c.Request.Context(), cacheKey); err == nil {
log.Printf("Cache HIT: %s", cacheKey)
routePath := c.FullPath()
if routePath == "" {
routePath = "unknown"
}
cacheHits.WithLabelValues(routePath).Inc()
payment, ok := verifyPaidRequest(c, requestBody)
if !ok {
c.Abort()
return
}
// Generate Receipt and Respond
// We treat the cached result as the AI result
// Generate receipt for cache hit using current request and cached result.
// Note: request_hash matches current request, response is from cache,
// but both are cryptographically valid since cache key ensures identical text.
if err := sendPaidResult(c, payment, requestBody, cached.Result); err != nil {
log.Printf("Failed to send cached response receipt: %v", err)
// generateAndSendReceipt already sent an error response (500)
}
c.Abort()
return
}
// Cache MISS
log.Printf("Cache MISS: %s", cacheKey)
routePath := c.FullPath()
if routePath == "" {
routePath = "unknown"
}
cacheMisses.WithLabelValues(routePath).Inc()
// Prepare to capture response
writer := &cachedWriter{
ResponseWriter: c.Writer,
body: &bytes.Buffer{},
cacheKey: cacheKey,
}
c.Writer = writer
c.Next()
// Handler finished. Check status and extract result with proper locking
writer.mu.RLock()
statusCode := writer.ResponseWriter.Status()
bodyBytes := writer.body.Bytes()
writer.mu.RUnlock()
if statusCode == 200 {
// Response format: {"result": "...", "receipt": ...}
var resp map[string]interface{}
if err := json.Unmarshal(bodyBytes, &resp); err == nil {
if result, ok := resp["result"].(string); ok {
// Store asynchronously with a deadline to prevent indefinite goroutines
go func(k, v string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
storeInCache(ctx, k, v)
}(cacheKey, result)
}
}
}
}
}
func getCacheKey(text string, model string) string {
// IMPORTANT: This cache key ONLY includes text and model.
// Cache version v1 - if parameters change, increment version to invalidate old caches
// If the AI provider's Generate() method is modified to accept additional parameters
// (temperature, max_tokens, top_p, etc.), those MUST be added to
// this cache key to prevent incorrect cache hits.
// TODO: Consider accepting a struct with all OpenRouter parameters
const cacheVersion = "v1"
combined := cacheVersion + ":" + text + ":" + model
hash := sha256.Sum256([]byte(combined))
return "ai:summary:" + hex.EncodeToString(hash[:])
}
func getFromCache(ctx context.Context, key string) (*CachedResponse, error) {
if redisClient == nil {
return nil, fmt.Errorf("redis not available")
}
val, err := redisClient.Get(ctx, key).Result()
if err != nil {
return nil, err
}
var cached CachedResponse
if err := json.Unmarshal([]byte(val), &cached); err != nil {
return nil, err
}
return &cached, nil
}
func storeInCache(ctx context.Context, key string, data string) {
if redisClient == nil {
return
}
ttl := time.Duration(getEnvAsInt("CACHE_TTL_SECONDS", 3600)) * time.Second
cached := CachedResponse{
Result: data,
CachedAt: time.Now().Unix(),
}
jsonData, err := json.Marshal(cached)
if err != nil {
log.Printf("[WARNING] Failed to marshal cache data for key %s: %v", safeKeyPrefix(key), err)
return
}
// Use the context provided by caller (already has 5s timeout from async goroutine)
if err := redisClient.Set(ctx, key, jsonData, ttl).Err(); err != nil {
log.Printf("[WARNING] Failed to store in cache for key %s: %v", safeKeyPrefix(key), err)
}
}
// safeKeyPrefix returns first 32 chars of key for logging, or full key if shorter
func safeKeyPrefix(key string) string {
if len(key) > 32 {
return key[:32] + "..."
}
return key
}
type cachedWriter struct {
gin.ResponseWriter
body *bytes.Buffer
cacheKey string
mu sync.RWMutex
}
func (w *cachedWriter) Write(data []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
w.body.Write(data)
return w.ResponseWriter.Write(data)
}
func (w *cachedWriter) WriteString(s string) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}