summaryrefslogtreecommitdiffstats
path: root/aconf/persistence/init.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2015-02-12 13:47:48 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2015-02-12 16:32:41 +0200
commit89a46b4a0d507181c697f8da43d74dbf7271b32f (patch)
tree09336caa936d5637d92058bdda2c3fc21596231c /aconf/persistence/init.lua
parent868b07f1299cce15bf6475456777d05a8b254b65 (diff)
downloadaconf-89a46b4a0d507181c697f8da43d74dbf7271b32f.tar.bz2
aconf-89a46b4a0d507181c697f8da43d74dbf7271b32f.tar.xz
persistence: introduce generic cache
Diffstat (limited to 'aconf/persistence/init.lua')
-rw-r--r--aconf/persistence/init.lua66
1 files changed, 39 insertions, 27 deletions
diff --git a/aconf/persistence/init.lua b/aconf/persistence/init.lua
index 8056c16..632c191 100644
--- a/aconf/persistence/init.lua
+++ b/aconf/persistence/init.lua
@@ -10,6 +10,7 @@ local address = require('aconf.path.address')
local util = require('aconf.util')
local contains = util.contains
+local copy = util.copy
local setdefault = util.setdefault
local stringy = require('stringy')
@@ -26,8 +27,11 @@ function DataStore:init()
loadmods('persistence/backends')
)
self.triggers = {pre={}, post={}}
+ self:flush_cache()
end
+function DataStore:flush_cache() self.cache = {} end
+
function DataStore:trigger(phase, path, func)
local funcs = setdefault(self.triggers[phase], path, {})
if not contains(funcs, func) then table.insert(funcs, func) end
@@ -42,48 +46,56 @@ function DataStore:split_path(path)
end
function DataStore:get(path)
- local backend, comps = self:split_path(path)
- local top = topology(path)
+ if not self.cache[path] then
+
+ local backend, comps = self:split_path(path)
+ local top = topology(path)
- local value, ts = backend:get(comps, top)
+ local value, ts = backend:get(comps, top)
- if top then
- local t = top.type
- if t and value ~= nil then
- local atype = type(value)
+ if top then
+ local t = top.type
+ if t and value ~= nil then
+ local atype = type(value)
- if t == 'table' then assert(atype == 'table')
+ if t == 'table' then assert(atype == 'table')
- else
- assert(atype ~= 'table')
+ else
+ assert(atype ~= 'table')
- if t == 'string' then value = tostring(value)
- elseif t == 'number' then value = tonumber(value)
+ if t == 'string' then value = tostring(value)
+ elseif t == 'number' then value = tonumber(value)
- elseif t == 'boolean' then
- if atype == 'string' then value = value:lower() end
- if value == 1 or contains(
- {'1', 't', 'true', 'y', 'yes'}, value
- ) then
- value = true
- elseif value == 0 or contains(
- {'0', 'f', 'false', 'n', 'no'}, value
- ) then
- value = false
- else value = value and true or false end
+ elseif t == 'boolean' then
+ if atype == 'string' then value = value:lower() end
+ if value == 1 or contains(
+ {'1', 't', 'true', 'y', 'yes'}, value
+ ) then
+ value = true
+ elseif value == 0 or contains(
+ {'0', 'f', 'false', 'n', 'no'}, value
+ ) then
+ value = false
+ else value = value and true or false end
- elseif contains({'binary', 'reference'}, t) then
- assert(atype == 'string')
+ elseif contains({'binary', 'reference'}, t) then
+ assert(atype == 'string')
- else assert(false) end
+ else assert(false) end
+ end
end
end
+
+ self.cache[path] = {copy(value), ts or self.mod_time[path] or 0}
end
- return util.copy(value), ts or self.mod_time[path] or 0
+ local value, ts = table.unpack(self.cache[path])
+ return copy(value), ts
end
function DataStore:_set_multiple(mods)
+ self:flush_cache()
+
local bms = {}
local trigger = {}