openwrt redis

2071 make V=s
2072 cd build_dir/target-x86_64_uClibc-0.9.33.2/root-x86/

cd package/network/services/zk-audit/

redis 远程登录   bind 0.0.0.0  /etc/redis/redis.confi 修改这个配置文件就可远程登录上去  

启动服务端 redis-server      1162 root     27372 S    redis-server 0.0.0.0:6379

启动客户端 redis-cli  

常用命令 : 

     1 显示所有的键值   keys *

127.0.0.1:6379> keys *
 1) "term_app_info:1"
 2) "term_info:18"
 3) "term_info:23"
 4) "term_info:29"
 5) "term_info:11"

   2  获取值的信息

127.0.0.1:6379> HGETall term_info:36
 1) "timestamp"
 2) "1533185362"
 3) "mac"

  

  3 准备用lua 连接redis 数据库

          然后从找到了开源的redis lua   https://github.com/openresty/lua-resty-redis

    https://github.com/gitforks/redis-lua

          然后又来个了nigx配置

     D:\GIT\Q3_mysql\zk_openwrt\wac\nginx.conf  

    

    location /demo {
      default_type text/html;
      content_by_lua '
      ngx.say("<p>hello, nginx</p>")';  
    }

           通过web可以访问      http://192.168.1.1:8080/demo  

          页面看到    hello, nginx       (ngx.say 说在页面上的  )

          文件路径很重要          /etc/nginx/nginx.conf   

         新加一个server的配置

  

    server {
        listen       8822;
        server_name  localhost;
        root /tmp/portal_cache/;

        location /test {
            content_by_lua '
                local redis = require "luci.lib.redis"
                local red = redis:new()

                red:set_timeout(1000) -- 1 sec

                -- or connect to a unix domain socket file listened
                -- by a redis server:
                --     local ok, err = red:connect("unix:/path/to/redis.sock")

                local ok, err = red:connect("127.0.0.1", 6379)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

                ok, err = red:set("dog", "an animal")
                if not ok then
                    ngx.say("failed to set dog: ", err)
                    return
                end

                ngx.say("set result: ", ok)

                local res, err = red: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

                ngx.say("dog: ", res)

                red:init_pipeline()
                red:set("cat", "Marry")
                red:set("horse", "Bob")
                red:get("cat")
                red:get("horse")
                local results, err = red:commit_pipeline()
                if not results then
                    ngx.say("failed to commit the pipelined requests: ", err)
                    return
                end

                for i, res in ipairs(results) do
                    if type(res) == "table" then
                        if res[1] == false then
                            ngx.say("failed to run command ", i, ": ", res[2])
                        else
                            -- process the table value
                        end
                    else
                        -- process the scalar value
                    end
                end

                -- put it into the connection pool of size 100,
                -- with 10 seconds max idle time
                local ok, err = red:set_keepalive(10000, 100)
                if not ok then
                    ngx.say("failed to set keepalive: ", err)
                    return
                end

                -- or just close the connection right away:
                -- local ok, err = red:close()
                -- if not ok then
                --     ngx.say("failed to close: ", err)
                --     return
                -- end
            ';
        }
    }

http://192.168.1.1:8822/test 通过访问触发就可以修改数据库, 测试有效

 /usr/local/openresty/nginx/nginx.conf    

在我的虚拟机里面执行的: openresty -c ~/work/conf/nginx.conf

这个是他的中文网站 : http://openresty.org/cn/components.html   

www.xiaohuamao.top:8022 体验了下openresty    

git hub 地址 https://github.com/openresty/openresty  可以学习下

这里还有个开源项目 https://gitee.com/alexa/VeryNginx

 

Redis transactions  事务 所以 Redis 的内部可以保持简单且快速

client:select(15) -- for testing purposes 选择15号数据库, 默认在0号数据库, 你在15号数据库找0号数据库的东西怎么找得到呢? 

从redis 里面读取数据然后写入mysql 里面永久存储

从redis 里面读取数据然后显示到页面上  速度快的很嘛  

DBSIZE 可以看到有多少条数据 keys * 可能卡

-- 演示将多条记录数据组合成一条sql插入语句(for mysql)
 
function getTpl0(tname)     -- 获取表各个字段
    local t = {
        tpl_pack = {"packId","itemId","`group`","num","rate","rateType"},
    }
    for k, v in pairs(t) do
        if tname == k then
            return t[k]
        end
    end
end
 
