博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[nginx]lua操作redis
阅读量:7063 次
发布时间:2019-06-28

本文共 2516 字,大约阅读时间需要 8 分钟。

 

local redis = require "resty.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            ';

 

转载于:https://www.cnblogs.com/linn/p/4793875.html

你可能感兴趣的文章
NYOJ176 整数划分(二)
查看>>
Linux下利用script命令录制并回放终端会话
查看>>
spark SQL学习(load和save操作)
查看>>
两小时入门 Docker
查看>>
主从复制延时判断
查看>>
render 和 redirect 的区别
查看>>
Java原子类--框架
查看>>
mysql-5.7.19免安装版的配置方法
查看>>
Spring IoC容器初始化过程学习
查看>>
后缀树
查看>>
layer.js中layer.tips
查看>>
字节跳动Android面试凉凉
查看>>
数据结构(1):C语言总结
查看>>
云计算的三种服务模式:IaaS,PaaS和SaaS(转载)
查看>>
JVM垃圾回收机制
查看>>
背包问题
查看>>
要吃鲷鱼到岛上钓
查看>>
图片自适应宽度显示正方形
查看>>
如何提高队列的消息处理效率
查看>>
Java中的代理
查看>>