-
Notifications
You must be signed in to change notification settings - Fork 2
/
expirable_emap.go
48 lines (41 loc) · 1.36 KB
/
expirable_emap.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
// Copyright(c) 2016 Ethan Zhuang <[email protected]>.
package emap
import (
"time"
)
// ExpirableValue is the interface which must be implemented by all the value in the expirable EMap.
type ExpirableValue interface {
// IsExpired returns if the value is expired.
// If true, the value will be deleted automatically.
IsExpired() bool
}
// NewExpirableEMap creates a new generic emap with an expiration checker.
// The expiration checker will check all the values in the emap with the period of input interval(milliseconds).
// All value inserted into the expirable emap must implements ExpirableValue interface of this package.
// If a value is expired, it will be deleted automatically.
func NewExpirableEMap(interval int) *GenericEMap {
instance := new(GenericEMap)
instance.values = make(map[interface{}]interface{})
instance.keys = make(map[interface{}][]interface{})
instance.indices = make(map[interface{}][]interface{})
if interval > 0 {
instance.interval = interval
go instance.collect(interval)
}
return instance
}
func (m *GenericEMap) collect(interval int) {
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
for {
select {
case <-ticker.C:
m.mtx.Lock()
for key, value := range m.values {
if value.(ExpirableValue).IsExpired() {
deleteByKey(m.values, m.keys, m.indices, key)
}
}
m.mtx.Unlock()
}
}
}