Skip to content

Commit

Permalink
feat: Add Cache at persist
Browse files Browse the repository at this point in the history
Signed-off-by: Edmond <edomondja@gmail.com>
  • Loading branch information
Edmond-J-A committed Aug 11, 2021
1 parent f751ead commit 8fecf62
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 9 deletions.
13 changes: 5 additions & 8 deletions src/main/CachedEnforcer.lua
Expand Up @@ -13,6 +13,7 @@
--limitations under the License.

local Enforcer = require("src.main.Enforcer")
local DefaultCache=require("src.persist.cache.DefaultCache")

-- CachedEnforcer wraps Enforcer and provides decision cache
local CachedEnforcer = {}
Expand All @@ -24,7 +25,7 @@ function CachedEnforcer:new(model, adapter)
self.__index = self
setmetatable(e, self)
e.cacheEnabled = true
e.m = {}
e.m = DefaultCache:new()
return e
end

Expand Down Expand Up @@ -66,19 +67,15 @@ function CachedEnforcer:enforce(...)
end

function CachedEnforcer:getCachedResult(key)
if self.m[key] ~= nil then
return self.m[key], true
end

return nil, false
return self.m:get(key)
end

function CachedEnforcer:setCachedResult(key, res)
self.m[key] = res
self.m:set(key, res)
end

function CachedEnforcer:invalidateCache()
self.m = {}
self.m:clear()
end

return CachedEnforcer
48 changes: 48 additions & 0 deletions src/persist/cache/Cache.lua
@@ -0,0 +1,48 @@
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
--You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
--Unless required by applicable law or agreed to in writing, software
--distributed under the License is distributed on an "AS IS" BASIS,
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--See the License for the specific language governing permissions and
--limitations under the License.
local Cache={}

function Cache:new()
local o = {}
setmetatable(o, self)
self.__index = self
return o
end
--Set puts key and value into cache.
-- First parameter for extra should be uint denoting expected survival time.
-- If survival time equals 0 or less, the key will always be survival.
function Cache:set(key, value, ...)

end

--Get returns result for key,
--If there's no such key existing in cache,
--ErrNoSuchKey will be returned.
function Cache:get(key)

end

--Delete will remove the specific key in cache.
--If there's no such key existing in cache,
--ErrNoSuchKey will be returned.
function Cache:delete(key)

end

--Clear deletes all the items stored in cache.
function Cache:clear()

end

return Cache
54 changes: 54 additions & 0 deletions src/persist/cache/DefaultCache.lua
@@ -0,0 +1,54 @@
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
--You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
--Unless required by applicable law or agreed to in writing, software
--distributed under the License is distributed on an "AS IS" BASIS,
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--See the License for the specific language governing permissions and
--limitations under the License.
local Cache=require("src/persist/cache/Cache")


local DefaultCache=Cache:new()

function DefaultCache:new()
local o = {}
self.__index = self
setmetatable(o, self)
o.m={}
return o
end

function DefaultCache:set(key, value, ...)
self.m[key] = value
return nil
end

function DefaultCache:get(key)
if self.m[key]==nil then
return nil, false
else
return self.m[key], true
end
end

function DefaultCache:delete(key)
if self.m[key]==nil then
return error("there's no such key existing in cache")
else
self.m[key]=nil
return true
end
end

function DefaultCache:clear()
self.m = { }
return true
end

return DefaultCache
2 changes: 1 addition & 1 deletion tests/main/cached_enforcer_spec.lua
Expand Up @@ -32,7 +32,7 @@ describe("Cached Enforcer tests", function ()
-- for ("alice", "data1", "read") will still be true, as it uses the cached result.
e:RemovePolicy("alice", "data1", "read")

assert.is.True(e:enforce("alice", "data1", "read"))

assert.is.False(e:enforce("alice", "data1", "write"))
assert.is.False(e:enforce("alice", "data2", "read"))
assert.is.False(e:enforce("alice", "data2", "write"))
Expand Down

0 comments on commit 8fecf62

Please sign in to comment.