-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.go
50 lines (42 loc) · 1.22 KB
/
opt.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
package ttlmap
import "time"
type cacheOptions struct {
cleanupDuration time.Duration
defaultCacheDuration time.Duration
maxLifetime time.Duration
shardCount int
}
func defaultCacheOptions() cacheOptions {
return cacheOptions{
cleanupDuration: time.Minute,
defaultCacheDuration: time.Hour,
maxLifetime: 365 * (24 * time.Hour),
shardCount: 32,
}
}
// CacheOption configures how we set up the cache map
type CacheOption func(options *cacheOptions)
// WithShardSize With a custom sub map shard size
func WithShardSize(shardSize int) CacheOption {
return func(o *cacheOptions) {
o.shardCount = shardSize
}
}
// WithDefaultTTL Sets the default duration for items stored
func WithDefaultTTL(ttl time.Duration) CacheOption {
return func(o *cacheOptions) {
o.defaultCacheDuration = ttl
}
}
// WithCleanupDuration Sets how frequently to cleanup expired items
func WithCleanupDuration(ttl time.Duration) CacheOption {
return func(o *cacheOptions) {
o.cleanupDuration = ttl
}
}
// WithMaxLifetime Sets the maximum amount of time an item can exist within the cache
func WithMaxLifetime(ttl time.Duration) CacheOption {
return func(o *cacheOptions) {
o.maxLifetime = ttl
}
}