-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache.go
159 lines (128 loc) · 3.04 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package levelcache
import (
"crypto/md5"
)
const (
version int = 1000
bucketLimit int = 256
)
type Hash [md5.Size]byte
type DevConf struct {
Name string
Dir string
Capacity int
}
type Config struct {
MetaDir string
ActionParallel int
AuxFactory AuxFactory
}
type Cache struct {
conf Config
meta *meta
devices []*device
}
type Auxiliary interface {
Add(key Hash, auxItem interface{})
Get(key Hash) interface{}
Del(key Hash)
Load(path string)
Dump(path string)
}
type AuxFactory func(idx int) Auxiliary
type Matcher func(aux Auxiliary) []Hash
func NewCache(conf Config, devices []DevConf) *Cache {
cache := &Cache{
conf: conf,
meta: newMeta(conf.MetaDir, conf.AuxFactory),
devices: make([]*device, len(devices))}
for lv, devConf := range devices {
cache.devices[lv] = newDevice(lv, devConf)
}
return cache
}
func (c *Cache) Close() {
for _, d := range c.devices {
d.close()
}
}
func (c *Cache) Dump() {
c.meta.dump(c.conf.ActionParallel)
for _, d := range c.devices {
d.dump(c.conf.ActionParallel)
}
}
func (c *Cache) levelUp(currentLevel int, key Hash, segIndex uint32, data []byte) {
if currentLevel >= len(c.devices)-1 {
return
}
// TODO, 更复杂的判断逻辑
d := c.devices[currentLevel+1]
d.add(key, segIndex, data)
}
func (c *Cache) Get(key Hash, start int, end int) (dataList [][]byte, hitDevs []string, missSegments [][2]int) {
item := c.meta.get(key)
if item == nil {
return nil, nil, nil
}
if end == -1 {
end = int(item.Size)
}
startSeg := uint32(start / int(item.SegSize))
endSeg := uint32(end / int(item.SegSize))
dataList = make([][]byte, 0)
missSegments = make([][2]int, 0)
hitDevs = make([]string, 0)
for seg := startSeg; seg <= endSeg; seg++ {
found := false
for lv := len(c.devices) - 1; lv >= 0; lv-- {
d := c.devices[lv]
if tmp := d.get(key, seg); tmp != nil {
dataList = append(dataList, tmp)
c.levelUp(lv, key, seg, tmp)
hitDevs = append(hitDevs, d.conf.Name)
found = true
break
}
}
if !found {
segment := [2]int{
int(seg * item.SegSize),
int(seg*item.SegSize + seg)}
missSegments = append(missSegments, segment)
}
}
return dataList, hitDevs, missSegments
}
func (c *Cache) AddItem(key Hash, expire, size int64, auxData interface{}) {
const maxSegSize uint32 = 1024 * 1024 * 64
const minSegSize uint32 = 1024 * 1024
const defaultSegCount int64 = 1024
segSize := uint32(size / defaultSegCount)
if segSize < minSegSize {
segSize = minSegSize
}
if segSize > maxSegSize {
segSize = maxSegSize
}
item := &item{
Expire: expire,
Size: size,
SegSize: segSize,
Segments: make([]uint32, 0)}
c.meta.addItem(key, item, auxData)
}
func (c *Cache) AddSegment(key Hash, start int, data []byte) {
c.meta.addSegment(key, start, start+len(data), func(segIndex uint32) {
c.devices[0].add(key, segIndex, data)
})
}
func (c *Cache) Del(k Hash) {
c.meta.del(k)
for _, d := range c.devices {
d.del(k)
}
}
func (c *Cache) DelBatch(m Matcher) {
c.meta.delBatch(c.conf.ActionParallel, m)
}