1. local redis = require "resty.redis"
  2. local red = redis:new()
  3.  
  4. red:set_timeout() -- sec
  5.  
  6. -- or connect to a unix domain socket file listened
  7. -- by a redis server:
  8. -- local ok, err = red:connect("unix:/path/to/redis.sock")
  9.  
  10. local ok, err = red:connect("127.0.0.1", )
  11. if not ok then
  12. ngx.say("failed to connect: ", err)
  13. return
  14. end
  15.  
  16. ok, err = red:set("dog", "an animal")
  17. if not ok then
  18. ngx.say("failed to set dog: ", err)
  19. return
  20. end
  21.  
  22. ngx.say("set result: ", ok)
  23.  
  24. local res, err = red:get("dog")
  25. if not res then
  26. ngx.say("failed to get dog: ", err)
  27. return
  28. end
  29.  
  30. if res == ngx.null then
  31. ngx.say("dog not found.")
  32. return
  33. end
  34.  
  35. ngx.say("dog: ", res)
  36.  
  37. red:init_pipeline()
  38. red:set("cat", "Marry")
  39. red:set("horse", "Bob")
  40. red:get("cat")
  41. red:get("horse")
  42. local results, err = red:commit_pipeline()
  43. if not results then
  44. ngx.say("failed to commit the pipelined requests: ", err)
  45. return
  46. end
  47.  
  48. for i, res in ipairs(results) do
  49. if type(res) == "table" then
  50. if res[] == false then
  51. ngx.say("failed to run command ", i, ": ", res[])
  52. else
  53. -- process the table value
  54. end
  55. else
  56. -- process the scalar value
  57. end
  58. end
  59.  
  60. -- put it into the connection pool of size ,
  61. -- with seconds max idle time
  62. local ok, err = red:set_keepalive(, )
  63. if not ok then
  64. ngx.say("failed to set keepalive: ", err)
  65. return
  66. end
  67.  
  68. -- or just close the connection right away:
  69. -- local ok, err = red:close()
  70. -- if not ok then
  71. -- ngx.say("failed to close: ", err)
  72. -- return
  73. -- end
  74. ';

[nginx]lua操作redis的更多相关文章

  1. 使用Nginx Lua实现redis高性能http接口

    使用Nginx Lua实现redis高性能http接口 时间 -- :: 峰云就她了 原文 http://xiaorui.cc/2015/01/27/使用nginx-lua实现redis高性能http ...

  2. nginx lua mysql redis设置

    最近公司网站改版,程序和数据库全部用新版,旧版的数据要导入,旧网站的30万条数据url要全部重定向到新版网站,正好前段时间在学习nginx+lua+mysql+memcache(redis),找资料真 ...

  3. lua 操作redis

    Redis在2.6推出了脚本功能,允许开发者使用Lua语言编写脚本传到Redis中执行.使用脚本的好处如下: 1.减少网络开销:本来5次网络请求的操作,可以用一个请求完成,原先5次请求的逻辑放在red ...

  4. Nginx+Lua+MySQL/Redis实现高性能动态网页展现

    Nginx结合Lua脚本,直接绕过Tomcat应用服务器,连接MySQL/Redis直接获取数据,再结合Lua中Template组件,直接写入动态数据,渲染成页面,响应前端,一次请求响应过程结束.最终 ...

  5. Nginx Lua拓展模块操作Redis、Mysql

    # Nginx的拓展模块 # ngx_lua模块 # 淘宝开发的ngx_lua模块通过lua解释器集成近Nginx,可以采用lua脚本实现业务逻辑,由于lua的紧凑.快速以及内建协程,所以在保证宝兵法 ...

  6. lua操作json,mysql,redis等

    ==========================example for lua json======================= local cjson = require("cj ...

  7. 高并发 Nginx+Lua OpenResty系列(9)——HTTP服务

    此处我说的HTTP服务主要指如访问京东网站时我们看到的热门搜索.用户登录.实时价格.实时库存.服务支持.广告语等这种非Web页面,而是在Web页面中异步加载的相关数据.这些服务有个特点即访问量巨大.逻 ...

  8. nginx+lua+redis初体验

    1.下载nginx.lua.redis nginx下载地址 wget  http://nginx.org/download/nginx-1.8.0.tar.gz lua下载地址 wget http:/ ...

  9. Nginx + Lua + redis (一)(转)

    使用 Lua 脚本语言操作 Redis. 由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua 脚本文件. 要使用 ...

随机推荐

  1. 100W数据,测试复合索引

    复合索引不是那么容易被catch到的. 两个查询条件都是等于的时候,才会被catch到. mysql> select count(*) from tf_user_index where sex ...

  2. Linux 最好是禁用IPV6

    看着不爽, 还容易出事. 编辑文件 – /etc/sysctl.conf $ sudo gedit /etc/sysctl.conf 在文件的最后加入下面的行. # IPv6 disabled net ...

  3. 一个故意消耗内存的java程序MemoryEater

    公司提供的测试服务器是vmware的,号称给我6G, 物理内存事实上是按需分配的. 等到真正拿到6G物理内存,黄花菜都凉了. 看到下面的文章,觉得故意用java程序侵占6G内存, 然后把侵占到内存的释 ...

  4. java反射教程

    什么是反射,为什么它是有用的,以及如何使用它? 1.什么是反射? “反射通常是JVM中运行的程序需要检测和修改运行时程序的行为的一种能力.”这个概念通常与内省(Introspection)混淆.以下是 ...

  5. vue/webpack的一些小技巧

    都知道我比较懒,今天给大家分享的就是如何让自己省事. 一.vue修改打包后的结构(不知道怎么描述合理,看效果图) /config/index.js 默认的: 修改的:(顺手修改了打包后的文件名) 这样 ...

  6. Git创建仓库的方法(github翻译)

    …通过命令行创建一个新的仓库 echo "# GitTest" >> README.md # 船舰一个说明文件,说明内容是 "# GitTest" ...

  7. ES6学习一 JS语言增强篇

    一 背景 JavaScript经过二十来年年的发展,由最初简单的交互脚本语言,发展到今天的富客户端交互,后端服务器处理,跨平台(Native),以及小程序等等的应用.JS的角色越来越重要,处理场景越来 ...

  8. PHP网站自动化配置的实现

    一直都在用yii2做项目,有在用自动化配置,但是没有自己去配置过.中午没事去看了下yii的初始化代码,发现都是php而已! yii2初始化项目代码 所以,我们做项目肯定是可以用php做的,于是我新建了 ...

  9. .net 应用程序 发布上线注意事项

    生产环境发布时,对应的程序目录必须新建当日rar压缩包进行备份生产环境数据库发布时,必须创建存储过程的副本sql用于回滚,操作方式:F7调出对象资源管理器详细信息->选中所有存储过程->编 ...

  10. 总线设备驱动模型---platform篇

    总线设备驱动模型----驱动篇 http://blog.chinaunix.net/uid-27664726-id-3334923.html http://blog.chinaunix.net/uid ...