|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + // ErrNotFound is an error which indicate an item is not found. |
| 12 | + ErrNotFound = errors.New("not found item") |
| 13 | + |
| 14 | + // ErrExpired is an error which indicate an item is expired. |
| 15 | + ErrExpired = errors.New("expired item") |
| 16 | +) |
| 17 | + |
| 18 | +// Item is an item |
| 19 | +type Item[T any] struct { |
| 20 | + Value T |
| 21 | + Expiration time.Duration |
| 22 | + CreatedAt time.Time |
| 23 | +} |
| 24 | + |
| 25 | +var nowFunc = time.Now |
| 26 | + |
| 27 | +// HasExpired returns true if the item has expired. |
| 28 | +// If the item's expiration is zero value, returns false. |
| 29 | +func (i Item[T]) HasExpired() bool { |
| 30 | + if i.Expiration <= 0 { |
| 31 | + return false |
| 32 | + } |
| 33 | + return i.CreatedAt.Add(i.Expiration).Before(nowFunc()) |
| 34 | +} |
| 35 | + |
| 36 | +// Cache is a base struct for creating in-memory cache. |
| 37 | +type Cache[K comparable, V any] struct { |
| 38 | + items map[K]Item[V] |
| 39 | + mu sync.RWMutex |
| 40 | +} |
| 41 | + |
| 42 | +// New creates a new cache. |
| 43 | +func New[K comparable, V any]() *Cache[K, V] { |
| 44 | + return &Cache[K, V]{ |
| 45 | + items: make(map[K]Item[V]), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// ItemOption is an option for cache item. |
| 50 | +type ItemOption func(o *options) |
| 51 | + |
| 52 | +type options struct { |
| 53 | + expiration time.Duration // default none |
| 54 | +} |
| 55 | + |
| 56 | +// WithExpiration is an option to set expiration time for any items. |
| 57 | +func WithExpiration(exp time.Duration) ItemOption { |
| 58 | + return func(o *options) { |
| 59 | + o.expiration = exp |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Set sets any item to the cache. replacing any existing item. |
| 64 | +// The default item never expires. |
| 65 | +func (c *Cache[K, V]) Set(k K, v V, opts ...ItemOption) { |
| 66 | + o := new(options) |
| 67 | + for _, optFunc := range opts { |
| 68 | + optFunc(o) |
| 69 | + } |
| 70 | + item := Item[V]{ |
| 71 | + Value: v, |
| 72 | + Expiration: o.expiration, |
| 73 | + CreatedAt: nowFunc(), |
| 74 | + } |
| 75 | + c.SetItem(k, item) |
| 76 | +} |
| 77 | + |
| 78 | +// SetItem sets any item to the cache. replacing any existing item. |
| 79 | +// The default item never expires. |
| 80 | +func (c *Cache[K, V]) SetItem(k K, v Item[V]) { |
| 81 | + c.mu.Lock() |
| 82 | + c.items[k] = v |
| 83 | + c.mu.Unlock() |
| 84 | +} |
| 85 | + |
| 86 | +// Get gets an item from the cache. |
| 87 | +// Returns the item or zero value, and a bool indicating whether the key was found. |
| 88 | +func (c *Cache[K, V]) Get(k K) (val V, ok bool) { |
| 89 | + item, err := c.GetItem(k) |
| 90 | + if err != nil { |
| 91 | + return |
| 92 | + } |
| 93 | + return item.Value, true |
| 94 | +} |
| 95 | + |
| 96 | +// GetItem gets an item from the cache. |
| 97 | +// Returns an error if the item was not found or expired. If there is no error, the |
| 98 | +// incremented value is returned. |
| 99 | +func (c *Cache[K, V]) GetItem(k K) (val Item[V], _ error) { |
| 100 | + c.mu.RLock() |
| 101 | + defer c.mu.RUnlock() |
| 102 | + |
| 103 | + got, found := c.items[k] |
| 104 | + if !found { |
| 105 | + return val, fmt.Errorf("key[%v]: %w", k, ErrNotFound) |
| 106 | + } |
| 107 | + if got.HasExpired() { |
| 108 | + return val, fmt.Errorf("key[%v]: %w", k, ErrExpired) |
| 109 | + } |
| 110 | + return got, nil |
| 111 | +} |
| 112 | + |
| 113 | +// Keys returns cache keys. the order is random. |
| 114 | +func (c *Cache[K, _]) Keys() []K { |
| 115 | + ret := make([]K, 0, len(c.items)) |
| 116 | + for key := range c.items { |
| 117 | + ret = append(ret, key) |
| 118 | + } |
| 119 | + return ret |
| 120 | +} |
| 121 | + |
| 122 | +// NumberCache is a in-memory cache which is able to store only Number constraint. |
| 123 | +type NumberCache[K comparable, V Number] struct { |
| 124 | + *Cache[K, V] |
| 125 | +} |
| 126 | + |
| 127 | +// NewNumber creates a new cache for Number constraint. |
| 128 | +func NewNumber[K comparable, V Number]() *NumberCache[K, V] { |
| 129 | + return &NumberCache[K, V]{ |
| 130 | + Cache: New[K, V](), |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// Increment an item of type Number constraint by n. |
| 135 | +// Returns an error if the item was not found or expired. If there is no error, the |
| 136 | +// incremented value is returned. |
| 137 | +func (nc *NumberCache[K, V]) Increment(k K, n V) (val V, err error) { |
| 138 | + got, err := nc.Cache.GetItem(k) |
| 139 | + if err != nil { |
| 140 | + return val, err |
| 141 | + } |
| 142 | + |
| 143 | + nv := got.Value + n |
| 144 | + got.Value = nv |
| 145 | + nc.Cache.SetItem(k, got) |
| 146 | + |
| 147 | + return nv, nil |
| 148 | +} |
| 149 | + |
| 150 | +// Decrement an item of type Number constraint by n. |
| 151 | +// Returns an error if the item was not found or expired. If there is no error, the |
| 152 | +// decremented value is returned. |
| 153 | +func (nc *NumberCache[K, V]) Decrement(k K, n V) (val V, err error) { |
| 154 | + got, err := nc.Cache.GetItem(k) |
| 155 | + if err != nil { |
| 156 | + return val, err |
| 157 | + } |
| 158 | + |
| 159 | + nv := got.Value - n |
| 160 | + got.Value = nv |
| 161 | + nc.Cache.SetItem(k, got) |
| 162 | + |
| 163 | + return nv, nil |
| 164 | +} |
0 commit comments