forked from Angey40/BaiduPCS-Go
/
idcachepool.go
110 lines (92 loc) · 2 KB
/
idcachepool.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
// Package cachepool []byte缓存池
package cachepool
import (
"sync"
"sync/atomic"
)
var (
// IDCachePool []byte 缓存池
IDCachePool = cachePool{
cachepool: sync.Map{},
}
)
type cachePool struct {
lastID int32
cachepool sync.Map
}
func (cp *cachePool) Apply(size int) (id int32) {
for {
_, ok := cp.cachepool.Load(cp.lastID)
atomic.AddInt32(&cp.lastID, 1)
if ok {
continue
}
break
}
cp.Set(cp.lastID, size)
return cp.lastID
}
func (cp *cachePool) Existed(id int32) (existed bool) {
_, existed = cp.cachepool.Load(id)
return
}
func (cp *cachePool) Get(id int32) []byte {
cache, ok := cp.cachepool.Load(id)
if !ok {
return nil
}
return cache.([]byte)
}
func (cp *cachePool) Set(id int32, size int) []byte {
cache := RawMallocByteSlice(size)
cp.cachepool.Store(id, cache)
return cp.Get(id)
}
func (cp *cachePool) SetIfNotExist(id int32, size int) []byte {
ok := cp.Existed(id)
cache := cp.Get(id)
if !ok || len(cache) < size {
cache = nil
cp.Delete(id)
cp.Set(id, size)
}
return cp.Get(id)
}
func (cp *cachePool) Delete(id int32) {
cp.cachepool.Store(id, nil)
cp.cachepool.Delete(id)
}
func (cp *cachePool) DeleteAll() {
cp.cachepool.Range(func(k interface{}, _ interface{}) bool {
cp.Delete(k.(int32))
return true
})
}
// Apply 申请缓存, 返回缓存id
func Apply(size int) (id int32) {
return IDCachePool.Apply(size)
}
// Existed 通过缓存id检测是否存在缓存
func Existed(id int32) bool {
return IDCachePool.Existed(id)
}
// Get 通过缓存id获取缓存[]byte
func Get(id int32) []byte {
return IDCachePool.Get(id)
}
// Set 设置缓存, 通过给定的缓存id
func Set(id int32, size int) []byte {
return IDCachePool.Set(id, size)
}
// SetIfNotExist 如果缓存不存在, 则设置缓存池
func SetIfNotExist(id int32, size int) []byte {
return IDCachePool.SetIfNotExist(id, size)
}
// Delete 通过缓存id删除缓存
func Delete(id int32) {
IDCachePool.Delete(id)
}
// DeleteAll 清空缓存池
func DeleteAll() {
IDCachePool.DeleteAll()
}