-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcache.lua
More file actions
136 lines (136 loc) · 3.13 KB
/
cache.lua
File metadata and controls
136 lines (136 loc) · 3.13 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
local concat
concat = table.concat
local json = require("cjson")
local serialize
serialize = function(obj)
return json.encode(obj)
end
local unserialize
unserialize = function(text)
return json.decode(text)
end
local CacheTable
do
local _class_0
local _base_0 = {
__tostring = function(self)
return "<CacheTable>"
end,
get = function(self, name, default)
if default == nil then
default = (function()
return CacheTable()
end)
end
local val = self[name]
if type(val) == "table" and getmetatable(val) ~= self.__class.__base then
self.__class:inject(val)
end
if val == nil then
val = default()
self[name] = val
return val
else
return val
end
end,
set = function(self, name, value)
self[name] = value
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function() end,
__base = _base_0,
__name = "CacheTable"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
local self = _class_0
self.inject = function(self, tbl)
return setmetatable(tbl, self.__base)
end
CacheTable = _class_0
end
local Cache
do
local _class_0
local _base_0 = {
load_cache = function(self)
if self.loaded then
return
end
self.loaded = true
if self.disabled then
self.cache = CacheTable()
return
end
if self.site.io.exists(self.fname) then
local content = self.site.io.read_file(self.fname)
local cache, err = unserialize(content)
if not (cache) then
error("could not load cache `" .. tostring(self.fname) .. "`, delete and try again: " .. tostring(err))
end
self.cache = cache
else
self.cache = { }
end
return CacheTable:inject(self.cache)
end,
write = function(self)
local _list_0 = self.finalize
for _index_0 = 1, #_list_0 do
local fn = _list_0[_index_0]
fn(self)
end
if self.disabled then
return
end
local text = serialize(self.cache)
if not text then
error("failed to serialize cache")
end
return self.site.io.write_file(self.fname, text)
end,
set = function(self, ...)
self:load_cache()
return self.cache:set(...)
end,
get = function(self, ...)
self:load_cache()
return self.cache:get(...)
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, site, fname)
if fname == nil then
fname = ".sitegen_cache"
end
self.site, self.fname = site, fname
self.finalize = { }
self.disabled = false
end,
__base = _base_0,
__name = "Cache"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Cache = _class_0
end
return {
Cache = Cache,
CacheTable = CacheTable
}