-
Notifications
You must be signed in to change notification settings - Fork 0
/
evcache.go
72 lines (58 loc) · 2.16 KB
/
evcache.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 evcache
import (
"context"
"github.com/prodadidb/gocache/cache"
"github.com/prodadidb/gocache/store"
)
//go:generate mockgen -destination=./mock_evcache_test.go -package=evcache_test -source=evcache.go
// Interface is Cache interface
type Interface interface {
Get(ctx context.Context, key interface{}) (interface{}, error)
Set(ctx context.Context, key, object interface{}) error
}
// NewCache instantiates adapter for cache.CacheInterface
func NewCache(cache cache.CacheInterface[any], option ...store.Option) Interface {
return &gocacheAdapter{
cache: cache,
option: option,
}
}
type gocacheAdapter struct {
cache cache.CacheInterface[any]
option []store.Option
}
func (c *gocacheAdapter) Get(ctx context.Context, key interface{}) (interface{}, error) {
return c.cache.Get(ctx, key)
}
func (c *gocacheAdapter) Set(ctx context.Context, key, object interface{}) error {
return c.cache.Set(ctx, key, object, c.option...)
}
// Marshaler is interface for marshaler.Marshaler
type Marshaler interface {
Get(ctx context.Context, key interface{}, returnObj interface{}) (interface{}, error)
Set(ctx context.Context, key, object interface{}, options ...store.Option) error
Delete(ctx context.Context, key interface{}) error
Invalidate(ctx context.Context, options ...store.InvalidateOption) error
Clear(ctx context.Context) error
}
// MarshallerReturnObj generates returned object to fill by marshaler.Marshaler
type MarshallerReturnObj func() interface{}
// NewCacheMarshaller generates adapter for marshaler.Marshaler
func NewCacheMarshaller(marshaller Marshaler, returnObj MarshallerReturnObj, option ...store.Option) Interface {
return &gocacheMarshallerAdapter{
marshaller: marshaller,
returnObj: returnObj,
option: option,
}
}
type gocacheMarshallerAdapter struct {
marshaller Marshaler
returnObj MarshallerReturnObj
option []store.Option
}
func (c *gocacheMarshallerAdapter) Get(ctx context.Context, key interface{}) (interface{}, error) {
return c.marshaller.Get(ctx, key, c.returnObj())
}
func (c *gocacheMarshallerAdapter) Set(ctx context.Context, key, object interface{}) error {
return c.marshaller.Set(ctx, key, object, c.option...)
}