forked from karrick/congomap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
congomap.go
82 lines (70 loc) · 2.28 KB
/
congomap.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package congomap
import (
"time"
)
// Congomap objects are useful when you need a concurrent go map.
type Congomap interface {
Close() error
Delete(string)
GC()
Halt()
Keys() []string
Load(string) (interface{}, bool)
LoadStore(string) (interface{}, error)
Lookup(func(string) (interface{}, error)) error
Pairs() <-chan *Pair
Reaper(func(interface{})) error
Store(string, interface{})
TTL(time.Duration) error
}
// Pair objects represent a single key-value pair and are passed
// through the channel returned by the Pairs() method while
// enumerating through the keys and values stored in a Congomap.
type Pair struct {
Key string
Value interface{}
}
// Setter declares the type of function used when creating a
// Congomap to change the instance's behavior.
type Setter func(Congomap) error
// Lookup is used to specify what function is to be called to retrieve
// the value for a key when the LoadStore() method is invoked for a
// key not found in a Congomap.
func Lookup(lookup func(string) (interface{}, error)) Setter {
return func(cgm Congomap) error {
return cgm.Lookup(lookup)
}
}
// Reaper is used to specify what function is to be called when
// garbage collecting item from the Congomap.
func Reaper(reaper func(interface{})) Setter {
return func(cgm Congomap) error {
return cgm.Reaper(reaper)
}
}
// TTL is used to specify the time-to-live for a key-value pair in the
// Congomap. Pairs that have expired are not immediately Garbage
// Collected until replaced by a new value, or the GC() method is
// invoked either manually or periodically.
func TTL(duration time.Duration) Setter {
return func(cgm Congomap) error {
return cgm.TTL(duration)
}
}
type expiringValue struct {
value interface{}
expiry int64
}
// ErrNoLookupDefined is returned by LoadStore() method when a key is
// not found in a Congomap for which there has been no lookup function
// declared.
type ErrNoLookupDefined struct{}
func (e ErrNoLookupDefined) Error() string {
return "congomap: no lookup callback function set"
}
// ErrInvalidDuration is returned by TTL() function when a
// time-to-live of less than or equal to zero is specified.
type ErrInvalidDuration time.Duration
func (e ErrInvalidDuration) Error() string {
return "congomap: duration must be greater than 0: " + time.Duration(e).String()
}