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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
-- Copyright 2025 David Vazgenovich Shakaryan
local util = require('util')
local mp_utils = require('mp.utils')
local cacher = {}
local function exec(obj, opts, call_opts, func, func_name, ...)
local call_opts = call_opts or {}
local fn = table.concat({opts.prefix, func_name, ...}, '.')
local path = mp_utils.join_path(opts.directory, fn)
local f = mp_utils.file_info(path)
local data
local miss = not f or os.time() - f.mtime > opts.time
if miss then
if call_opts.before_miss then
call_opts.before_miss()
end
data = func(obj, ...)
if data then
util.write_json_file(path, data)
end
else
if call_opts.before_hit then
call_opts.before_hit()
end
end
if f and not data then
print('using cached ' .. fn)
data = util.read_json_file(path)
end
if miss then
if call_opts.after_miss then
call_opts.after_miss()
end
else
if call_opts.after_hit then
call_opts.after_hit()
end
end
return data
end
function cacher.wrap(obj, opts)
local proxy = {}
function proxy:with_opts(f, ...)
local n = select('#', ...)
local args = {...}
return exec(
obj, opts, args[n], obj[f], f, unpack(args, 1, n - 1))
end
setmetatable(proxy, {
__index = function(t, k)
local v = obj[k]
if type(v) ~= 'function' or not opts.functions[k] then
return v
end
local fn = function(_, ...)
return exec(obj, opts, nil, v, k, ...)
end
t[k] = fn
return fn
end,
})
return proxy
end
return cacher
|