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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file globalcache.lua
--
-- define module: globalcache
local globalcache = globalcache or {}
local _instance = _instance or {}
-- load modules
local table = require("base/table")
local io = require("base/io")
local os = require("base/os")
local path = require("base/path")
local global = require("base/global")
-- new an instance
function _instance.new(name)
local instance = table.inherit(_instance)
instance._NAME = name
instance:load()
instance._DATA = instance._DATA or {}
return instance
end
-- get cache name
function _instance:name()
return self._NAME
end
-- get cache data
function _instance:data()
return self._DATA
end
-- load cache
function _instance:load()
local result = io.load(path.join(global.cachedir(), self:name()))
if result ~= nil then
self._DATA = result
end
end
-- save cache
function _instance:save()
local ok, errors = io.save(path.join(global.cachedir(), self:name()), self._DATA)
if not ok then
os.raise(errors)
end
end
-- get cache value in level/1
function _instance:get(key)
return self._DATA[key]
end
-- get cache value in level/2
function _instance:get2(key1, key2)
local value1 = self:get(key1)
if value1 ~= nil then
return value1[key2]
end
end
-- get cache value in level/3
function _instance:get3(key1, key2, key3)
local value2 = self:get2(key1)
if value2 ~= nil then
return value2[key3]
end
end
-- set cache value in level/1
function _instance:set(key, value)
self._DATA[key] = value
end
-- set cache value in level/2
function _instance:set2(key1, key2, value2)
local value1 = self:get(key1)
if value1 == nil then
value1 = {}
self:set(key1, value1)
end
value1[key2] = value2
end
-- set cache value in level/3
function _instance:set3(key1, key2, key3, value3)
local value2 = self:get2(key1, key2)
if value2 == nil then
value2 = {}
self:set2(key1, key2, value2)
end
value2[key3] = value3
end
-- clear cache scopes
function _instance:clear()
self._DATA = {}
end
-- get cache instance
function globalcache.cache(cachename)
local caches = globalcache._CACHES
if not caches then
caches = {}
globalcache._CACHES = caches
end
local instance = caches[cachename]
if not instance then
instance = _instance.new(cachename)
caches[cachename] = instance
end
return instance
end
-- get all caches
function globalcache.caches(opt)
opt = opt or {}
if opt.flush then
for _, cachefile in ipairs(os.files(path.join(global.cachedir(), "*"))) do
local cachename = path.filename(cachefile)
globalcache.cache(cachename)
end
end
return globalcache._CACHES
end
-- get cache value in level/1
function globalcache.get(cachename, key)
return globalcache.cache(cachename):get(key)
end
-- get cache value in level/2
function globalcache.get2(cachename, key1, key2)
return globalcache.cache(cachename):get2(key1, key2)
end
-- get cache value in level/3
function globalcache.get3(cachename, key1, key2, key3)
return globalcache.cache(cachename):get3(key1, key2, key3)
end
-- set cache value in level/1
function globalcache.set(cachename, key, value)
return globalcache.cache(cachename):set(key, value)
end
-- set cache value in level/2
function globalcache.set2(cachename, key1, key2, value)
return globalcache.cache(cachename):set2(key1, key2, value)
end
-- set cache value in level/3
function globalcache.set3(cachename, key1, key2, key3, value)
return globalcache.cache(cachename):set3(key1, key2, key3, value)
end
-- save the given cache, it will save all caches if cache name is nil
function globalcache.save(cachename)
if cachename then
globalcache.cache(cachename):save()
else
local caches = globalcache.caches()
if caches then
for _, cache in pairs(caches) do
cache:save()
end
end
end
end
-- clear the given cache, it will clear all caches if cache name is nil
function globalcache.clear(cachename)
if cachename then
globalcache.cache(cachename):clear()
else
local caches = globalcache.caches({flush = true})
if caches then
for _, cache in pairs(caches) do
cache:clear()
end
end
end
end
-- return module: globalcache
return globalcache
|