-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringDbModel.go
More file actions
83 lines (64 loc) · 1.27 KB
/
stringDbModel.go
File metadata and controls
83 lines (64 loc) · 1.27 KB
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 go_orm
import (
"sync"
)
type StringDbModel struct {
cache map[string]PotokStringOrm
cacheMutex *sync.RWMutex
isInit bool
}
func (m *StringDbModel) init() {
if m.isInit != true {
m.cacheMutex = &sync.RWMutex{}
m.cache = make(map[string]PotokStringOrm)
m.isInit = true
}
}
func (m *StringDbModel) GetCache() interface{} {
m.init()
return m.cache
}
func (m *StringDbModel) FindInCache(id string) PotokStringOrm {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
if _, ok := m.cache[id]; ok {
result := m.cache[id].(PotokStringOrm)
if result.IsActive() {
return result
}
}
return nil
}
func (m *StringDbModel) AddToCache(v PotokStringOrm) {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
m.cache[v.GetId()] = v
}
func (m *StringDbModel) ClearCache() {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
m.cache = make(map[string]PotokStringOrm)
}
func (m *StringDbModel) Len() int {
return len(m.cache)
}
func (m *StringDbModel) ClearOutDated() {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
data := m.cache
for key, item := range data {
if item.OutDated() {
delete(data, key)
}
}
m.cache = data
}
type PotokStringOrm interface {
IsActive() bool
GetId() string
OutDated() bool
}