ngx.shared.DICT.expire 详解
ngx.shared.DICT.expire
原文链接: ngx.shared.DICT.expire
syntax: success, err = ngx.shared.DICT:expire(key, exptime)
context: init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*,
header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*,
balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*,
ssl_session_store_by_lua*
requires: resty.core.shdict or resty.core
设置存储于共享内存 ngx.shared.DICT 中的键值对的过期时间(以秒为单位)。如果操作完成返回布尔值指示成功,否则返回 nil 和错误信息字符串。
如果 key 不存在,则该方法返回 nil 和错误信息字符串 "not found"。
exptime 过期参数的分辨率为 0.001 秒,如果 exptime 为 0,则该项将永不过期。如下:
require "resty.core"
local cats = ngx.shared.cats
local succ, err = cats:set("Marry", "a nice cat", 0.1)
succ, err = cats:expire("Marry", 0.5)
ngx.sleep(0.2)
local val, err = cats:get("Marry")
ngx.say(val) -- "a nice cat"
expire 源码实现
local function shdict_expire(zone, kye, exptime)
zone = check_zone(zone)
if not exptime then
error('bad "exptime" argument', 2)
end
if key == nil then
return nil, "nil key"
end
if (type(key) ~= "string") then
key = tostring(key)
end
local key_len = #key
if key_len == 0 then
return nil, "empty key"
end
if key_len > 65535 then
return nil, "key too long"
end
local rc = C.ngx_http_lua_ffi_shdict_set_expire(zone, key, key_len,
exptime * 1000)
if rc == FFI_ERROR then
return nil, "bad zone"
end
if rc == FFI_DECLINED then
return nil, "not found"
end
-- NGINX_OK/FFI_OK
return true
end
ngx_http_lua_ffi_shdict_set_expire
int
ngx_http_lua_ffi_shdict_set_exptire(ngx_shm_zone_t *zone, u_char *key,
size_t key_len, long exptime)
{
uint32_t hash;
ngx_int_t rc;
ngx_time_t *tp = NULL;
ngx_http_lua_shdict_ctx_t *ctx;
ngx_http_lua_shdict_node_t *sd;
if (zone == NULL) {
return NGX_ERROR;
}
if (exptime > 0) {
tp = ngx_timeofday();
}
ctx = zone->data;
hash = ngx_crc32_short(key, key_len);
ngx_shmtx_lock(&ctx->shpool->mutex);
rc = ngx_http_lua_shdict_peek(zone, hash, key, key_len, &sd);
if (rc == NGX_DECLINED) {
ngx_shmtx_unlock(&ctx->shpool->mutex);
return NGX_DECLINED;
}
/* rc == NGX_OK */
if (exptime > 0) {
sd->expires = (uint64_t) tp->sec * 1000 + tp->msec
+ (uint64_t) exptime;
} else {
sd->expires = 0;
}
ngx_shmtx_unlock(&ctx->shpool->mutex);
return NGX_OK;
}
ngx_http_lua_shdict_peek
static ngx_int_t
ngx_http_lua_shdict_peek(ngx_shm_zone_t *shm_zone, ngx_uint_t hash,
u_char *kdata, size_t klen, ngx_http_lua_shdict_node_t **sdp)
{
ngx_int_t rc;
ngx_rbtree_node_t *node, *sentinel;
ngx_http_lua_shdict_ctx_t *ctx;
ngx_http_lua_shdict_node_t *sd;
ctx = shm_zone->data;
node = ctx->sh->rbtree.root;
sentinel = ctx->sh->rbtree.sentinel;
while (node != sentinel) {
if (hash < node->key) {
node = node->left;
continue;
}
if (hash > node->key) {
node = node->right;
continue;
}
/* hash == node->key */
sd = (ngx_http_lua_shdict_node_t *) &node->color;
rc = ngx_memn2cmp(kdata, sd->data, klen, (size_t) sd->key_len);
if (rc == 0) {
*sdp = sd;
return NGX_OK;
}
node = (rc < 0) ? node->left : node->right;
}
*sdp = NULL;
return NGX_DECLINED;
}
ngx.shared.DICT.expire 详解的更多相关文章
- ngx.shared.DICT.get 详解
ngx.shared.DICT.get 原文: ngx.shared.DICT.get syntax: value, flags = ngx.shared.DICT:get(key) context: ...
- ngx.shared.DICT.incr 详解
ngx.shared.DICT.incr 原文: ngx.shared.DICT.incr syntax: newval, err, forcible? = ngx.shared.DICT:incr( ...
- OpenResty之ngx.shared.DICT
参考链接: resty.core.shdict ngx_shared.DICT 源码正文: dict.lua 部分源码如下: local ffi = require 'ffi' local base ...
- ngx.shared.DICT.set
ngx.shared.DICT.set 原文: ngx.shared.DICT.set syntax: success, err, forcible = ngx.shared.DICT:set(key ...
- Python 中的字符串(str)、字典(dict)详解及操作方法
一.字符串 在python中字符串是一种重要数据类型.其他数据类型分别为: 数字-number -------- int.long.float.complex这几种 字符串-string ------ ...
- Python之dict字典详解
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,字典是另 一种可变容器模型,且可存储任意类型对象.具有极快的查找速度. 字典是一种通过名字或者关键字 ...
- 【精选】Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...
- Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...
- Python中dict详解
from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...
随机推荐
- js 递归获取子节点所有父节点,深度遍历获取第一个子树
前端需求. 递归 深度优先遍历算法 // 查找一个节点的所有父节点 familyTree (arr1, id) { var temp = [] var forFn = function (arr, i ...
- vue 打印html
1.https://github.com/xyl66/vuePlugs_printjs从这个路径下载print.js.放到你的代码中 2.我是放到我本地一个js文件中. 3.引入当前文件 //打印插件 ...
- Ubuntu armhf 版本国内源
Ubuntu armhf 版本国内源: deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted u ...
- 推荐系统(5)---大量项目topk近邻相似度
Kd树+BBF(最邻近.次邻近查询)Python实现 kd树和BBF算法 精确Top-K检索及其加速方法探讨
- 《hello--world团队》第六次作业:团队项目系统设计改进与详细设计
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十 团队作业6:团队项目系统设计改进与详细设计 团队名称 <hello--wo ...
- 搭建jenkins+python+selenium+robot framework环境
1.安装jenkins 具体参考:https://www.cnblogs.com/dydxw/p/10538103.html 2.下载插件 我是为了方便,把有关python.selenium.robo ...
- Java8 中的 Optional 相关用法
基本方法: ofNullable() 为可能 null 的值创建一个 Optional 实例, 然后可以对该实例遍历/过滤, 判断是否存在,或者为空时执行.. ifPresent(...) 如果值存 ...
- ASP.NET MVC 入门6、TempData
TempData用来给控制各Action间传递值,或Action给View传递临时值时使用. TempData实际是将值临时存储于Session中. TempData中存储的值只能供一次访问使用, 即 ...
- POJ-2065-SETI(高斯消元)
链接: https://vjudge.net/problem/POJ-2065 题意: For some years, quite a lot of work has been put into li ...
- centos6.5 安装163yum源
1.下载yum源 http://mirrors.163.com/.help/CentOS6-Base-163.repo 2.把下载好的yum源放到/etc/yum.repos.d下 mv CentOS ...