-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis_cache.go
More file actions
122 lines (102 loc) · 3.13 KB
/
redis_cache.go
File metadata and controls
122 lines (102 loc) · 3.13 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
package ratelimit
import (
"context"
"encoding/binary"
"fmt"
"math"
"time"
"github.com/redis/go-redis/v9"
)
// RedisCache implements the Cache interface using Redis
type RedisCache struct {
client *redis.Client
keyPrefix string
ttl time.Duration
ctx context.Context
}
// RedisConfig holds configuration for the Redis cache
type RedisConfig struct {
Addr string // Redis server address (e.g., "localhost:6379")
Password string // Redis password (empty for no auth)
DB int // Redis database number
KeyPrefix string // Prefix for all keys (e.g., "ratelimit:")
TTL time.Duration // Default TTL for keys
}
// NewRedisCache creates a new Redis cache with the specified configuration
func NewRedisCache(config RedisConfig) (*RedisCache, error) {
rdb := redis.NewClient(&redis.Options{
Addr: config.Addr,
Password: config.Password,
DB: config.DB,
})
// Test the connection with a timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := rdb.Ping(ctx).Err(); err != nil {
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
}
// Set default key prefix if not specified
keyPrefix := config.KeyPrefix
if keyPrefix == "" {
keyPrefix = "ratelimit:"
}
return &RedisCache{
client: rdb,
keyPrefix: keyPrefix,
ttl: config.TTL,
ctx: context.Background(),
}, nil
}
// Get retrieves a value from Redis
func (r *RedisCache) Get(key uint64) *ClientLimiter {
redisKey := r.getRedisKey(key)
val, err := r.client.Get(r.ctx, redisKey).Bytes()
if err != nil {
if err == redis.Nil {
return nil
}
// Log error but don't fail - return nil to indicate cache miss
return nil
}
if len(val) < 16 { // 8 bytes for float64 + 8 bytes for int64 timestamp
return nil
}
limiter := &ClientLimiter{
allowedRequests: math.Float64frombits(binary.LittleEndian.Uint64(val[0:8])),
lastRequest: time.Unix(0, int64(binary.LittleEndian.Uint64(val[8:16]))),
}
return limiter
}
// Set stores a value in Redis
func (r *RedisCache) Set(key uint64, value *ClientLimiter) {
redisKey := r.getRedisKey(key)
// Pack into 16 bytes: 8 bytes float64 + 8 bytes int64 timestamp
data := make([]byte, 16)
binary.LittleEndian.PutUint64(data[0:8], math.Float64bits(value.allowedRequests))
binary.LittleEndian.PutUint64(data[8:16], uint64(value.lastRequest.UnixNano()))
r.client.Set(r.ctx, redisKey, data, r.ttl)
}
// Delete removes a value from Redis
func (r *RedisCache) Delete(key uint64) {
redisKey := r.getRedisKey(key)
r.client.Del(r.ctx, redisKey)
}
// Clear removes all entries from the cache by deleting all keys with the prefix
func (r *RedisCache) Clear() {
pattern := r.keyPrefix + "*"
keys, err := r.client.Keys(r.ctx, pattern).Result()
if err != nil {
return // Fail silently
}
if len(keys) > 0 {
r.client.Del(r.ctx, keys...)
}
}
// Close closes the Redis connection
func (r *RedisCache) Close() error {
return r.client.Close()
}
// getRedisKey creates a Redis key with the configured prefix
func (r *RedisCache) getRedisKey(key uint64) string {
return fmt.Sprintf("%s%x", r.keyPrefix, key)
}