-
Notifications
You must be signed in to change notification settings - Fork 2
/
icache.go
58 lines (44 loc) · 1.5 KB
/
icache.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
package icache
import (
"errors"
"time"
)
// ErrNotFound means the pot couldn't find the entry with the given key
var ErrNotFound = errors.New("entry not found")
// ErrNotClosable means the pot doesn't have invalidation strategy.
var ErrNotClosable = errors.New("pot cannot be closed")
// Pot holds your cached data
type Pot[T any] interface {
// Purge invalidates all entries
Purge()
// Len returns count of the entries
Len() (l int)
// Drop invalidates the entry with the given key
Drop(key ...string)
// DropTags invalidates all entries with the given tags
DropTags(tags ...string)
// Exists checks if the entry with the given key exists
Exists(key string) bool
// Set stores the variable in the given key entry
// tags can be set or ignored
Set(k string, v T, tags ...string)
// Get restores the T previously stored as the given key
// returns ErrNotFound if the key doesn't exist.
Get(key string) (v T, err error)
// ExpireTime returns expire time of the given entry
ExpireTime(key string) (t *time.Time, err error)
// Close stops the invalidation functionality goroutines.
// - After closing, the pot resets and next entries you add won't expire. so don't use the pot after closing it.
// - Pots without invalidation strategy cannot be closed.
// - Panics if the pot is already closed.
Close() error
}
// NewPot creates new cache Pot with the given options.
func NewPot[T any](options ...Option) Pot[T] {
p := new(pot[T])
for _, option := range options {
option(p)
}
p.init()
return p
}