summaryrefslogtreecommitdiff
path: root/server/src/vm/chain.lua
blob: 6e7c6ac7e9aa76bfd3b18c77298b097f2d65b549 (plain)
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
local valueMgr = require 'vm.value'
local sourceMgr = require 'vm.source'

local mt = {}
mt.__index = mt
mt.type = 'chain'

mt.min = 100
mt.max = 100
mt.count = 0

function mt:clearCache()
    if self.count <= self.max then
        return
    end
    local clock = os.clock()
    local n = 0
    for uri, value in pairs(self.cache) do
        local ok = value:eachInfo(function ()
            return true
        end)
        if ok then
            n = n + 1
        else
            value:getSource():kill()
            self.cache[uri] = nil
        end
    end
    self.count = n
    self.max = self.count * 1.1 + 10
    if self.max < self.min then
        self.max = self.min
    end
    local passed = os.clock() - clock
    if passed > 0.1 then
        log.warn(('chain:clearCache takes: [%.3f]sec, self.count: %d'):format(passed, self.count))
    end
end

function mt:get(uri)
    if not self.cache[uri] then
        self.count = self.count + 1
        self:clearCache()
        self.cache[uri] = valueMgr.create('any', sourceMgr.dummy())
        self.cache[uri]:markGlobal()
        self.cache[uri].uri = uri
    end
    return self.cache[uri]
end

function mt:remove()
    if self.removed then
        return
    end
    self.removed = true
    for _, value in pairs(self.cache) do
        value:getSource():kill()
    end
end

return function ()
    return setmetatable({
        cache = {},
    }, mt)
end