mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2026-01-06 19:53:56 +00:00
Add. Basic cache class
Fix. When memcache stopped and mod_memcache loaded dialplan did not build from DB.
This commit is contained in:
@@ -24,15 +24,21 @@
|
||||
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
-- POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
local cache = require"resources.functions.cache"
|
||||
|
||||
--get the cache
|
||||
if (trim(api:execute("module_exists", "mod_memcache")) == "true") then
|
||||
XML_STRING = trim(api:execute("memcache", "get dialplan:" .. call_context));
|
||||
else
|
||||
XML_STRING = "-ERR NOT FOUND";
|
||||
XML_STRING, err = cache.get("dialplan:" .. call_context)
|
||||
|
||||
if debug['cache'] then
|
||||
if XML_STRING then
|
||||
freeswitch.consoleLog("notice", "[xml_handler] dialplan:"..call_context.." source: memcache\n");
|
||||
elseif err ~= 'NOT FOUND' then
|
||||
freeswitch.consoleLog("notice", "[xml_handler] error get element form cache: " .. err .. "\n");
|
||||
end
|
||||
end
|
||||
|
||||
--set the cache
|
||||
if (XML_STRING == "-ERR NOT FOUND") then
|
||||
if not XML_STRING then
|
||||
|
||||
--connect to the database
|
||||
require "resources.functions.database_handle";
|
||||
@@ -287,8 +293,9 @@
|
||||
XML_STRING = table.concat(xml, "\n");
|
||||
|
||||
--set the cache
|
||||
tmp = XML_STRING:gsub("\\", "\\\\");
|
||||
result = trim(api:execute("memcache", "set dialplan:" .. call_context .. " '"..tmp:gsub("'", "'").."' "..expire["dialplan"]));
|
||||
if cache.support() then
|
||||
cache.set("dialplan:" .. call_context, XML_STRING, expire["dialplan"])
|
||||
end
|
||||
|
||||
--send the xml to the console
|
||||
if (debug["xml_string"]) then
|
||||
@@ -304,12 +311,5 @@
|
||||
|
||||
--close the database connection
|
||||
dbh:release();
|
||||
else
|
||||
--replace the ' back to a single quote
|
||||
XML_STRING = XML_STRING:gsub("'", "'");
|
||||
|
||||
--send to the console
|
||||
if (debug["cache"]) then
|
||||
freeswitch.consoleLog("notice", "[xml_handler] dialplan:"..call_context.." source: memcache\n");
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
63
resources/install/scripts/resources/functions/cache.lua
Normal file
63
resources/install/scripts/resources/functions/cache.lua
Normal file
@@ -0,0 +1,63 @@
|
||||
-- @usage cache = require "resources.functions.cache"
|
||||
-- value = cache.get(key)
|
||||
-- if not value then
|
||||
-- ...
|
||||
-- cache.set(key, value, expire)
|
||||
-- end
|
||||
--
|
||||
|
||||
require "resources.functions.trim";
|
||||
|
||||
local api = api or freeswitch.API();
|
||||
|
||||
local Cache = {}
|
||||
|
||||
local function check_error(result)
|
||||
result = trim(result or '')
|
||||
|
||||
if result and result:sub(1, 4) == '-ERR' then
|
||||
return nil, trim(result:sub(5))
|
||||
end
|
||||
|
||||
if result == 'INVALID COMMAND!' and not Cache.support() then
|
||||
return nil, 'INVALID COMMAND'
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function Cache.support()
|
||||
-- assume it is not unloadable
|
||||
if Cache._support then
|
||||
return true
|
||||
end
|
||||
Cache._support = (trim(api:execute('module_exists', 'mod_memcache')) == 'true')
|
||||
return Cache._support
|
||||
end
|
||||
|
||||
--- Get element from cache
|
||||
--
|
||||
-- @tparam key string
|
||||
-- @return[1] string value
|
||||
-- @return[2] nil
|
||||
-- @return[2] error string `e.g. 'NOT FOUND'
|
||||
-- @note error string does not contain `-ERR` prefix
|
||||
function Cache.get(key)
|
||||
local result, err = check_error(api:execute('memcache', 'get ' .. key))
|
||||
if not result then return nil, err end
|
||||
return (result:gsub("'", "'"))
|
||||
end
|
||||
|
||||
function Cache.set(key, value, expire)
|
||||
value = value:gsub("'", "'"):gsub("\\", "\\\\")
|
||||
expire = expire and tostring(expire) or ""
|
||||
return check_error(api:execute("memcache", "set " .. key .. " '" .. value .. "' " .. expire))
|
||||
end
|
||||
|
||||
function Cache.del(key)
|
||||
local result, err = check_error(api:execute("memcache", "set " .. key .. " '" .. value .. "' " .. expire))
|
||||
if not result then return nil, err end
|
||||
return result == '+OK'
|
||||
end
|
||||
|
||||
return Cache
|
||||
Reference in New Issue
Block a user