-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
72 lines (57 loc) · 1.86 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
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
package test
import (
"github.com/alexfalkowski/go-service/cache/redis"
rem "github.com/alexfalkowski/go-service/cache/redis/telemetry/metrics"
"github.com/alexfalkowski/go-service/cache/ristretto"
rim "github.com/alexfalkowski/go-service/cache/ristretto/telemetry/metrics"
gr "github.com/alexfalkowski/go-service/redis"
sr "github.com/alexfalkowski/go-service/ristretto"
"github.com/alexfalkowski/go-service/runtime"
"github.com/alexfalkowski/go-service/telemetry/tracer"
"github.com/go-redis/cache/v9"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
"go.uber.org/zap"
)
// Cache for test.
type Cache struct {
Lifecycle fx.Lifecycle
Redis *redis.Config
Logger *zap.Logger
Tracer *tracer.Config
Meter metric.Meter
}
// NewRedisCache for test.
func (c *Cache) NewRedisCache() (*cache.Cache, error) {
cl, err := c.NewRedisClient()
if err != nil {
return nil, err
}
params := redis.OptionsParams{Client: cl, Config: c.Redis, Marshaller: Marshaller, Map: Compressor}
opts, err := redis.NewOptions(params)
if err != nil {
return nil, err
}
cache := redis.NewCache(opts)
rem.Register(cache, c.Meter)
return cache, nil
}
// NewRedisClient for test.
func (c *Cache) NewRedisClient() (gr.Client, error) {
tracer, err := tracer.NewTracer(c.Lifecycle, Environment, Version, Name, c.Tracer, c.Logger)
if err != nil {
return nil, err
}
opts, err := redis.NewRingOptions(c.Redis)
runtime.Must(err)
client := redis.NewClient(redis.ClientParams{Lifecycle: c.Lifecycle, RingOptions: opts, Tracer: tracer, Logger: c.Logger})
return client, nil
}
// NewRistrettoCache for test.
func (c *Cache) NewRistrettoCache() sr.Cache {
cfg := &ristretto.Config{NumCounters: 1e7, MaxCost: 1 << 30, BufferItems: 64}
ca, err := ristretto.NewCache(ristretto.CacheParams{Lifecycle: c.Lifecycle, Config: cfg})
runtime.Must(err)
rim.Register(ca, c.Meter)
return ca
}