-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from Edmond-J-A/master
feat: Add Cache at persist
- Loading branch information
Showing
3 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |