From 8fecf627774cc36e5f8f1e180d1118c05c7c3f2e Mon Sep 17 00:00:00 2001 From: Edmond Date: Wed, 11 Aug 2021 19:41:00 +0800 Subject: [PATCH] feat: Add Cache at persist Signed-off-by: Edmond --- src/main/CachedEnforcer.lua | 13 +++---- src/persist/cache/Cache.lua | 48 +++++++++++++++++++++++++ src/persist/cache/DefaultCache.lua | 54 +++++++++++++++++++++++++++++ tests/main/cached_enforcer_spec.lua | 2 +- 4 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 src/persist/cache/Cache.lua create mode 100644 src/persist/cache/DefaultCache.lua diff --git a/src/main/CachedEnforcer.lua b/src/main/CachedEnforcer.lua index f5063b6..3aec660 100644 --- a/src/main/CachedEnforcer.lua +++ b/src/main/CachedEnforcer.lua @@ -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 = {} @@ -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 @@ -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 \ No newline at end of file diff --git a/src/persist/cache/Cache.lua b/src/persist/cache/Cache.lua new file mode 100644 index 0000000..a8cf5ff --- /dev/null +++ b/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 \ No newline at end of file diff --git a/src/persist/cache/DefaultCache.lua b/src/persist/cache/DefaultCache.lua new file mode 100644 index 0000000..fab9868 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/tests/main/cached_enforcer_spec.lua b/tests/main/cached_enforcer_spec.lua index d7fbdd7..1066028 100644 --- a/tests/main/cached_enforcer_spec.lua +++ b/tests/main/cached_enforcer_spec.lua @@ -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"))