Skip to content

Commit

Permalink
orm增加TTI缓存淘汰策略
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Mar 18, 2024
1 parent a4ba9b8 commit b889bf9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lualib/skynet-fly/db/orm/ormtable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local type = type
local ipairs = ipairs
local sformat = string.format
local next = next
local tostring = tostring

local FILED_TYPE = {
int8 = 1,
Expand Down Expand Up @@ -434,15 +435,18 @@ end

local week_mata = {__mode = "kv"}
-- 设置缓存时间
function M:set_cache(expire, inval)
function M:set_cache(expire, inval, cache_limit)
if cache_limit then
assert(cache_limit > 0, "err cache_limit " .. tostring(cache_limit))
end
assert(not self._is_builder, "builded can`t set_cache")
assert(not self._time_obj, "repeat time_obj")
assert(expire >= 0, "err expire " .. expire) --缓存时间
assert(inval > 0, "err inval") --自动保存间隔
self._cache_time = expire

if expire > 0 then --0表示缓存不过期
self._cache_map = tti:new(expire, cache_time_out)
self._cache_map = tti:new(expire, cache_time_out, cache_limit)
end

local week_t = setmetatable({},week_mata) --挂载一个弱引用表
Expand Down
35 changes: 34 additions & 1 deletion test/module/ormmongo_test_m.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,36 @@ local function test_disconnect()
delete_table()
end

local function test_tti()
local adapter = ormadapter_mongo:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(6000,500, 10) --缓存60秒,5秒保存一次,最大缓存10条
:builder(adapter)

local entry_list = {}
for i = 1, 11 do
local entry = orm_obj:create_one_entry({
player_id = tonumber(1000 .. i),
role_id = 101,
sex = 1,
})
table.insert(entry_list, entry)
skynet.sleep(100)
end

local entry = orm_obj:get_one_entry(10001, 101, 1)
assert(entry ~= entry_list[1])
local entry2 = orm_obj:get_one_entry(10002, 101, 1)
assert(entry2 == entry_list[2])
end

function CMD.start()
skynet.fork(function()
delete_table()
Expand Down Expand Up @@ -1117,9 +1147,12 @@ function CMD.start()
test_delete_all()
log.info("test_craete_one>>>>>")
test_craete_one()
log.info("test_craete_one>>>>>")
log.info("test_select_one>>>>>")
test_select_one()
log.info("test_disconnect>>>>>")
test_disconnect()
log.info("test_tti>>>>>")
test_tti()
delete_table()
log.info("test over >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
end)
Expand Down
50 changes: 49 additions & 1 deletion test/module/ormmysql_test_m.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1139,26 +1139,74 @@ local function test_disconnect()
delete_table()
end

local function test_tti()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(6000,500, 10) --缓存60秒,5秒保存一次,最大缓存10条
:builder(adapter)

local entry_list = {}
for i = 1, 11 do
local entry = orm_obj:create_one_entry({
player_id = tonumber(1000 .. i),
role_id = 101,
sex = 1,
})
table.insert(entry_list, entry)
skynet.sleep(100)
end

local entry = orm_obj:get_one_entry(10001, 101, 1)
assert(entry ~= entry_list[1])
local entry2 = orm_obj:get_one_entry(10002, 101, 1)
assert(entry2 == entry_list[2])
end

function CMD.start()
skynet.fork(function()
log.info("test start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
delete_table()
log.info("test start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
log.info("test_create_table>>>>>")
test_create_table(true)
log.info("test_alter_table>>>>>")
test_alter_table()
log.info("test_create_entry>>>>>")
test_create_entry()
log.info("test_select_entry>>>>>")
test_select_entry()
log.info("test_save_entry>>>>>")
test_save_entry()
log.info("test_delete_entry>>>>>")
test_delete_entry()
log.info("test_cache_entry>>>>>")
test_cache_entry()
log.info("test_inval_save>>>>>")
test_inval_save()
log.info("test_sql_over>>>>>")
test_sql_over()
log.info("test_over_clear_time>>>>>")
test_over_clear_time()
log.info("test_permanent>>>>>")
test_permanent()
log.info("test_get_all>>>>>")
test_get_all()
log.info("test_delete_all>>>>>")
test_delete_all()
log.info("test_craete_one>>>>>")
test_craete_one()
log.info("test_select_one>>>>>")
test_select_one()
log.info("test_disconnect>>>>>")
test_disconnect()
log.info("test_tti>>>>>")
test_tti()
delete_table()
log.info("test over >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
end)
Expand Down

0 comments on commit b889bf9

Please sign in to comment.