--tpl = {3813,10,0,2,0,1,1,0,350,5,220,6,0,0,0,0,154,0,0,0,210,80,29}
tpl9122 = {
-- "packId","itemId","`group`","num","rate","rateType"
    {9122, 3294, '', 1, 1, 2},
    {9122, 3295, '', 1, 1, 2},
    {9122, 3296, '', 1, 1, 2},
    {9122, 3297, '', 1, 1, 2},
    {9122, 3298, '', 1, 1, 2},
 
    {9122, 9004, '', 2, 4, 2},
    {9122, 117, '', 8, 4, 2},
    {9122, 118, '', 8, 4, 2},
    {9122, 119, '', 8, 4, 2},
    {9122, 120, '', 8, 4, 2},
    {9122, 322, '', 2, 4, 2},
    {9122, 160, '', 5, 5, 2},
    {9122, 327, '', 5, 5, 2},
    {9122, 2900, '', 1, 6, 2},
    {9122, 9101, '', 20, 7, 2},
    {9122, 115, '', 15, 10, 2},
    {9122, 114, '', 15, 12, 2},
    {9122, 112, '', 15, 13, 2},
    {9122, 113, '', 15, 13, 2},
}
 
tpl9123 = {
-- "packId","itemId","`group`","num","rate","rateType"
    {9123, 3299, '', 1, 1, 2},
    {9123, 3300, '', 1, 1, 2},
    {9123, 3301, '', 1, 1, 2},
    {9123, 3302, '', 1, 1, 2},
    {9123, 3303, '', 1, 1, 2},
 
    {9123, 9004, '', 2, 4, 2},
    {9123, 117, '', 8, 4, 2},
    {9123, 118, '', 8, 4, 2},
    {9123, 119, '', 8, 4, 2},
    {9123, 120, '', 8, 4, 2},
    {9123, 322, '', 2, 4, 2},
    {9123, 160, '', 5, 5, 2},
    {9123, 327, '', 5, 5, 2},
    {9123, 2900, '', 1, 6, 2},
    {9123, 9101, '', 20, 7, 2},
    {9123, 115, '', 15, 10, 2},
    {9123, 114, '', 15, 12, 2},
    {9123, 112, '', 15, 13, 2},
    {9123, 113, '', 15, 13, 2},
}
 
function createInsertSql(tname, tpl)
    local tpl0 = getTpl0(tname)     -- 获取表各个字段
    local ret = {}                  -- 插入记录sql
 
    table.insert(ret, string.format("insert into `%s`(", tname))
    for k, v in pairs(tpl0) do
        if k > 1 then
            table.insert(ret, ",")
        end
        table.insert(ret, v)
    end
    table.insert(ret, ") values ")
 
    for k, v in pairs(tpl) do
        if k > 1 then
            table.insert(ret, ",")
        end
        table.insert(ret, "(")
        for k2, v2 in pairs(v) do
            if k2 > 1 then
                table.insert(ret, ",")
            end
            if type(v2) == "string" then
                table.insert(ret, string.format("'%s'", v2))
            else
                table.insert(ret, v2)
            end
        end
        table.insert(ret, ")")
    end
    table.insert(ret, ";")
 
    local result = table.concat(ret)    -- 最终的sql语句
    print(result)
    print()
end
createInsertSql("tpl_pack", tpl9122)
createInsertSql("tpl_pack", tpl9123)
 最终的执行结果如下:

1
2
3
4
[zcm@lua 6]$lua t1.lua
insert into `tpl_pack`(packId,itemId,`group`,num,rate,rateType) values (9122,3294,'',1,1,2),(9122,3295,'',1,1,2),(9122,3296,'',1,1,2),(9122,3297,'',1,1,2),(9122,3298,'',1,1,2),(9122,9004,'',2,4,2),(9122,117,'',8,4,2),(9122,118,'',8,4,2),(9122,119,'',8,4,2),(9122,120,'',8,4,2),(9122,322,'',2,4,2),(9122,160,'',5,5,2),(9122,327,'',5,5,2),(9122,2900,'',1,6,2),(9122,9101,'',20,7,2),(9122,115,'',15,10,2),(9122,114,'',15,12,2),(9122,112,'',15,13,2),(9122,113,'',15,13,2);
 
insert into `tpl_pack`(packId,itemId,`group`,num,rate,rateType) values (9123,3299,'',1,1,2),(9123,3300,'',1,1,2),(9123,3301,'',1,1,2),(9123,3302,'',1,1,2),(9123,3303,'',1,1,2),(9123,9004,'',2,4,2),(9123,117,'',8,4,2),(9123,118,'',8,4,2),(9123,119,'',8,4,2),(9123,120,'',8,4,2),(9123,322,'',2,4,2),(9123,160,'',5,5,2),(9123,327,'',5,5,2),(9123,2900,'',1,6,2),(9123,9101,'',20,7,2),(9123,115,'',15,10,2),(9123,114,'',15,12,2),(9123,112,'',15,13,2),(9123,113,'',15,13,2);

 

posted @ 2018-08-02 10:25  _liulin  阅读(1384)  评论(0编辑  收藏  举报