From 89a46b4a0d507181c697f8da43d74dbf7271b32f Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Thu, 12 Feb 2015 13:47:48 +0200 Subject: persistence: introduce generic cache --- aconf/persistence/init.lua | 66 +++++++++++++++++++++++++++------------------- 1 file 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 = {} -- cgit v1.2.3