Commit 9ad697c838101e9031586234bce452a690bce8d9

Authored by zhouahaihai
1 parent 197f12ee

删除 fields ,增加数据库数据类型 table 由 msgpack 支持, 增加 数据缓存开关 实现频繁使用的数据字段 统一写入

@@ -149,7 +149,7 @@ skynet.register_protocol { @@ -149,7 +149,7 @@ skynet.register_protocol {
149 end, 149 end,
150 dispatch = function(session, address, cmd, data) 150 dispatch = function(session, address, cmd, data)
151 skynet.ignoreret() 151 skynet.ignoreret()
152 - skynet.trace() --执行序的跟踪统计 152 + -- skynet.trace() --执行序的跟踪统计
153 cs(function() 153 cs(function()
154 if cmd == actionCodes.Sys_heartBeat then 154 if cmd == actionCodes.Sys_heartBeat then
155 agent_util:heart_beat(agentInfo) 155 agent_util:heart_beat(agentInfo)
@@ -190,7 +190,7 @@ skynet.register_protocol { @@ -190,7 +190,7 @@ skynet.register_protocol {
190 pack = skynet.pack, 190 pack = skynet.pack,
191 unpack = skynet.unpack, 191 unpack = skynet.unpack,
192 dispatch = function(session, address, submethod, ...) 192 dispatch = function(session, address, submethod, ...)
193 - skynet.trace() --执行序的跟踪统计 193 + -- skynet.trace() --执行序的跟踪统计
194 local result 194 local result
195 if not agentInfo.role then 195 if not agentInfo.role then
196 result = "__OFFLINE__" 196 result = "__OFFLINE__"
@@ -256,7 +256,7 @@ end @@ -256,7 +256,7 @@ end
256 256
257 skynet.start(function() 257 skynet.start(function()
258 skynet.dispatch("lua", function(session, source, command, ...) 258 skynet.dispatch("lua", function(session, source, command, ...)
259 - skynet.trace() --执行序的跟踪统计 259 + -- skynet.trace() --执行序的跟踪统计
260 local f = CMD[command] 260 local f = CMD[command]
261 if f then 261 if f then
262 if command == "exit" then 262 if command == "exit" then
src/models/Daily.lua
@@ -7,14 +7,9 @@ function Daily:ctor(properties) @@ -7,14 +7,9 @@ function Daily:ctor(properties)
7 end 7 end
8 8
9 Daily.schema = { 9 Daily.schema = {
10 - key = {"string"}, -- redis key  
11 commentHero = {"string", ""}, --单日评论食灵记录 type=1 10 commentHero = {"string", ""}, --单日评论食灵记录 type=1
12 } 11 }
13 12
14 -Daily.fields = {  
15 - commentHero = true,  
16 -}  
17 -  
18 function Daily:updateProperty(params) 13 function Daily:updateProperty(params)
19 local type, default = table.unpack(self.schema[params.field]) 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,7 +4,6 @@ local HeroPlugin = import(".HeroPlugin")
4 HeroPlugin.bind(Hero) 4 HeroPlugin.bind(Hero)
5 5
6 Hero.schema = { 6 Hero.schema = {
7 - key = { "string" },  
8 id = {"number"}, 7 id = {"number"},
9 type = {"number", 0}, 8 type = {"number", 0},
10 level = {"number", 1}, -- 等级 9 level = {"number", 1}, -- 等级
@@ -18,20 +17,6 @@ Hero.schema = { @@ -18,20 +17,6 @@ Hero.schema = {
18 skin = {"number", 0}, --皮肤 0 、 1、 2、 3 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 function Hero:ctor( properties ) 20 function Hero:ctor( properties )
36 Hero.super.ctor(self, properties) 21 Hero.super.ctor(self, properties)
37 end 22 end
src/models/Role.lua
@@ -16,7 +16,6 @@ function Role:ctor( properties ) @@ -16,7 +16,6 @@ function Role:ctor( properties )
16 end 16 end
17 17
18 Role.schema = { 18 Role.schema = {
19 - key = {"string"},  
20 id = {"number"}, 19 id = {"number"},
21 uid = {"string", ""}, 20 uid = {"string", ""},
22 name = {"string", ""}, 21 name = {"string", ""},
@@ -35,24 +34,10 @@ Role.schema = { @@ -35,24 +34,10 @@ Role.schema = {
35 reDiamond = {"number", 0}, 34 reDiamond = {"number", 0},
36 items = {"string", ""}, 35 items = {"string", ""},
37 loveStatus = {"string", ""}, --统计角色的最高 好感度等级 类型相关 -- type=loveL type=loveL 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 local ModelBase = class("ModelBase") 1 local ModelBase = class("ModelBase")
2 ModelBase.key = "key" 2 ModelBase.key = "key"
3 -ModelBase.schema = {  
4 - key = {"string"}  
5 -}  
6 -ModelBase.fields = {} -- 数据库字段 field, update 是否立即更新 3 +ModelBase.schema = {}
7 4
8 local string_format = string.format 5 local string_format = string.format
9 local table_insert = table.insert 6 local table_insert = table.insert
@@ -24,12 +21,61 @@ end @@ -24,12 +21,61 @@ end
24 21
25 function ModelBase:ctor(properties) 22 function ModelBase:ctor(properties)
26 self.isModelBase_ = true 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 if type(properties) ~= "table" then properties = {} end 34 if type(properties) ~= "table" then properties = {} end
30 self:setProperties(properties, true) --缺少的域将设置默认值 35 self:setProperties(properties, true) --缺少的域将设置默认值
31 end 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 返回对象的 ID 值。 81 返回对象的 ID 值。
@@ -71,26 +117,24 @@ function ModelBase:create() @@ -71,26 +117,24 @@ function ModelBase:create()
71 return nil 117 return nil
72 end 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 self:save() 120 self:save()
79 self:onCreate() 121 self:onCreate()
80 122
81 return self 123 return self
82 end 124 end
83 - 125 +-- save 忽略 缓存配置
84 function ModelBase:save() 126 function ModelBase:save()
85 local redisProperties = self:getProperties() 127 local redisProperties = self:getProperties()
86 128
87 local params = {} 129 local params = {}
88 for fieldName, value in pairs(redisProperties) do 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 table_insert(params, self[propname]) 136 table_insert(params, self[propname])
93 - -- end 137 + end
94 end 138 end
95 if next(params) then 139 if next(params) then
96 redisproxy:hmset(self:getKey(), table_unpack(params)) 140 redisproxy:hmset(self:getKey(), table_unpack(params))
@@ -127,6 +171,10 @@ function ModelBase:setProperties(properties, notWrite) @@ -127,6 +171,10 @@ function ModelBase:setProperties(properties, notWrite)
127 local typ, def = table_unpack(schema) 171 local typ, def = table_unpack(schema)
128 local propname = field .. "_" 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 local val = properties[field] or def 178 local val = properties[field] or def
131 if val ~= nil then 179 if val ~= nil then
132 if typ == "number" then val = tonumber(val) end 180 if typ == "number" then val = tonumber(val) end
@@ -134,8 +182,13 @@ function ModelBase:setProperties(properties, notWrite) @@ -134,8 +182,13 @@ function ModelBase:setProperties(properties, notWrite)
134 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", 182 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s",
135 tostring(self), self.class.__cname, field, typ, type(val))) 183 tostring(self), self.class.__cname, field, typ, type(val)))
136 self[propname] = val 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 end 192 end
140 end 193 end
141 if not notWrite and next(params) then 194 if not notWrite and next(params) then
@@ -159,7 +212,7 @@ end @@ -159,7 +212,7 @@ end
159 ]] 212 ]]
160 function ModelBase:getProperties(fields, filter) 213 function ModelBase:getProperties(fields, filter)
161 local schema = self.class.schema 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 local properties = {} 217 local properties = {}
165 for i, field in ipairs(fields) do 218 for i, field in ipairs(fields) do
@@ -185,7 +238,7 @@ function ModelBase:getProperty(property) @@ -185,7 +238,7 @@ function ModelBase:getProperty(property)
185 return self:getProperties({property})[property] 238 return self:getProperties({property})[property]
186 end 239 end
187 240
188 -function ModelBase:setProperty(property, value, update) 241 +function ModelBase:setProperty(property, value)
189 if not self.class.schema[property] then 242 if not self.class.schema[property] then
190 print(string_format("%s [%s:setProperty()] Invalid property : %s", 243 print(string_format("%s [%s:setProperty()] Invalid property : %s",
191 tostring(self), self.class.__cname, property)) 244 tostring(self), self.class.__cname, property))
@@ -196,19 +249,24 @@ function ModelBase:setProperty(property, value, update) @@ -196,19 +249,24 @@ function ModelBase:setProperty(property, value, update)
196 local propname = property .. "_" 249 local propname = property .. "_"
197 250
198 if typ == "number" then value = tonumber(value) end 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 assert(type(value) == typ, 255 assert(type(value) == typ,
200 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", 256 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s",
201 tostring(self), self.class.__cname, property, typ, type(value))) 257 tostring(self), self.class.__cname, property, typ, type(value)))
202 self[propname] = value 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 redisproxy:hset(self:getKey(), property, value) 265 redisproxy:hset(self:getKey(), property, value)
206 - else  
207 - -- self.dirtyFields[property] = true -- record the fields been modified  
208 end 266 end
209 end 267 end
210 268
211 -function ModelBase:incrProperty(property, value, update) 269 +function ModelBase:incrProperty(property, value)
212 if not self.class.schema[property] then 270 if not self.class.schema[property] then
213 print(string_format("%s [%s:setProperty()] Invalid property : %s", 271 print(string_format("%s [%s:setProperty()] Invalid property : %s",
214 tostring(self), self.class.__cname, property)) 272 tostring(self), self.class.__cname, property))
@@ -218,16 +276,16 @@ function ModelBase:incrProperty(property, value, update) @@ -218,16 +276,16 @@ function ModelBase:incrProperty(property, value, update)
218 local typ, def = table_unpack(self.class.schema[property]) 276 local typ, def = table_unpack(self.class.schema[property])
219 local propname = property .. "_" 277 local propname = property .. "_"
220 278
  279 + if typ == "table" then return end
221 if typ == "number" then value = tonumber(value) end 280 if typ == "number" then value = tonumber(value) end
  281 +
222 assert(type(value) == typ, 282 assert(type(value) == typ,
223 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", 283 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s",
224 tostring(self), self.class.__cname, property, typ, type(value))) 284 tostring(self), self.class.__cname, property, typ, type(value)))
225 self[propname] = self[propname] + value 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 return redisproxy:hincrby(self:getKey(), property, value) 288 return redisproxy:hincrby(self:getKey(), property, value)
229 - else  
230 - -- self.dirtyFields[property] = true -- record the fields been modified  
231 end 289 end
232 end 290 end
233 291