ngx.shared.DICT.set
syntax: success, err, forcible = ngx.shared.DICT:set(key, value, exptime?, flags?)
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.*
Unconditionally sets a key-value pair into the shm-based dictionary ngx.shared.DICT. Returns three values:
success: boolean value to indicate whether the key-value pair is stored or not.
err: textual error message, can be "no memory".
forcible: a boolean value to indicate whether other valid items have been removed forcibly when out of storage in the shared memory zone.
The value argument inserted can be Lua booleans, numbers, strings, or nil. Their value type will also be stored into the dictionary and the same data type can be retrieved later via the get method.
上面这段话摘自http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT.set 。其中也是只能插入Lua booleans, numbers, strings, or nil.
在 2014年8月6日星期三UTC+8下午2时36分06秒,lhmwzy写道:
> http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT
>
>
>
>
> 在 2014年8月6日 下午2:13,lhmwzy <lhm...@gmail.com>写道:
>
>
> 可以考虑用ngx.ctx?
>
>
>
>
> 在 2014年8月6日 下午2:06, <hebutc...@gmail.com>写道:
>
>
>
>
> 多谢提醒和指点,我应该在linux下发邮件的,这样就可以代码复制了。~~
>
> 下面是之前图片的代码。
>
> location /lua {
>
> content_by_lua '
>
> local redis = require "resty.redis"
>
> local cache = redis.new()
>
> local ok, err = redis.connect(cache, "127.0.0.1", "6379")
>
> cache:set_timeout(6000)
>
> if not ok then
>
> ngx.say("faile to connect", err)
>
> return
>
> end
>
>
>
> local aa = {}
>
> aa[1] = {}
>
> aa[1]["a"] = "aa"
>
> aa[1]["b"] = "bb"
>
>
>
> aa[2] = {}
>
> aa[2]["a"] = "cc"
>
> aa[2]["b"] = "dd"
>
>
>
> for key, value in pairs(aa) do
>
> ngx.say("value[a] is : ", value["a"])
>
> ngx.say("value[b] is : ", value["b"])
>
> end
>
>
>
> res, err = cache:set("dog", aa)
>
> if not ok then
>
> ngx.say("failed to set dog: ", err)
>
> return
>
> end
>
>
>
> ngx.say("set result: ", res)
>
>
>
> res, err = cache:get("dog")
>
>
>
> if not res then
>
> ngx.say("failed to get dog: ", err)
>
> return
>
> end
>
>
>
> if res == ngx.null then
>
> ngx.say("dog not found.")
>
> return
>
> end
>
>
>
> --for key, value in pairs(res) do
>
> -- ngx.say("value[a] is : ", value["a"])
>
> -- ngx.say("value[b] is : ", value["b"])
>
> --end
>
>
>
> ngx.say("res is : ", res)
>
>
>
>
>
> 最初的场景是这样的,我在nginx下接收后台服务器的心跳注册,然后保存其状态,根据策略做负载均衡处理,当客户端有请求时,选出最优的后台服务器与之交互。
>
>
>
> 顺其自然的,我首先想到定义全局表来保存服务器的信息,如
>
> http {
>
> include mime.types;
>
> #default_type application/octet-stream;
>
> default_type text/plain;
>
>
>
> log_format main '$remote_addr - $remote_user [$time_local] "$request" '
>
> '$status $body_bytes_sent "$http_referer" '
>
> '"$http_user_agent" "$http_x_forwarded_for"';
>
>
>
> #access_log logs/$server_name.log main;
>
>
>
> sendfile on;
>
> #tcp_nopush on;
>
>
>
> keepalive_timeout 20;
>
> #keepalive_timeout 65;
>
>
>
> server_tokens off;
>
>
>
> #lua_package_path "/usr/local/lib/lua/5.1";
>
>
>
> access_log on;
>
>
>
> upstream redis_pool {
>
> server 127.0.0.1:6379;
>
> #keepalive 1024 single;
>
> }
>
>
>
>
>
> autoindex_exact_size on;
>
> autoindex_localtime on;
>
>
>
> lua_code_cache on;
>
>
>
> #gzip on;
>
>
>
> init_by_lua ' g_cachetable = {} g_pubtable = {} g_livetable = {} g_target = {} require("LuaXml") g_sort = require("table_sort") ';
>
>
>
> server {
>
> listen 80;
>
> server_name localhost;
>
>
>
> 但是随之而来的问题就是,nginx开启多进程后,会出现有的进程取出的全局变量为空,也就是说定义在这里的全局变量不是多进程之间共享的。
>
>
>
> 所以最后就来到了内存数据库redis。
>
>
>
> 对于我这样的需求,如何进行数据保存,有什么建议么?感谢关注和回复。
>
>
>
>
>
> --
>
>