-
Notifications
You must be signed in to change notification settings - Fork 0
/
rimcu.go
86 lines (71 loc) · 2.07 KB
/
rimcu.go
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
package rimcu
import (
"github.com/iwanbk/rimcu/logger"
)
// Protocol represents the underlying Redis protocol used by rimcu.
// It currently support RESP2 & RESP3 (experimental)
type Protocol string
const (
// ProtoResp2 represent RESP2 protocol that is used from Redis 2
ProtoResp2 Protocol = "RESP2"
// ProtoResp2ClusterProxy represent RESP2 protocol on redis cluster
// with front proxy
ProtoResp2ClusterProxy Protocol = "RESP2ClusterProxy"
// ProtoResp3 represents RESP3 protocol that supported since Redis 6
ProtoResp3 Protocol = "RESP3"
)
// Config represents config of Cache
type Config struct {
// redis server address
// in case of RESP2ClusterProxy protocol, it is the address of the proxy
ServerAddr string
// Redis password
Password string
// size of the in memory cache
// Default is 10K
CacheSize int
// Protocol of redis being used.
//
// The default is ProtoResp2
Protocol Protocol
// Logger to be used, the default logger will print nothing
Logger logger.Logger
// ClusterNodes is a list of cluster nodes
// only being used by ProtoResp2ClusterProxy protocol.
ClusterNodes []string
}
// Rimcu is a redis client which implements client side caching.
// It is safe for concurrent use by multiples goroutine
type Rimcu struct {
serverAddr string
logger logger.Logger
protocol Protocol
clusterNodes []string
password string
}
// New creates a new Rimcu redis client
func New(cfg Config) *Rimcu {
if cfg.Logger == nil {
cfg.Logger = logger.NewDefault()
}
return &Rimcu{
serverAddr: cfg.ServerAddr,
logger: cfg.Logger,
protocol: cfg.Protocol,
clusterNodes: cfg.ClusterNodes,
password: cfg.Password,
}
}
// NewStringsCache creates a new strings cache and do the required initialization
func (r *Rimcu) NewStringsCache(cfg StringsCacheConfig) (*StringsCache, error) {
cfg.logger = r.logger
cfg.serverAddr = r.serverAddr
cfg.protocol = r.protocol
cfg.clusterNodes = r.clusterNodes
cfg.password = r.password
return newStringsCache(cfg)
}
const (
defaultCacheSize = 100000
defaultCacheTTLSec = 60 * 20
)