main
c8b91bc ยท 4 months ago 7 commits
 1package ai
 2
 3import (
 4	"sync"
 5	"time"
 6)
 7
 8type cacheEntry struct {
 9	response  string
10	expiresAt time.Time
11}
12
13var (
14	cache   = make(map[string]cacheEntry)
15	cacheMu sync.RWMutex
16	cacheTTL = 5 * time.Minute
17)
18
19func getCached(prompt string) (string, bool) {
20	cacheMu.RLock()
21	defer cacheMu.RUnlock()
22
23	entry, ok := cache[prompt]
24	if !ok || time.Now().After(entry.expiresAt) {
25		return "", false
26	}
27	return entry.response, true
28}
29
30func setCached(prompt, response string) {
31	cacheMu.Lock()
32	defer cacheMu.Unlock()
33	cache[prompt] = cacheEntry{
34		response:  response,
35		expiresAt: time.Now().Add(cacheTTL),
36	}
37}