2019-10-31 09:27:28 +08:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You 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.
|
|
|
|
#
|
2019-09-10 09:42:55 +08:00
|
|
|
use t::APISIX 'no_plan';
|
2019-06-10 20:40:39 +08:00
|
|
|
|
|
|
|
repeat_each(1);
|
|
|
|
no_long_string();
|
|
|
|
no_root_location();
|
|
|
|
log_level("info");
|
|
|
|
|
|
|
|
run_tests;
|
|
|
|
|
|
|
|
__DATA__
|
|
|
|
|
|
|
|
=== TEST 1: sanity
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local core = require("apisix.core")
|
|
|
|
|
|
|
|
local idx = 0
|
|
|
|
local function create_obj()
|
|
|
|
idx = idx + 1
|
|
|
|
return {idx = idx}
|
|
|
|
end
|
|
|
|
|
|
|
|
local obj = core.lrucache.global("key", nil, create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
|
|
|
|
obj = core.lrucache.global("key", nil, create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
|
|
|
|
obj = core.lrucache.global("key", "1", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
obj: {"idx":1}
|
|
|
|
obj: {"idx":1}
|
2020-09-27 11:38:05 +08:00
|
|
|
obj: {"idx":2}
|
2019-06-10 20:40:39 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-02 16:47:32 +08:00
|
|
|
=== TEST 2: new
|
2019-06-10 20:40:39 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local core = require("apisix.core")
|
|
|
|
|
|
|
|
local idx = 0
|
|
|
|
local function create_obj()
|
|
|
|
idx = idx + 1
|
|
|
|
return {idx = idx}
|
|
|
|
end
|
|
|
|
|
|
|
|
local lru_get = core.lrucache.new()
|
|
|
|
|
|
|
|
local obj = lru_get("key", nil, create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
|
|
|
|
obj = lru_get("key", nil, create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
|
|
|
|
obj = lru_get("key", "1", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
|
|
|
|
obj = lru_get("key", "1", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
|
|
|
|
obj = lru_get("key-different", "1", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-10 20:40:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
obj: {"idx":1}
|
|
|
|
obj: {"idx":1}
|
2020-09-27 11:38:05 +08:00
|
|
|
obj: {"idx":2}
|
|
|
|
obj: {"idx":2}
|
|
|
|
obj: {"idx":3}
|
2019-06-10 20:40:39 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2019-06-12 10:34:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-02 16:47:32 +08:00
|
|
|
=== TEST 3: cache the non-table object, eg: number or string
|
2019-06-12 10:34:45 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local core = require("apisix.core")
|
|
|
|
|
|
|
|
local idx = 0
|
|
|
|
local function create_num()
|
|
|
|
idx = idx + 1
|
|
|
|
return idx
|
|
|
|
end
|
|
|
|
|
|
|
|
local obj = core.lrucache.global("key", nil, create_num)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-12 10:34:45 +08:00
|
|
|
|
|
|
|
obj = core.lrucache.global("key", nil, create_num)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-12 10:34:45 +08:00
|
|
|
|
|
|
|
obj = core.lrucache.global("key", "1", create_num)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2019-06-12 10:34:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
obj: 1
|
|
|
|
obj: 1
|
|
|
|
obj: 2
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2019-08-07 13:42:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-02 16:47:32 +08:00
|
|
|
=== TEST 4: sanity
|
2019-08-07 13:42:48 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local core = require("apisix.core")
|
|
|
|
|
|
|
|
local function server_release(self)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("release: ", require("toolkit.json").encode(self))
|
2019-08-07 13:42:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local lrucache_server_picker = core.lrucache.new({
|
|
|
|
ttl = 300, count = 256, release = server_release,
|
|
|
|
})
|
|
|
|
|
|
|
|
local t1 = lrucache_server_picker("nnn", "t1",
|
|
|
|
function () return {name = "aaa"} end)
|
|
|
|
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(t1))
|
2019-08-07 13:42:48 +08:00
|
|
|
|
|
|
|
local t2 = lrucache_server_picker("nnn", "t2",
|
|
|
|
function () return {name = "bbb"} end)
|
|
|
|
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(t2))
|
2019-08-07 13:42:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2020-09-27 11:38:05 +08:00
|
|
|
obj: {"name":"aaa"}
|
|
|
|
release: {"name":"aaa"}
|
|
|
|
obj: {"name":"bbb"}
|
2019-08-07 13:42:48 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2020-02-19 20:30:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-02 16:47:32 +08:00
|
|
|
=== TEST 5: invalid_stale = true
|
2020-02-19 20:30:34 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local core = require("apisix.core")
|
|
|
|
|
|
|
|
local idx = 0
|
|
|
|
local function create_obj()
|
|
|
|
idx = idx + 1
|
|
|
|
return {idx = idx}
|
|
|
|
end
|
|
|
|
|
|
|
|
local lru_get = core.lrucache.new({
|
|
|
|
ttl = 0.1, count = 256, invalid_stale = true,
|
|
|
|
})
|
|
|
|
|
|
|
|
local obj = lru_get("key", "ver", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2020-02-19 20:30:34 +08:00
|
|
|
local obj = lru_get("key", "ver", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2020-02-19 20:30:34 +08:00
|
|
|
|
|
|
|
ngx.sleep(0.15)
|
|
|
|
local obj = lru_get("key", "ver", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2020-02-19 20:30:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2020-09-27 11:38:05 +08:00
|
|
|
obj: {"idx":1}
|
|
|
|
obj: {"idx":1}
|
|
|
|
obj: {"idx":2}
|
2020-02-19 20:30:34 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2020-04-22 17:47:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-02 16:47:32 +08:00
|
|
|
=== TEST 6: when creating cached objects, use resty-lock to avoid repeated creation.
|
2020-04-22 17:47:16 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local core = require("apisix.core")
|
|
|
|
|
|
|
|
local idx = 0
|
|
|
|
local function create_obj()
|
|
|
|
idx = idx + 1
|
|
|
|
ngx.sleep(0.1)
|
|
|
|
return {idx = idx}
|
|
|
|
end
|
|
|
|
|
|
|
|
local lru_get = core.lrucache.new({
|
2020-11-02 16:47:32 +08:00
|
|
|
ttl = 1, count = 256, invalid_stale = true, serial_creating = true,
|
2020-04-22 17:47:16 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
local function f()
|
|
|
|
local obj = lru_get("key", "ver", create_obj)
|
2020-11-30 10:32:35 +08:00
|
|
|
ngx.say("obj: ", require("toolkit.json").encode(obj))
|
2020-04-22 17:47:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
ngx.thread.spawn(f)
|
|
|
|
ngx.thread.spawn(f)
|
|
|
|
|
|
|
|
ngx.sleep(0.3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2020-09-27 11:38:05 +08:00
|
|
|
obj: {"idx":1}
|
|
|
|
obj: {"idx":1}
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-02 16:47:32 +08:00
|
|
|
=== TEST 7: different `key` and `ver`, cached same one table
|
2020-09-27 11:38:05 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local core = require("apisix.core")
|
|
|
|
|
|
|
|
local item = {}
|
|
|
|
local idx = 0
|
|
|
|
local function create_obj()
|
|
|
|
idx = idx + 1
|
|
|
|
ngx.say("create obj ", idx, " time")
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
|
|
|
|
local lru_get = core.lrucache.new({
|
|
|
|
ttl = 10, count = 256
|
|
|
|
})
|
|
|
|
|
|
|
|
local obj = lru_get("key", "ver", create_obj)
|
|
|
|
ngx.say("fetch obj: ", obj == item)
|
|
|
|
|
|
|
|
obj = lru_get("key2", "ver2", create_obj)
|
|
|
|
ngx.say("fetch obj: ", obj == item)
|
|
|
|
|
|
|
|
obj = lru_get("key", "ver", create_obj)
|
|
|
|
ngx.say("fetch obj: ", obj == item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
create obj 1 time
|
|
|
|
fetch obj: true
|
|
|
|
create obj 2 time
|
|
|
|
fetch obj: true
|
|
|
|
fetch obj: true
|
2020-04-22 17:47:16 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|