/
memory.go
120 lines (101 loc) · 2.41 KB
/
memory.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
package teacache
import (
"errors"
"github.com/TeaWeb/build/internal/teamemory"
"strings"
"time"
)
// 内存缓存管理器
type MemoryManager struct {
Manager
Capacity float64 // 容量
Life time.Duration // 有效期
grid *teamemory.Grid
}
func NewMemoryManager() *MemoryManager {
m := &MemoryManager{}
return m
}
func (this *MemoryManager) SetOptions(options map[string]interface{}) {
if this.Life <= 0 {
this.Life = 1800 * time.Second
}
countCells := 128
opts := []interface{}{}
if this.Capacity > 0 {
capacityBytes := int64(this.Capacity)
opts = append(opts, teamemory.NewLimitSizeOpt(capacityBytes))
countCells = int(capacityBytes / 1024 / 1024 / 128)
}
this.grid = teamemory.NewGrid(countCells, opts...)
}
func (this *MemoryManager) Write(key string, data []byte) error {
if this.grid == nil {
return errors.New("grid has not been initialized")
}
life := int64(this.Life.Seconds())
this.grid.WriteBytes([]byte(key), data, life)
return nil
}
func (this *MemoryManager) Read(key string) (data []byte, err error) {
if this.grid == nil {
return nil, errors.New("grid has not been initialized")
}
item := this.grid.Read([]byte(key))
if item == nil {
return nil, ErrNotFound
}
return item.Bytes(), nil
}
// 删除
func (this *MemoryManager) Delete(key string) error {
this.grid.Delete([]byte(key))
return nil
}
// 删除key前缀
func (this *MemoryManager) DeletePrefixes(prefixes []string) (int, error) {
if len(prefixes) == 0 {
return 0, nil
}
grid := this.grid
if grid == nil {
return 0, nil
}
count := 0
keys := [][]byte{}
for _, cell := range grid.Cells() {
cell.Range(func(item *teamemory.Item) {
key := string(item.Key)
for _, prefix := range prefixes {
if strings.HasPrefix(key, prefix) || strings.HasPrefix("http://"+key, prefix) || strings.HasPrefix("https://"+key, prefix) {
keys = append(keys, item.Key)
count++
break
}
}
})
}
for _, key := range keys {
grid.Delete(key)
}
return count, nil
}
// 统计
func (this *MemoryManager) Stat() (size int64, countKeys int, err error) {
stat := this.grid.Stat()
return stat.TotalBytes, stat.CountItems, nil
}
// 清理
func (this *MemoryManager) Clean() error {
this.grid.Reset()
return nil
}
// 关闭
func (this *MemoryManager) Close() error {
if this.grid == nil {
return nil
}
//logs.Println("[cache]close cache policy instance: memory")
this.grid.Destroy()
return nil
}