-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringSlicedDbModel.go
More file actions
99 lines (81 loc) · 1.75 KB
/
stringSlicedDbModel.go
File metadata and controls
99 lines (81 loc) · 1.75 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package go_orm
import (
"sync"
)
const (
add = iota
remove
)
type StringSlicedDbModel struct {
cache map[string][]PotokStringSliceOrm
cacheMutex *sync.RWMutex
isInit bool
lenght int
}
func (m *StringSlicedDbModel) init() {
if m.isInit != true {
m.cacheMutex = &sync.RWMutex{}
m.cache = make(map[string][]PotokStringSliceOrm)
m.isInit = true
m.lenght = 0
}
}
func (m *StringSlicedDbModel) GetCache() interface{} {
m.init()
return m.cache
}
func (m *StringSlicedDbModel) FindInCache(id string) []PotokStringSliceOrm {
m.init()
m.cacheMutex.Lock()
var result []PotokStringSliceOrm
var t []PotokStringSliceOrm
defer m.cacheMutex.Unlock()
if _, ok := m.cache[id]; ok {
result = m.cache[id]
}
for _, val := range result {
if val.IsActive() {
t = append(t, val)
}
}
return t
}
func (m *StringSlicedDbModel) AddToCache(v PotokStringSliceOrm) {
m.cacheAction(v, add)
}
func (m *StringSlicedDbModel) RemoveFromCache(v PotokStringSliceOrm) {
m.cacheAction(v, remove)
}
func (m *StringSlicedDbModel) cacheAction(v PotokStringSliceOrm, action int) {
m.init()
var res []PotokStringSliceOrm
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
current := m.cache[v.GetId()]
m.lenght -= len(current)
for _, val := range current {
if val.GetRelationKey() != v.GetRelationKey() {
res = append(res, val)
}
}
m.lenght += len(res)
if action == add {
m.lenght += 1
res = append(res, v)
}
m.cache[v.GetId()] = res
}
func (m *StringSlicedDbModel) ClearCache() {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
m.cache = make(map[string][]PotokStringSliceOrm)
}
func (m *StringSlicedDbModel) Len() int {
return m.lenght
}
type PotokStringSliceOrm interface {
GetRelationKey() int
GetId() string
IsActive() bool
}