lua-resty-tlc
通用两级缓存 (lrucache + 共享字典)
$ opm get hamishforbes/lua-resty-tlc
#lua-resty-tlc
使用 lua-resty-lrucache 和 共享字典 实现的两层缓存。
缓存条目写入当前工作进程的 lru-cache 和共享字典。
如果工作进程的 lru-cache 实例中未命中,则从共享字典中重新填充缓存读取(如果可用)。
共享字典中的值会自动序列化和反序列化为 JSON(支持自定义序列化函数)。
还提供了一个管理器模块来维护 TLC 缓存实例的全局集合。
#概述
lua_package_path "/path/to/lua-resty-tlc/lib/?.lua;;";
lua_shared_dict tlc_cache 10m;
lua_shared_dict tlc_cache2 1m;
init_by_lua_block {
local manager = require("resty.tlc.manager")
manager.new("my_cache", {size = 500, dict = "tlc_cache"})
manager.new("my_cache2", {size = 500, dict = "tlc_cache2"})
}
location = /get {
content_by_lua_block {
local manager = require("resty.tlc.manager")
local cache = manager.get("my_cache")
local args = ngx.req.get_uri_args()
local key = args["key"]
local data, err = cache:get(key)
if err then
ngx.log(ngx.ERR, err)
elseif data == nil then
ngx.status = ngx.HTTP_NOT_FOUND
ngx.say("Not Found")
else
ngx.say(tostring(data))
end
}
}
location = /set {
content_by_lua_block {
local manager = require("resty.tlc.manager")
local cache = manager.get("my_cache")
local args = ngx.req.get_uri_args()
local key = args["key"]
local val = args["val"] or { foo = bar }
local ttl = args["ttl"]
local ok, err = cache:set(key, val, ttl)
if not ok then
ngx.log(ngx.ERR, err)
end
}
}
location = /flush {
content_by_lua_block {
local manager = require("resty.tlc.manager")
local cache = manager.get("my_cache")
cache:flush()
}
}
location = /list {
content_by_lua_block {
local manager = require("resty.tlc.manager")
local instances = manager.list()
ngx.say(require("cjson").encode(instances))
}
}
#方法
管理器
新建
语法:ok, err = manager.new(name, opts)
使用给定的名称/ID 和选项创建一个新的 resty.tlc.cache
实例。
不会检查实例是否已存在,现有实例将被覆盖。
获取
语法:cache = manager.get(name)
返回指定的 TLC 缓存实例或 nil。
删除
语法:manager.delete(name)
删除指定的缓存实例。
列表
语法:instances = manager.list()
返回可用缓存实例的数组表。
缓存
新建
语法:instance = cache:new(opts)
创建一个新的 resty.tlc.cache
实例,opts
是此实例的选项表。
opts = {
dict = dict, -- Shared dictionary name, required
size = size, -- max_items parameter for LRU cache, optional, default 200
pureffi = pureffi, -- Use the pureffi LRU cache variant, optional, default false
loadfactor = loadfactor, -- Load factor for pureffi LRU cache, optional
serialiser = serialiser, -- Function to serialise values when saving to shared dictionary, optional, defaults to pcall'd cjson encode
unserialiser = unserialiser, -- Function to unserialise values when saving to shared dictionary, optional, defaults to pcall'd cjson decode
}
序列化和反序列化函数应在失败时 return nil, err
。
设置
语法:ok, err = cache:set(key, value, ttl?)
设置或更新缓存中的条目。
ttl
是可选的,单位为秒。
获取
语法:data = cache:get(key)
从缓存中返回数据,如果未设置则返回 nil
。
删除
语法:cache:delete(key)
从 LRU 缓存和共享字典中删除条目。
待办事项:在所有工作进程中从 LRU 缓存中删除。
刷新
语法:cache:flush(hard?)
重新初始化当前工作进程中的 LRU 缓存并刷新共享字典。
hard
参数还将在字典上调用 flush_expired()
。
待办事项:在所有工作进程中重新初始化 LRU 缓存。
待办事项
向 ngx_lua 共享字典添加功能以检索条目的剩余 TTL。
同步工作进程之间的 LRU 缓存删除/刷新。
作者
Hamish Forbes
许可证
mit
版本
-
通用两级缓存 (lrucache + 共享字典) 2016-10-03 09:46:52