Skip to content

Commit d8b4f6e

Browse files
committed
Add Cache object to go-redis
1 parent d70d9ea commit d8b4f6e

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

cache.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package redis
2+
3+
import (
4+
"github.com/dgraph-io/ristretto"
5+
)
6+
7+
// Cache structure
8+
type Cache struct {
9+
cache *ristretto.Cache
10+
}
11+
12+
// NewCache creates a new Cache instance with the given configuration
13+
func NewCache(numKeys int64, memSize int64) (*Cache, error) {
14+
// Create a new cache with the given configuration
15+
config := &ristretto.Config{
16+
NumCounters: numKeys * 10, // number of keys to track frequency of (10x number of items to cache)
17+
MaxCost: memSize, // maximum cost of cache (in bytes)
18+
BufferItems: 64, // number of keys per Get buffer
19+
}
20+
21+
cache, err := ristretto.NewCache(config)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
return &Cache{cache: cache}, nil
27+
}
28+
29+
// Set adds a value to the cache
30+
func (c *Cache) Set(key, value interface{}, cost int64) bool {
31+
return c.cache.Set(key, value, cost)
32+
}
33+
34+
// Get retrieves a value from the cache
35+
func (c *Cache) Get(key interface{}) (interface{}, bool) {
36+
return c.cache.Get(key)
37+
}
38+
39+
// ClearKey clears a specific key from the cache
40+
func (c *Cache) ClearKey(key interface{}) {
41+
c.cache.Del(key)
42+
}
43+
44+
// Clear clears the entire cache
45+
func (c *Cache) Clear() {
46+
c.cache.Clear()
47+
}

cache_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package redis_test
2+
3+
import (
4+
"log"
5+
"testing"
6+
"time"
7+
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
// Testredis.NewCache tests the creation of a new cache instance
12+
func TestNewCache(t *testing.T) {
13+
_, err := redis.NewCache(1000, 1<<20) // 1 MB size
14+
if err != nil {
15+
t.Fatalf("Failed to create cache: %v", err)
16+
}
17+
t.Log("Cache created successfully")
18+
}
19+
20+
// TestCacheSetAndGet tests setting and getting values in the cache
21+
func TestCacheSetAndGet(t *testing.T) {
22+
cache, err := redis.NewCache(1000, 1<<20)
23+
if err != nil {
24+
t.Fatalf("Failed to create cache: %v", err)
25+
}
26+
27+
key, value := "key1", "value1"
28+
setSuccess := cache.Set(key, value, 1)
29+
if !setSuccess {
30+
t.Fatalf("Failed to set key: %s", key)
31+
}
32+
log.Printf("Set operation successful for key: %s", key)
33+
34+
// Allow value to pass through buffers
35+
time.Sleep(10 * time.Millisecond)
36+
37+
getValue, found := cache.Get(key)
38+
if !found || getValue != value {
39+
t.Errorf("Failed to get key: %s, expected value: %s, got: %v", key, value, getValue)
40+
} else {
41+
log.Printf("Get operation successful for key: %s", key)
42+
}
43+
}
44+
45+
// TestCacheClearKey tests the clearing of a specific key from the cache
46+
func TestCacheClearKey(t *testing.T) {
47+
cache, err := redis.NewCache(1000, 1<<20)
48+
if err != nil {
49+
t.Fatalf("Failed to create cache: %v", err)
50+
}
51+
52+
key := "key1"
53+
cache.Set(key, "value1", 1)
54+
log.Printf("Key %s set in cache", key)
55+
56+
time.Sleep(10 * time.Millisecond)
57+
58+
cache.ClearKey(key)
59+
log.Printf("Key %s cleared from cache", key)
60+
61+
time.Sleep(10 * time.Millisecond)
62+
63+
_, found := cache.Get(key)
64+
if found {
65+
t.Errorf("Expected key %s to be cleared", key)
66+
} else {
67+
log.Printf("ClearKey operation successful for key: %s", key)
68+
}
69+
}
70+
71+
// TestCacheClear tests clearing all keys from the cache
72+
func TestCacheClear(t *testing.T) {
73+
cache, err := redis.NewCache(1000, 1<<20)
74+
if err != nil {
75+
t.Fatalf("Failed to create cache: %v", err)
76+
}
77+
78+
key := "key1"
79+
cache.Set(key, "value1", 1)
80+
log.Printf("Key %s set in cache", key)
81+
82+
time.Sleep(10 * time.Millisecond)
83+
84+
cache.Clear()
85+
t.Log("All keys cleared from cache")
86+
87+
time.Sleep(10 * time.Millisecond)
88+
89+
_, found := cache.Get(key)
90+
if found {
91+
t.Errorf("Expected cache to be cleared, but key %s was found", key)
92+
} else {
93+
t.Log("Clear operation successful, cache is empty")
94+
}
95+
}

0 commit comments

Comments
 (0)