From 7191eae559c53d89bc88a5141f9b67b423c2abd4 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Tue, 15 Sep 2015 18:02:46 +0400 Subject: [PATCH] Fix. `cache.del` method. Fix. cache.set returns boolean value. Add. basic self_test --- .../scripts/resources/functions/cache.lua | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/resources/install/scripts/resources/functions/cache.lua b/resources/install/scripts/resources/functions/cache.lua index 29c1b9d57e..974e327dd9 100644 --- a/resources/install/scripts/resources/functions/cache.lua +++ b/resources/install/scripts/resources/functions/cache.lua @@ -51,11 +51,13 @@ 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)) + local ok, err = check_error(api:execute("memcache", "set " .. key .. " '" .. value .. "' " .. expire)) + if not ok then return nil, err end + return ok == '+OK' end function Cache.del(key) - local result, err = check_error(api:execute("memcache", "set " .. key .. " '" .. value .. "' " .. expire)) + local result, err = check_error(api:execute("memcache", "delete " .. key)) if not result then if err == 'NOT FOUND' then return true @@ -65,4 +67,23 @@ function Cache.del(key) return result == '+OK' end +function Cache._self_test() + assert(Cache.support()) + Cache.del("a") + + local ok, err = Cache.get("a") + assert(nil == ok) + assert(err == "NOT FOUND") + + local s = "hello \\ ' world" + assert(true == Cache.set("a", s)) + assert(s == Cache.get("a")) + + assert(true == Cache.del("a")) +end + +-- if debug.self_test then +-- Cache._self_test() +-- end + return Cache