-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoptions.go
40 lines (34 loc) · 938 Bytes
/
options.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
package cache
import "time"
// Option func type
type Option func(lc *cacheImpl) error
// OnEvicted called automatically for automatically and manually deleted entries
func OnEvicted(fn func(key string, value interface{})) Option {
return func(lc *cacheImpl) error {
lc.onEvicted = fn
return nil
}
}
// MaxKeys functional option defines how many keys to keep.
// By default it is 0, which means unlimited.
func MaxKeys(maximum int) Option {
return func(lc *cacheImpl) error {
lc.maxKeys = maximum
return nil
}
}
// TTL functional option defines TTL for all cache entries.
// By default it is set to 10 years, sane option for expirable cache might be 5 minutes.
func TTL(ttl time.Duration) Option {
return func(lc *cacheImpl) error {
lc.ttl = ttl
return nil
}
}
// LRU sets cache to LRU (Least Recently Used) eviction mode.
func LRU() Option {
return func(lc *cacheImpl) error {
lc.isLRU = true
return nil
}
}