Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / lib / tools / rr_cache.lua
1 --[[\r
2 \r
3     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
4 \r
5     Licensed under the Apache License, Version 2.0 (the "License");\r
6     you may not use this file except in compliance with the License.\r
7     You may obtain a copy of the License at\r
8 \r
9             http://www.apache.org/licenses/LICENSE-2.0\r
10 \r
11     Unless required by applicable law or agreed to in writing, software\r
12     distributed under the License is distributed on an "AS IS" BASIS,\r
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14     See the License for the specific language governing permissions and\r
15     limitations under the License.\r
16 \r
17 --]]\r
18 \r
19 -- round-robin cache over LRUCache, used within one worker processes\r
20 local _M = {}\r
21 _M._VERSION = '1.0.0'\r
22 \r
23 local lrucache = require "resty.lrucache"\r
24 \r
25 -- we need to initialize the cache on the lua module level so that\r
26 -- it can be shared by all the requests served by each nginx worker process:\r
27 local rrcache,err = lrucache.new(500)  -- allow up to 200 items in the cache\r
28 if not rrcache then\r
29         return ngx.log(ngx.ERR,"failed to create the cache: " .. (err or "unknown"))\r
30 end\r
31 \r
32 function _M.get(key)\r
33         return rrcache:get(key)\r
34 end\r
35 \r
36 function _M.set(key,value)\r
37         rrcache:set(key, value)\r
38 end\r
39 \r
40 return _M