一、lua中redis的配置依赖:

1、OpenResty的lua访问redis的插件:https://github.com/openresty/lua-resty-redis

二、下载后,导入对应的插件:

lua_package_path "/opt/openresty/lualib/kafka/?.lua;;";
lua_need_request_body on;

三、lua代码

2、使用lua访问redis:

server {
        location /test {
            content_by_lua_block {
                local redis = require "resty.redis"
                local red = redis:new()

red:set_timeout(1000)

-- 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
            }
        }
    }

3、使用redis连接池

local ok, err = red:set_keepalive(60000, 20)

4、需要密码的redis的访问:使用 auth 方法

local ok, err = red.connect(red, "127.0.0.1", "6379")
    if not ok then
        return
    end

local res, err = red:auth("password")
    if not res then
        return
    end

5、redis的操作,不需要单独封装方法,lua-resty-redis 支持自动生成对应的lua方法

具体配置方法是:redis.lua 中,common_cmds 的array,在这里添加需要使用的方法

例如:需要使用redis hsah的hincrby,那么就在 common_cmds 添加 hincrby,在lua中直接使用就可以,red:hincrby(key, field, 1)

6、项目中的使用场景

(1)前端http查询一些数据,直接在nginx中通过lua访问redis拿到,直接返回到前端,减少服务器的压力;

    redis中数据通过服务器进行主动更新

(2)点击次数和页面打开次数分析:在点击和页面打开之间,加上了请求到达nginx的统计,当请求到达nginx时,通过lua将访问的页面次数写入redis中,然后通过点击次数、nginx获得的请求次数、页面打开次数进行具体业务的分析

OpenResty + Lua访问Redis,实现高并发访问时的毫秒级响应打回的更多相关文章

  1. [转]高并发访问下避免对象缓存失效引发Dogpile效应

    避免Redis/Memcached缓存失效引发Dogpile效应 Redis/Memcached高并发访问下的缓存失效时可能产生Dogpile效应(Cache Stampede效应). 推荐阅读:高并 ...

  2. (实例篇)php 使用redis锁限制并发访问类示例

    1.并发访问限制问题 对于一些需要限制同一个用户并发访问的场景,如果用户并发请求多次,而服务器处理没有加锁限制,用户则可以多次请求成功. 例如换领优惠券,如果用户同一时间并发提交换领码,在没有加锁限制 ...

  3. php 使用redis锁限制并发访问类

    1.并发访问限制问题 对于一些需要限制同一个用户并发访问的场景,如果用户并发请求多次,而服务器处理没有加锁限制,用户则可以多次请求成功. 例如换领优惠券,如果用户同一时间并发提交换领码,在没有加锁限制 ...

  4. nginx+lua+redis构建高并发应用(转)

    nginx+lua+redis构建高并发应用 ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求. url请求nginx服务器,然后lua查询redis, ...

  5. redis是单进程数据库,多用户排队对统一数据进行访问,不存在并发访问生产的线程安全问题

    redis是单进程数据库,多用户排队对统一数据进行访问,不存在并发访问生产的线程安全问题. oracle是多进程数据库,存在并发访问的问题,必须事务加锁等方式进行处理.

  6. Nginx与Redis解决高并发问题

    原文链接:http://bbs.phpchina.com/forum.php?mod=viewthread&tid=229629 第一版产品采用的是Jquery,Nginx,PHP(CI框架) ...

  7. Redis实现高并发分布式序列号

    使用Redis实现高并发分布式序列号生成服务 序列号的构成 为建立良好的数据治理方案,作数据掌握.分析.统计.商业智能等用途,业务数据的编码制定通常都会遵循一定的规则,一般来讲,都会有自己的编码规则和 ...

  8. 关于Redis处理高并发

    Redis的高并发和快速原因 1.Redis是基于内存的,内存的读写速度非常快: 2.Redis是单线程的,省去了很多上下文切换线程的时间: 3.Redis使用多路复用技术,可以处理并发的连接.非阻塞 ...

  9. 《Netty Redis Zookeeper 高并发实战》 勘误

    <Netty Redis Zookeeper 高并发实战> 勘误与申明 疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 勘误一 文字问题: Page1 J ...

随机推荐

  1. 斑马105SLPlus串口打印二维码

    1.根据说明书调试硬件,校准介质还有色带(很重要),我自己搞了好几天才搞明白. 2.设置好参数,比如打印介质连续.非连续,热敏还是热转质 3.打印机上电后悔自动校准,校准成功后就可以直接通过串口打印, ...

  2. PHP mysqli_get_connection_stats() 函数

    定义和用法 mysqli_get_connection_stats() 函数返回有关客户端连接的统计. 语法 mysqli_get_connection_stats(connection); 返回有关 ...

  3. Burpsuite 2.0.11 Beta 破解版下载

    1.解包 jar xvf burpsuite_pro_v2.0.11beta.jar 自行定制,删除自带chrome和7zip软件包之后,软件精简至39M. 2.打包 jar cvfm META-IN ...

  4. canvas实现水印

    最近遇到一个需求,给所有页面加水印(登录人),不影响其他点击等功能的使用,目的是防止信息外漏,当时就在想:这年头,PS就不说人人都能使用,谁手机还没个涂鸦功能,防不了,但是就是这么个需求,那就实现吧! ...

  5. ubuntu 安装eclipse for c++

    linux的GUI和windos比起来实在逊色,虽然它的终端模式(命令行模式)非常强大.linux发行版ubuntu的GUI相对其他版本要华丽一些,所以最近由redhat转向ubuntu进行linux ...

  6. decimal模块 --数字的精度、保留小数位数、取整问题

    开始之前需要注意一点是:精度值为数字的总位数,如:1.23, 精度值为3: 0.123,精度值也为3 1.更改默认精度值后,直接进行计算即可保留对应精度值 from decimal import ge ...

  7. Java-Unsafe

    Unsafe 是 sun.misc 包下的一个类,可以直接操作堆外内存,可以随意查看及修改 JVM 中运行时的数据,使 Java 语言拥有了类似 C 语言指针一样操作内存空间的能力. Unsafe 的 ...

  8. PHP-生产随机验证码图片

    // <span style="white-space:pre"> </span>//因为要把产生的验证码保存到session中,此处为session开始 ...

  9. centos7 开启80端口

    关闭与开启防火墙 systemctl stop firewalld.servicesystemctl start firewalld.service 先查看防火墙是否开启的状态,以及开放端口的情况:s ...

  10. YOLO: You Only Look Once论文阅读摘要

    论文链接: https://arxiv.org/pdf/1506.02640.pdf 代码下载: https://github.com/gliese581gg/YOLO_tensorflow Abst ...