/
saveable.go
159 lines (137 loc) · 3.39 KB
/
saveable.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gentity
// 保存数据的接口
// 用于检查数据是否修改过
type Saveable interface {
// 数据是否改变过
IsChanged() bool
// 重置
ResetChanged()
}
// 保存数据作为一个整体,只要一个字段修改了,整个数据都需要缓存
//
// examples:
//
// type ProteTest struct {
// BaseDirtyMark
// Data *pb.Data `db:"ProteTest"`
// }
//
// type MapTest struct {
// BaseDirtyMark
// Data map[int32]*pb.Data `db:"MapTest"`
// }
//
// type MapTest struct {
// BaseDirtyMark
// Data map[int32]string `db:"MapTest"`
// }
//
// type SliceTest struct {
// BaseDirtyMark
// Data []*pb.Data `db:"SliceTest"`
// }
type DirtyMark interface {
// 需要保存的数据是否修改了
IsDirty() bool
// 设置数据修改标记
SetDirty()
// 重置标记
ResetDirty()
}
// 同时需要保存数据库和缓存的接口
// Saveable:
//
// 保存数据库的频率低,比如玩家下线时才会保存数据库,那么Saveable只会在上线期间记录有没有改变过就可以
//
// DirtyMark:
//
// 缓存的保存频率高,比如玩家每一次操作都可能引起缓存的更新
type SaveableDirtyMark interface {
Saveable
DirtyMark
}
type BaseDirtyMark struct {
// 数据是否修改过,用于保存数据库的数据修改标记
isChanged bool
// 脏数据标记,用于缓存的数据修改标记
isDirty bool
}
// 数据是否改变过
func (this *BaseDirtyMark) IsChanged() bool {
return this.isChanged
}
func (this *BaseDirtyMark) ResetChanged() {
this.isChanged = false
}
func (this *BaseDirtyMark) IsDirty() bool {
return this.isDirty
}
func (this *BaseDirtyMark) SetDirty() {
this.isDirty = true
this.isChanged = true
}
func (this *BaseDirtyMark) ResetDirty() {
this.isDirty = false
}
// map格式的保存数据
// 第一次有数据修改时,会把整体数据缓存一次,之后只保存修改过的项(增量更新)
//
// examples:
//
// type MapTest struct {
// BaseMapDirtyMark
// Data map[int32]*pb.Data `db:"MapTest"`
// }
//
// type MapTest struct {
// BaseMapDirtyMark
// Data map[int32]string `db:"MapTest"`
// }
type MapDirtyMark interface {
// 需要保存的数据是否修改了
IsDirty() bool
// 设置数据修改标记
SetDirty(k interface{}, isAddOrUpdate bool)
// 重置标记
ResetDirty()
// 是否把整体数据缓存过了
HasCached() bool
// 第一次有数据修改时,会把整体数据缓存一次,之后只保存修改过的项(增量更新)
SetCached()
RangeDirtyMap(f func(dirtyKey interface{}, isAddOrUpdate bool))
}
type BaseMapDirtyMark struct {
isChanged bool
hasCached bool
dirtyMap map[interface{}]bool
}
func (this *BaseMapDirtyMark) IsChanged() bool {
return this.isChanged
}
func (this *BaseMapDirtyMark) ResetChanged() {
this.isChanged = false
}
func (this *BaseMapDirtyMark) IsDirty() bool {
return len(this.dirtyMap) > 0
}
func (this *BaseMapDirtyMark) SetDirty(k interface{}, isAddOrUpdate bool) {
if this.dirtyMap == nil {
this.dirtyMap = make(map[interface{}]bool)
}
this.dirtyMap[k] = isAddOrUpdate
this.isChanged = true
}
func (this *BaseMapDirtyMark) ResetDirty() {
this.dirtyMap = make(map[interface{}]bool)
}
func (this *BaseMapDirtyMark) HasCached() bool {
return this.hasCached
}
func (this *BaseMapDirtyMark) SetCached() {
this.hasCached = true
}
func (this *BaseMapDirtyMark) RangeDirtyMap(f func(dirtyKey interface{}, isAddOrUpdate bool)) {
for k, v := range this.dirtyMap {
f(k, v)
}
}