forked from patrickmn/go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_redis_test.go
More file actions
145 lines (120 loc) · 3.75 KB
/
cache_redis_test.go
File metadata and controls
145 lines (120 loc) · 3.75 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
package cache
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/redis/go-redis/v9"
)
// RedisAdapter implements the cache.RedisClient interface using go-redis/v9
type RedisAdapter struct {
client *redis.Client
}
func (r *RedisAdapter) Get(ctx context.Context, key string) ([]byte, error) {
return r.client.Get(ctx, key).Bytes()
}
func (r *RedisAdapter) Set(ctx context.Context, key string, value any, expiration time.Duration) error {
return r.client.Set(ctx, key, value, expiration).Err()
}
func (r *RedisAdapter) Del(ctx context.Context, key string) error {
return r.client.Del(ctx, key).Err()
}
func (r *RedisAdapter) TTL(ctx context.Context, key string) (time.Duration, error) {
return r.client.TTL(ctx, key).Result()
}
type RedisTestStruct struct {
Name string
Value int
}
func getRedisClient(t *testing.T) *RedisAdapter {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
ctx := context.Background()
if err := rdb.Ping(ctx).Err(); err != nil {
t.Skip("Redis not available, skipping test:", err)
}
rdb.FlushDB(ctx)
return &RedisAdapter{client: rdb}
}
func TestRedisCache_SetGet(t *testing.T) {
rdb := getRedisClient(t)
c := New[RedisTestStruct](5*time.Minute, 10*time.Minute)
c.WithRedis(rdb)
ctx := context.Background()
key := "test_key"
val := RedisTestStruct{Name: "foo", Value: 42}
// 1. Set in Cache -> Should go to Redis async
c.Set(key, val, 0)
time.Sleep(100 * time.Millisecond) // Wait for async write
// Verify in Redis
data, err := rdb.Get(ctx, key)
if err != nil {
t.Fatalf("Expected key in Redis, got error: %v", err)
}
var res RedisTestStruct
json.Unmarshal(data, &res)
if res != val {
t.Errorf("Redis value mismatch. Got %v, want %v", res, val)
}
// 2. Flush Local Cache
c.Flush()
if c.ItemCount() != 0 {
t.Errorf("Expected 0 items after Flush, got %d", c.ItemCount())
}
// 3. Get from Cache -> Should fallback to Redis
got, found := c.Get(key)
if found != Found {
t.Errorf("Expected to find item via Redis fallback")
}
if got != val {
t.Errorf("Got %v, want %v", got, val)
}
}
func TestBoundedCache_Eviction(t *testing.T) {
// this test doesn't need redis
c := New[int](NoExpiration, 0)
capacity := 10
c.WithCapacity(capacity)
// Fill to capacity
for i := 0; i < capacity; i++ {
c.Set(string(rune(i)), i, 0)
}
if c.ItemCount() != capacity {
t.Errorf("Expected %d items, got %d", capacity, c.ItemCount())
}
// Add 11th item -> should evict down to 75% (7 items) + 1 new item = 8
c.Set("new", 99, 0)
// Target size is 7 (75% of 10), so after eviction (7) + new item (1) = 8
// We expect count to be <= 8.
// Note: Since map iteration is random, relying on exact count usually works if logic is deterministic.
// Our logic: loops until len <= target. So len will be exactly target (7). Then we add 1. So 8.
maxExpected := int(float64(capacity)*0.75) + 1
if c.ItemCount() > maxExpected {
t.Errorf("Expected max %d items after bulk eviction, got %d", maxExpected, c.ItemCount())
}
}
func TestRedisCache_ExpiryFallback(t *testing.T) {
rdb := getRedisClient(t)
c := New[string](DefaultExpiration, 0)
c.WithRedis(rdb)
key := "expired_local"
// Manually set in Redis with long TTL
rdb.Set(context.Background(), key, "\"still_valid_in_redis\"", time.Hour)
// Set in local with short TTL
c.Set(key, "old_local", 5*time.Millisecond)
time.Sleep(1 * time.Millisecond) // Let it expire locally
got, found := c.Get(key)
if found != Found {
t.Errorf("Expected to find item")
}
if got != "old_local" {
t.Errorf("Got %v, want %v", got, "old_local")
}
time.Sleep(10 * time.Millisecond) // Let it expire locally
// Get -> Should see it's expired locally (or check expiry) and fetch from Redis
_, found = c.Get(key)
if found != Miss {
t.Errorf("Expected to miss item")
}
}