-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
83 lines (71 loc) · 1.31 KB
/
cache.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
83
package main
import (
// "os"
// "fmt"
// "time"
"sync"
// "sync/atiom"
)
type item struct {
name string
when time.Time
}
type cache struct {
buf chan *item
idx map[string]byte
//rwm *sync.RWMutex
mtx *sync.Mutex
dur time.Duration
}
func NewCache(dur time.Duration) (c *cache) {
c = &cache{
buf: make(chan *item),
idx: make(map[string]byte),
//rwm: new(sync.RWMutex),
mtx: new(sync.Mutex),
dur: dur,
}
return
}
/*
func (c *cache)check(name string) (e bool){
c.rwm.RLock()
_, e = c.idx[name]
c.rwm.RUnlock()
if e { return }
c.rwm.Lock()
c.idx[name] = 1
c.rwm.Unlock()
c.buf <- &item{name, 0} // TODO init dead
return
}
*/
func (c *cache)check(name string) (e bool){
c.mtx.Lock()
_, e = c.idx[name]
if !e {
c.idx[name] = 1
}
c.rwm.Unlock()
if !e {
c.buf <- &item{name, time.Now().Add(c.dur)}
}
return
}
func (c *cache)clean() {
for i := range c.buf {
<-time.After(c.i.when.Sub(time.Now()))
c.rwm.Lock()
delete(c.idx, i.name)
c.rwm.Unlock()
}
}
func main() {
m := new(sync.RWMutex)
println("new RWMutex")
m.RLock()
println("got RLock")
m.RUnlock()
m.Lock()
println("Got Lock")
}