summaryrefslogtreecommitdiff
path: root/xmake/core/cache/localcache.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2020-12-21 22:38:24 +0800
committerruki <[email protected]>2020-12-21 22:38:24 +0800
commitbcc7f92bf3779ab59dd7ab8783ba0eb27cc9be89 (patch)
treeb51be65a5b285637403b35885afe8823bd46aa10 /xmake/core/cache/localcache.lua
parent28696782d6d96f03d05198dc956901f39b1b1a23 (diff)
impl localcache and globalcache
Diffstat (limited to 'xmake/core/cache/localcache.lua')
-rw-r--r--xmake/core/cache/localcache.lua166
1 files changed, 159 insertions, 7 deletions
diff --git a/xmake/core/cache/localcache.lua b/xmake/core/cache/localcache.lua
index 647d74aa8..f937bc9d0 100644
--- a/xmake/core/cache/localcache.lua
+++ b/xmake/core/cache/localcache.lua
@@ -19,15 +19,167 @@
--
-- define module: localcache
-local localcache = localcache or {}
+local localcache = localcache or {}
+local _instance = _instance or {}
-- load modules
-local io = require("base/io")
-local os = require("base/os")
-local path = require("base/path")
-local table = require("base/table")
-local utils = require("base/utils")
-local global = require("base/global")
+local table = require("base/table")
+local io = require("base/io")
+local path = require("base/path")
+local config = require("project/config")
+
+-- 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(config.cachedir(), self:name()))
+ if result then
+ self._DATA = result
+ end
+end
+
+-- save cache
+function _instance:save()
+ local ok, errors = io.save(path.join(config.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 localcache.cache(cachename)
+ local caches = localcache._CACHES
+ if not caches then
+ caches = {}
+ localcache._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 localcache.caches()
+ return localcache._CACHES
+end
+
+-- get cache value in level/1
+function localcache.get(cachename, key)
+ return localcache.cache(cachename):get(key)
+end
+
+-- get cache value in level/2
+function localcache.get2(cachename, key1, key2)
+ return localcache.cache(cachename):get2(key1, key2)
+end
+
+-- get cache value in level/3
+function localcache.get3(cachename, key1, key2, key3)
+ return localcache.cache(cachename):get3(key1, key2, key3)
+end
+
+-- set cache value in level/1
+function localcache.set(cachename, key, value)
+ return localcache.cache(cachename):set(key, value)
+end
+
+-- set cache value in level/2
+function localcache.set2(cachename, key1, key2, value)
+ return localcache.cache(cachename):set2(key1, key2, value)
+end
+
+-- set cache value in level/3
+function localcache.set3(cachename, key1, key2, key3, value)
+ return localcache.cache(cachename):set3(key1, key2, key3, value)
+end
+
+-- clear the given cache, it will clear all caches if cache name is nil
+function localcache.clear(cachename)
+ local caches = localcache.caches()
+ if caches then
+ for _, cache in pairs(caches) do
+ if cachename then
+ if cache:name() == cachename then
+ cache:clear()
+ end
+ else
+ cache:clear()
+ end
+ end
+ end
+end
-- return module: localcache
return localcache
+