-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
43 lines (35 loc) · 1.73 KB
/
cache.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
package test
import (
"github.com/alexfalkowski/go-service/cache/redis"
"github.com/alexfalkowski/go-service/cache/redis/client"
"github.com/alexfalkowski/go-service/cache/redis/trace/opentracing"
cristretto "github.com/alexfalkowski/go-service/cache/ristretto"
"github.com/alexfalkowski/go-service/compressor"
"github.com/alexfalkowski/go-service/marshaller"
"github.com/dgraph-io/ristretto"
"github.com/go-redis/cache/v8"
"go.uber.org/fx"
"go.uber.org/zap"
)
// NewRedisCache for test.
func NewRedisCache(lc fx.Lifecycle, host string, logger *zap.Logger, compressor compressor.Compressor, marshaller marshaller.Marshaller) *cache.Cache {
params := redis.OptionsParams{Client: NewRedisClient(lc, host, logger), Compressor: compressor, Marshaller: marshaller}
opts := redis.NewOptions(params)
return redis.NewCache(redis.CacheParams{Lifecycle: lc, Config: NewRedisConfig(host), Options: opts, Version: Version})
}
// NewRedisClient for test.
func NewRedisClient(lc fx.Lifecycle, host string, logger *zap.Logger) client.Client {
tracer, _ := opentracing.NewTracer(opentracing.TracerParams{Lifecycle: lc, Config: NewJaegerConfig(), Version: Version})
client := redis.NewClient(redis.ClientParams{Lifecycle: lc, RingOptions: redis.NewRingOptions(NewRedisConfig(host)), Tracer: tracer, Logger: logger})
return client
}
// NewRedisConfig for test.
func NewRedisConfig(host string) *redis.Config {
return &redis.Config{Addresses: map[string]string{"server": host}}
}
// NewRistrettoCache for test.
func NewRistrettoCache(lc fx.Lifecycle) *ristretto.Cache {
cfg := &cristretto.Config{NumCounters: 1e7, MaxCost: 1 << 30, BufferItems: 64}
c, _ := cristretto.NewCache(cristretto.CacheParams{Lifecycle: lc, Config: cfg, Version: Version})
return c
}