...
1 package ttlcache
2
3 import "time"
4
5
6 type Option[K comparable, V any] interface {
7 apply(opts *options[K, V])
8 }
9
10
11 type optionFunc[K comparable, V any] func(*options[K, V])
12
13
14 func (fn optionFunc[K, V]) apply(opts *options[K, V]) {
15 fn(opts)
16 }
17
18
19 type options[K comparable, V any] struct {
20 capacity uint64
21 ttl time.Duration
22 loader Loader[K, V]
23 disableTouchOnHit bool
24 enableVersionTracking bool
25 }
26
27
28 func applyOptions[K comparable, V any](v *options[K, V], opts ...Option[K, V]) {
29 for i := range opts {
30 opts[i].apply(v)
31 }
32 }
33
34
35
36 func WithCapacity[K comparable, V any](c uint64) Option[K, V] {
37 return optionFunc[K, V](func(opts *options[K, V]) {
38 opts.capacity = c
39 })
40 }
41
42
43
44 func WithTTL[K comparable, V any](ttl time.Duration) Option[K, V] {
45 return optionFunc[K, V](func(opts *options[K, V]) {
46 opts.ttl = ttl
47 })
48 }
49
50
51
52
53 func WithVersion[K comparable, V any](enable bool) Option[K, V] {
54 return optionFunc[K, V](func(opts *options[K, V]) {
55 opts.enableVersionTracking = enable
56 })
57 }
58
59
60
61
62 func WithLoader[K comparable, V any](l Loader[K, V]) Option[K, V] {
63 return optionFunc[K, V](func(opts *options[K, V]) {
64 opts.loader = l
65 })
66 }
67
68
69
70
71
72
73 func WithDisableTouchOnHit[K comparable, V any]() Option[K, V] {
74 return optionFunc[K, V](func(opts *options[K, V]) {
75 opts.disableTouchOnHit = true
76 })
77 }
78
View as plain text