Commit 9ad697c838101e9031586234bce452a690bce8d9
1 parent
197f12ee
删除 fields ,增加数据库数据类型 table 由 msgpack 支持, 增加 数据缓存开关 实现频繁使用的数据字段 统一写入
Showing
5 changed files
with
89 additions
and
66 deletions
Show diff stats
src/agent.lua
... | ... | @@ -149,7 +149,7 @@ skynet.register_protocol { |
149 | 149 | end, |
150 | 150 | dispatch = function(session, address, cmd, data) |
151 | 151 | skynet.ignoreret() |
152 | - skynet.trace() --执行序的跟踪统计 | |
152 | + -- skynet.trace() --执行序的跟踪统计 | |
153 | 153 | cs(function() |
154 | 154 | if cmd == actionCodes.Sys_heartBeat then |
155 | 155 | agent_util:heart_beat(agentInfo) |
... | ... | @@ -190,7 +190,7 @@ skynet.register_protocol { |
190 | 190 | pack = skynet.pack, |
191 | 191 | unpack = skynet.unpack, |
192 | 192 | dispatch = function(session, address, submethod, ...) |
193 | - skynet.trace() --执行序的跟踪统计 | |
193 | + -- skynet.trace() --执行序的跟踪统计 | |
194 | 194 | local result |
195 | 195 | if not agentInfo.role then |
196 | 196 | result = "__OFFLINE__" |
... | ... | @@ -256,7 +256,7 @@ end |
256 | 256 | |
257 | 257 | skynet.start(function() |
258 | 258 | skynet.dispatch("lua", function(session, source, command, ...) |
259 | - skynet.trace() --执行序的跟踪统计 | |
259 | + -- skynet.trace() --执行序的跟踪统计 | |
260 | 260 | local f = CMD[command] |
261 | 261 | if f then |
262 | 262 | if command == "exit" then | ... | ... |
src/models/Daily.lua
... | ... | @@ -7,14 +7,9 @@ function Daily:ctor(properties) |
7 | 7 | end |
8 | 8 | |
9 | 9 | Daily.schema = { |
10 | - key = {"string"}, -- redis key | |
11 | 10 | commentHero = {"string", ""}, --单日评论食灵记录 type=1 |
12 | 11 | } |
13 | 12 | |
14 | -Daily.fields = { | |
15 | - commentHero = true, | |
16 | -} | |
17 | - | |
18 | 13 | function Daily:updateProperty(params) |
19 | 14 | local type, default = table.unpack(self.schema[params.field]) |
20 | 15 | ... | ... |
src/models/Hero.lua
... | ... | @@ -4,7 +4,6 @@ local HeroPlugin = import(".HeroPlugin") |
4 | 4 | HeroPlugin.bind(Hero) |
5 | 5 | |
6 | 6 | Hero.schema = { |
7 | - key = { "string" }, | |
8 | 7 | id = {"number"}, |
9 | 8 | type = {"number", 0}, |
10 | 9 | level = {"number", 1}, -- 等级 |
... | ... | @@ -18,20 +17,6 @@ Hero.schema = { |
18 | 17 | skin = {"number", 0}, --皮肤 0 、 1、 2、 3 |
19 | 18 | } |
20 | 19 | |
21 | -Hero.fields = { | |
22 | - id = true, | |
23 | - type = true, | |
24 | - level = true, | |
25 | - breakL = true, | |
26 | - wakeL = true, | |
27 | - skillL = true, | |
28 | - talent = true, | |
29 | - battleV = true, | |
30 | - loveExp = true, | |
31 | - loveL = true, | |
32 | - skin = true, | |
33 | -} | |
34 | - | |
35 | 20 | function Hero:ctor( properties ) |
36 | 21 | Hero.super.ctor(self, properties) |
37 | 22 | end | ... | ... |
src/models/Role.lua
... | ... | @@ -16,7 +16,6 @@ function Role:ctor( properties ) |
16 | 16 | end |
17 | 17 | |
18 | 18 | Role.schema = { |
19 | - key = {"string"}, | |
20 | 19 | id = {"number"}, |
21 | 20 | uid = {"string", ""}, |
22 | 21 | name = {"string", ""}, |
... | ... | @@ -35,24 +34,10 @@ Role.schema = { |
35 | 34 | reDiamond = {"number", 0}, |
36 | 35 | items = {"string", ""}, |
37 | 36 | loveStatus = {"string", ""}, --统计角色的最高 好感度等级 类型相关 -- type=loveL type=loveL |
38 | -} | |
39 | 37 | |
40 | -Role.fields = { | |
41 | - id = true, | |
42 | - uid = true, | |
43 | - sid = true, | |
44 | - device = true, | |
45 | - name = true, | |
46 | - banTime = true, | |
47 | - banType = true, | |
48 | - ltime = true, | |
49 | - ctime = true, | |
50 | - sversion = true, | |
51 | - level = true, | |
52 | - diamond = true, | |
53 | - reDiamond = true, | |
54 | - items = true, | |
55 | - loveStatus = true, | |
38 | + advPass = {"string", ""}, -- 冒险 通关记录 | |
39 | + advInfo = {"table", {}}, -- 冒险 相关信息 | |
40 | + | |
56 | 41 | } |
57 | 42 | |
58 | 43 | ... | ... |
src/shared/ModelBase.lua
1 | 1 | local ModelBase = class("ModelBase") |
2 | 2 | ModelBase.key = "key" |
3 | -ModelBase.schema = { | |
4 | - key = {"string"} | |
5 | -} | |
6 | -ModelBase.fields = {} -- 数据库字段 field, update 是否立即更新 | |
3 | +ModelBase.schema = {} | |
7 | 4 | |
8 | 5 | local string_format = string.format |
9 | 6 | local table_insert = table.insert |
... | ... | @@ -24,12 +21,61 @@ end |
24 | 21 | |
25 | 22 | function ModelBase:ctor(properties) |
26 | 23 | self.isModelBase_ = true |
27 | - -- self.dirtyFields = {} | |
24 | + self.cacheFields = {} --缓存字段 不更新数据库的字段 | |
25 | + | |
26 | + self[self.class.key .. "_"] = properties[self.class.key] --数据库key | |
27 | + properties[self.class.key] = nil | |
28 | + | |
29 | + if not self:isValidKey() then | |
30 | + print(string_format("%s [%s:key] should be give in new(ctor)", tostring(self), self.class.__cname)) | |
31 | + return | |
32 | + end | |
28 | 33 | |
29 | 34 | if type(properties) ~= "table" then properties = {} end |
30 | 35 | self:setProperties(properties, true) --缺少的域将设置默认值 |
31 | 36 | end |
32 | 37 | |
38 | +-- startCache 和 endCache 在恰当的时候配对使用 | |
39 | +function ModelBase:startCache( ... ) | |
40 | + for _, field in ipairs({ ... }) do | |
41 | + if self.class.schema[field] then | |
42 | + self.cacheFields[field] = true | |
43 | + end | |
44 | + end | |
45 | +end | |
46 | + | |
47 | +function ModelBase:endCache( ... ) | |
48 | + args = { ... } | |
49 | + params = {} | |
50 | + | |
51 | + function doOneCache(field) | |
52 | + local propname = field .. "_" | |
53 | + table_insert(params, field) | |
54 | + if self.class.schema[fieldName][1] == "table" then | |
55 | + table_insert(params, MsgPack.pack(self[propname])) | |
56 | + else | |
57 | + table_insert(params, self[propname]) | |
58 | + end | |
59 | + end | |
60 | + | |
61 | + if not next(args) then | |
62 | + for field, _ in pairs(self.cacheFields) do | |
63 | + doOneCache(field) | |
64 | + end | |
65 | + self.cacheFields = {} | |
66 | + else | |
67 | + for _, field in ipairs(args) do | |
68 | + if self.cacheFields[field] then | |
69 | + self.cacheFields[field] = nil | |
70 | + doOneCache(field) | |
71 | + end | |
72 | + end | |
73 | + end | |
74 | + | |
75 | + if next(params) then | |
76 | + redisproxy:hmset(self:getKey(), table_unpack(params)) | |
77 | + end | |
78 | +end | |
33 | 79 | --[[-- |
34 | 80 | |
35 | 81 | 返回对象的 ID 值。 |
... | ... | @@ -71,26 +117,24 @@ function ModelBase:create() |
71 | 117 | return nil |
72 | 118 | end |
73 | 119 | |
74 | - --将所有的域都置为dirty, 存储到redis | |
75 | - -- for fieldName, update in pairs(self.class.fields) do | |
76 | - -- self.dirtyFields[fieldName] = true | |
77 | - -- end | |
78 | 120 | self:save() |
79 | 121 | self:onCreate() |
80 | 122 | |
81 | 123 | return self |
82 | 124 | end |
83 | - | |
125 | +-- save 忽略 缓存配置 | |
84 | 126 | function ModelBase:save() |
85 | 127 | local redisProperties = self:getProperties() |
86 | 128 | |
87 | 129 | local params = {} |
88 | 130 | for fieldName, value in pairs(redisProperties) do |
89 | - -- if self.dirtyFields[fieldName] then | |
90 | - local propname = fieldName .. "_" | |
91 | - table_insert(params, fieldName) | |
131 | + local propname = fieldName .. "_" | |
132 | + table_insert(params, fieldName) | |
133 | + if self.class.schema[fieldName][1] == "table" then | |
134 | + table_insert(params, MsgPack.pack(self[propname])) | |
135 | + else | |
92 | 136 | table_insert(params, self[propname]) |
93 | - -- end | |
137 | + end | |
94 | 138 | end |
95 | 139 | if next(params) then |
96 | 140 | redisproxy:hmset(self:getKey(), table_unpack(params)) |
... | ... | @@ -127,6 +171,10 @@ function ModelBase:setProperties(properties, notWrite) |
127 | 171 | local typ, def = table_unpack(schema) |
128 | 172 | local propname = field .. "_" |
129 | 173 | |
174 | + if typ == "table" and type(properties[field]) == "string" then | |
175 | + properties[field] = MsgPack.unpack(properties[field]) | |
176 | + end | |
177 | + | |
130 | 178 | local val = properties[field] or def |
131 | 179 | if val ~= nil then |
132 | 180 | if typ == "number" then val = tonumber(val) end |
... | ... | @@ -134,8 +182,13 @@ function ModelBase:setProperties(properties, notWrite) |
134 | 182 | string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", |
135 | 183 | tostring(self), self.class.__cname, field, typ, type(val))) |
136 | 184 | self[propname] = val |
137 | - table_insert(params, field) | |
138 | - table_insert(params, val) | |
185 | + if not self.cacheFields[field] then | |
186 | + table_insert(params, field) | |
187 | + if typ == "table" then | |
188 | + val = MsgPack.pack(val) | |
189 | + end | |
190 | + table_insert(params, val) | |
191 | + end | |
139 | 192 | end |
140 | 193 | end |
141 | 194 | if not notWrite and next(params) then |
... | ... | @@ -159,7 +212,7 @@ end |
159 | 212 | ]] |
160 | 213 | function ModelBase:getProperties(fields, filter) |
161 | 214 | local schema = self.class.schema |
162 | - if type(fields) ~= "table" then fields = table.keys(self.class.fields) end | |
215 | + if type(fields) ~= "table" then fields = table.keys(self.class.schema) end | |
163 | 216 | |
164 | 217 | local properties = {} |
165 | 218 | for i, field in ipairs(fields) do |
... | ... | @@ -185,7 +238,7 @@ function ModelBase:getProperty(property) |
185 | 238 | return self:getProperties({property})[property] |
186 | 239 | end |
187 | 240 | |
188 | -function ModelBase:setProperty(property, value, update) | |
241 | +function ModelBase:setProperty(property, value) | |
189 | 242 | if not self.class.schema[property] then |
190 | 243 | print(string_format("%s [%s:setProperty()] Invalid property : %s", |
191 | 244 | tostring(self), self.class.__cname, property)) |
... | ... | @@ -196,19 +249,24 @@ function ModelBase:setProperty(property, value, update) |
196 | 249 | local propname = property .. "_" |
197 | 250 | |
198 | 251 | if typ == "number" then value = tonumber(value) end |
252 | + if typ == "table" and not value then | |
253 | + value = self[propname] -- table 可以用自己的缓冲 | |
254 | + end | |
199 | 255 | assert(type(value) == typ, |
200 | 256 | string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", |
201 | 257 | tostring(self), self.class.__cname, property, typ, type(value))) |
202 | 258 | self[propname] = value |
203 | 259 | |
204 | - if self.class.fields[property] or update then | |
260 | + if not self.cacheFields[property] then | |
261 | + -- table 使用msgpack | |
262 | + if typ == "table" then | |
263 | + value = MsgPack.pack(value) | |
264 | + end | |
205 | 265 | redisproxy:hset(self:getKey(), property, value) |
206 | - else | |
207 | - -- self.dirtyFields[property] = true -- record the fields been modified | |
208 | 266 | end |
209 | 267 | end |
210 | 268 | |
211 | -function ModelBase:incrProperty(property, value, update) | |
269 | +function ModelBase:incrProperty(property, value) | |
212 | 270 | if not self.class.schema[property] then |
213 | 271 | print(string_format("%s [%s:setProperty()] Invalid property : %s", |
214 | 272 | tostring(self), self.class.__cname, property)) |
... | ... | @@ -218,16 +276,16 @@ function ModelBase:incrProperty(property, value, update) |
218 | 276 | local typ, def = table_unpack(self.class.schema[property]) |
219 | 277 | local propname = property .. "_" |
220 | 278 | |
279 | + if typ == "table" then return end | |
221 | 280 | if typ == "number" then value = tonumber(value) end |
281 | + | |
222 | 282 | assert(type(value) == typ, |
223 | 283 | string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", |
224 | 284 | tostring(self), self.class.__cname, property, typ, type(value))) |
225 | 285 | self[propname] = self[propname] + value |
226 | 286 | |
227 | - if self.class.fields[property] or update then | |
287 | + if not self.cacheFields[property] then | |
228 | 288 | return redisproxy:hincrby(self:getKey(), property, value) |
229 | - else | |
230 | - -- self.dirtyFields[property] = true -- record the fields been modified | |
231 | 289 | end |
232 | 290 | end |
233 | 291 | ... | ... |