-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpireTable.lua
More file actions
47 lines (38 loc) · 881 Bytes
/
ExpireTable.lua
File metadata and controls
47 lines (38 loc) · 881 Bytes
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
local class = require("class")
---@class util.ExpireTable
---@operator call: util.ExpireTable
local ExpireTable = class()
ExpireTable.time = 0
ExpireTable.timeout = 1
ExpireTable.length = 0
---@param timer time.ITimer?
function ExpireTable:new(timer)
self.timer = assert(timer or self.timer, "timer is required")
---@type {[any]: {[1]: any, time: number}}
self.data = {}
end
---@param k any
---@return any?
function ExpireTable:get(k)
local t = self
local time = self.timer:getTime()
local data = t.data
if not data[k] then
data[k] = {t:load(k)}
end
data[k].time = time
local timeout = t.timeout
if time > t.time + timeout then
t.time = time
for key, obj in pairs(data) do
if obj.time + timeout < time then
data[key] = nil
end
end
end
return data[k][1]
end
---@param k any
---@return any?
function ExpireTable:load(k) end
return ExpireTable