|
1 | 1 | package simple
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 |
| - "fmt" |
6 | 4 | "sync"
|
7 |
| - "time" |
8 | 5 |
|
9 | 6 | cache "github.com/Code-Hex/go-generics-cache"
|
10 | 7 | )
|
11 | 8 |
|
12 |
| -var ( |
13 |
| - // ErrNotFound is an error which indicate an item is not found. |
14 |
| - ErrNotFound = errors.New("not found item") |
15 |
| - |
16 |
| - // ErrExpired is an error which indicate an item is expired. |
17 |
| - ErrExpired = errors.New("expired item") |
18 |
| -) |
19 |
| - |
20 |
| -// Item is an item |
21 |
| -type Item[T any] struct { |
22 |
| - Value T |
23 |
| - Expiration time.Duration |
24 |
| - CreatedAt time.Time |
25 |
| -} |
26 |
| - |
27 |
| -var nowFunc = time.Now |
28 |
| - |
29 |
| -// HasExpired returns true if the item has expired. |
30 |
| -// If the item's expiration is zero value, returns false. |
31 |
| -func (i Item[T]) HasExpired() bool { |
32 |
| - if i.Expiration <= 0 { |
33 |
| - return false |
34 |
| - } |
35 |
| - return i.CreatedAt.Add(i.Expiration).Before(nowFunc()) |
36 |
| -} |
37 |
| - |
38 | 9 | // Cache is a simple cache has no clear priority for evict cache.
|
39 | 10 | type Cache[K comparable, V any] struct {
|
40 |
| - items map[K]*Item[V] |
41 |
| - options *options |
42 |
| - mu sync.RWMutex |
| 11 | + items map[K]*cache.Item[K, V] |
| 12 | + mu sync.RWMutex |
43 | 13 | }
|
44 | 14 |
|
45 | 15 | var _ cache.Cache[interface{}, any] = (*Cache[interface{}, any])(nil)
|
46 | 16 |
|
47 | 17 | // NewCache creates a new cache.
|
48 |
| -func NewCache[K comparable, V any](opts ...Option) *Cache[K, V] { |
49 |
| - o := new(options) |
50 |
| - for _, optFunc := range opts { |
51 |
| - optFunc(o) |
52 |
| - } |
| 18 | +func NewCache[K comparable, V any]() *Cache[K, V] { |
53 | 19 | return &Cache[K, V]{
|
54 |
| - items: make(map[K]*Item[V]), |
55 |
| - options: o, |
56 |
| - } |
57 |
| -} |
58 |
| - |
59 |
| -// Option is an option for cache. |
60 |
| -type Option func(o *options) |
61 |
| - |
62 |
| -type options struct { |
63 |
| - expiration time.Duration // default none |
64 |
| -} |
65 |
| - |
66 |
| -// WithExpiration is an option to set expiration time for any items. |
67 |
| -func WithExpiration(exp time.Duration) Option { |
68 |
| - return func(o *options) { |
69 |
| - o.expiration = exp |
| 20 | + items: make(map[K]*cache.Item[K, V]), |
70 | 21 | }
|
71 | 22 | }
|
72 | 23 |
|
73 | 24 | // Set sets any item to the cache. replacing any existing item.
|
74 | 25 | // The default item never expires.
|
75 |
| -func (c *Cache[K, V]) Set(k K, v V) { |
76 |
| - item := &Item[V]{ |
77 |
| - Value: v, |
78 |
| - Expiration: c.options.expiration, |
79 |
| - CreatedAt: nowFunc(), |
80 |
| - } |
81 |
| - c.SetItem(k, item) |
82 |
| -} |
83 |
| - |
84 |
| -// SetItem sets any item to the cache. replacing any existing item. |
85 |
| -// The default item never expires. |
86 |
| -func (c *Cache[K, V]) SetItem(k K, v *Item[V]) { |
| 26 | +func (c *Cache[K, V]) Set(k K, v V, opts ...cache.ItemOption) { |
87 | 27 | c.mu.Lock()
|
88 |
| - c.items[k] = v |
| 28 | + c.items[k] = cache.NewItem(k, v, opts...) |
89 | 29 | c.mu.Unlock()
|
90 | 30 | }
|
91 | 31 |
|
92 | 32 | // Get gets an item from the cache.
|
93 | 33 | // Returns the item or zero value, and a bool indicating whether the key was found.
|
94 | 34 | func (c *Cache[K, V]) Get(k K) (val V, ok bool) {
|
95 |
| - item, err := c.GetItem(k) |
96 |
| - if err != nil { |
97 |
| - return |
98 |
| - } |
99 |
| - return item.Value, true |
100 |
| -} |
101 |
| - |
102 |
| -// GetItem gets an item from the cache. |
103 |
| -// Returns an error if the item was not found or expired. If there is no error, the |
104 |
| -// incremented value is returned. |
105 |
| -func (c *Cache[K, V]) GetItem(k K) (val *Item[V], _ error) { |
106 | 35 | c.mu.RLock()
|
107 | 36 | defer c.mu.RUnlock()
|
108 | 37 |
|
109 | 38 | got, found := c.items[k]
|
110 | 39 | if !found {
|
111 |
| - return nil, fmt.Errorf("key[%v]: %w", k, ErrNotFound) |
| 40 | + return |
112 | 41 | }
|
113 | 42 | if got.HasExpired() {
|
114 |
| - return nil, fmt.Errorf("key[%v]: %w", k, ErrExpired) |
| 43 | + return |
115 | 44 | }
|
116 |
| - return got, nil |
| 45 | + return got.Value, true |
117 | 46 | }
|
118 | 47 |
|
119 | 48 | // Keys returns cache keys. the order is random.
|
|
0 commit comments