/
fscache_mem.go
139 lines (120 loc) · 2.94 KB
/
fscache_mem.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
/*
Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package models
import (
"errors"
"sync"
)
type ConcurrentFSCacheMap struct {
sync.RWMutex
// key1:fsID key2:cacheID
value map[string]map[string]*FSCache
}
func newFSCacheMap() *ConcurrentFSCacheMap {
cm := new(ConcurrentFSCacheMap)
cm.value = map[string]map[string]*FSCache{}
return cm
}
func (cm *ConcurrentFSCacheMap) Get(key1, key2 string) *FSCache {
cm.RLock()
var retValue *FSCache
if v1, ok := cm.value[key1]; ok {
retValue = v1[key2]
}
cm.RUnlock()
return retValue
}
func (cm *ConcurrentFSCacheMap) GetBatch(key string) []FSCache {
cm.RLock()
var tmp []FSCache
if v, ok := cm.value[key]; ok {
for _, v1 := range v {
tmp = append(tmp, *v1)
}
}
cm.RUnlock()
return tmp
}
func (cm *ConcurrentFSCacheMap) Put(key string, value *FSCache) {
cm.Lock()
tempV := map[string]*FSCache{}
if v, ok := cm.value[key]; ok {
tempV = v
}
tempV[value.CacheID] = value
cm.value[key] = tempV
cm.Unlock()
}
func (cm *ConcurrentFSCacheMap) Delete(key1, key2 string) error {
cm.Lock()
var err error
if cm.value != nil {
if key1 != "" {
if key2 != "" {
fsMap := cm.value[key1]
delete(fsMap, key2)
cm.value[key1] = fsMap
} else {
delete(cm.value, key1)
}
}
} else {
err = errors.New("FSCache map is null")
}
cm.Unlock()
return err
}
func (cm *ConcurrentFSCacheMap) Update(key, value *FSCache) (has bool, err error) {
cm.Lock()
defer cm.Unlock()
if v1, ok := cm.value[value.FsID]; ok {
_, ok = v1[value.CacheID]
if ok {
has = true
v1[value.CacheID] = value
}
}
return has, nil
}
func newMemFSCache() FSCacheStore {
m := new(MemFSCache)
m.fsCacheMap = newFSCacheMap()
return m
}
type MemFSCache struct {
fsCacheMap *ConcurrentFSCacheMap
}
func (mem *MemFSCache) Add(value *FSCache) error {
mem.fsCacheMap.Put(value.FsID, value)
return nil
}
func (mem *MemFSCache) Get(fsID string, cacheID string) (*FSCache, error) {
return mem.fsCacheMap.Get(fsID, cacheID), nil
}
func (mem *MemFSCache) Delete(fsID, cacheID string) error {
return mem.fsCacheMap.Delete(fsID, cacheID)
}
func (mem *MemFSCache) List(fsID, cacheID string) ([]FSCache, error) {
var retMap []FSCache
if fsID != "" {
if cacheID != "" {
retMap = append(retMap, *mem.fsCacheMap.Get(fsID, cacheID))
} else {
retMap = mem.fsCacheMap.GetBatch(fsID)
}
}
return retMap, nil
}
func (mem *MemFSCache) Update(value *FSCache) (int64, error) {
return 0, nil
}