-
Notifications
You must be signed in to change notification settings - Fork 9
/
inmemory.go
86 lines (77 loc) · 2.49 KB
/
inmemory.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
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package inmemory
import (
"context"
"fmt"
"time"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/modules/cacher/definition"
)
type cacher[T any] struct {
config configs.Configuration
cache *lru.TwoQueueCache[string, T]
prefix string
fetcher definition.Fetcher[T]
}
// New returns a new Cacher.
func New[T any](config configs.Configuration, prefix string, fetcher definition.Fetcher[T]) (definition.Cacher[T], error) {
cache, err := lru.New2Q[string, T](config.Cache.Inmemory.Size)
if err != nil {
return nil, err
}
return &cacher[T]{
config: config,
cache: cache,
prefix: prefix,
fetcher: fetcher,
}, nil
}
// Set sets the value of given key if it is new to the cache.
// Param val should not be nil.
func (c *cacher[T]) Set(ctx context.Context, key string, val T, _ ...time.Duration) error {
c.cache.Add(definition.GenKey(c.config, c.prefix, key), val)
return nil
}
// Get tries to fetch a value corresponding to the given key from the cache.
// If error occurs during the first time fetching, it will be cached until the
// sequential fetching triggered by the refresh goroutine succeed.
func (c *cacher[T]) Get(ctx context.Context, key string) (T, error) {
result, ok := c.cache.Get(definition.GenKey(c.config, c.prefix, key))
if !ok {
if c.fetcher == nil {
return result, definition.ErrNotFound
}
result, err := c.fetcher(key)
if err != nil {
return result, err
}
err = c.Set(ctx, key, result)
if err != nil {
return result, fmt.Errorf("Set value failed: %w", err)
}
return result, nil
}
err := c.Set(ctx, key, result)
if err != nil {
return result, fmt.Errorf("Set value failed: %w", err)
}
return result, nil
}
// Del deletes the value corresponding to the given key from the cache.
func (c *cacher[T]) Del(ctx context.Context, key string) error {
c.cache.Remove(definition.GenKey(c.config, c.prefix, key))
return nil
}