当前位置:   article > 正文

openresty-redis操作_openresty操作redis

openresty操作redis

我们这里将配置文件写在一个文件夹下

 redis_factory.lua:

  1. local redis_factory = function(h)
  2. local h = h
  3. h.redis = require('resty.redis')
  4. h.cosocket_pool = {max_idel = 10000, size = 10000}
  5. h.commands = {
  6. "append", "auth", "bgrewriteaof",
  7. "bgsave", "bitcount", "bitop",
  8. "blpop", "brpop",
  9. "brpoplpush", "client", "config",
  10. "dbsize",
  11. "debug", "decr", "decrby",
  12. "del", "discard", "dump",
  13. "echo",
  14. "eval", "exec", "exists",
  15. "expire", "expireat", "flushall",
  16. "flushdb", "get", "getbit",
  17. "getrange", "getset", "hdel",
  18. "hexists", "hget", "hgetall",
  19. "hincrby", "hincrbyfloat", "hkeys",
  20. "hlen",
  21. "hmget", "hmset", "hscan",
  22. "hset",
  23. "hsetnx", "hvals", "incr",
  24. "incrby", "incrbyfloat", "info",
  25. "keys",
  26. "lastsave", "lindex", "linsert",
  27. "llen", "lpop", "lpush",
  28. "lpushx", "lrange", "lrem",
  29. "lset", "ltrim", "mget",
  30. "migrate",
  31. "monitor", "move", "mset",
  32. "msetnx", "multi", "object",
  33. "persist", "pexpire", "pexpireat",
  34. "ping", "psetex", "psubscribe",
  35. "pttl",
  36. "publish", "punsubscribe", "pubsub",
  37. "quit",
  38. "randomkey", "rename", "renamenx",
  39. "restore",
  40. "rpop", "rpoplpush", "rpush",
  41. "rpushx", "sadd", "save",
  42. "scan", "scard", "script",
  43. "sdiff", "sdiffstore",
  44. "select", "set", "setbit",
  45. "setex", "setnx", "setrange",
  46. "shutdown", "sinter", "sinterstore",
  47. "sismember", "slaveof", "slowlog",
  48. "smembers", "smove", "sort",
  49. "spop", "srandmember", "srem",
  50. "sscan",
  51. "strlen", "subscribe", "sunion",
  52. "sunionstore", "sync", "time",
  53. "ttl",
  54. "type", "unsubscribe", "unwatch",
  55. "watch", "zadd", "zcard",
  56. "zcount", "zincrby", "zinterstore",
  57. "zrange", "zrangebyscore", "zrank",
  58. "zrem", "zremrangebyrank", "zremrangebyscore",
  59. "zrevrange", "zrevrangebyscore", "zrevrank",
  60. "zscan",
  61. "zscore", "zunionstore", "evalsha",
  62. -- resty redis private command
  63. "set_keepalive", "init_pipeline", "commit_pipeline",
  64. "array_to_hash", "add_commands", "get_reused_times",
  65. }
  66. -- connect
  67. -- @param table connect_info, e.g { host="127.0.0.1", port=6379, pass="", timeout=1000, database=0}
  68. -- @return boolean result
  69. -- @return userdata redis_instance
  70. h.connect = function(connect_info)
  71. local redis_instance = h.redis:new()
  72. redis_instance:set_timeout(connect_info.timeout)
  73. if not redis_instance:connect(connect_info.host, connect_info.port) then
  74. return false, nil
  75. end
  76. if connect_info.pass ~= '' then
  77. redis_instance:auth(connect_info.pass)
  78. end
  79. redis_instance:select(connect_info.database)
  80. return true, redis_instance
  81. end
  82. -- spawn_client
  83. -- @param table h, include config info
  84. -- @param string name, redis config name
  85. -- @return table redis_client
  86. h.spawn_client = function(h, name)
  87. local self = {}
  88. self.name = ""
  89. self.redis_instance = nil
  90. self.connect = nil
  91. self.connect_info = {
  92. host = "", port = 0, pass = "",
  93. timeout = 0, database = 0
  94. }
  95. -- construct
  96. self.construct = function(_, h, name)
  97. -- set info
  98. self.name = name
  99. self.connect = h.connect
  100. self.connect_info = h[name]
  101. -- gen redis proxy client
  102. for _, v in pairs(h.commands) do
  103. self[v] = function(self, ...)
  104. -- instance test and reconnect
  105. if (type(self.redis_instance) == 'userdata: NULL' or type(self.redis_instance) == 'nil') then
  106. local ok
  107. ok, self.redis_instance = self.connect(self.connect_info)
  108. if not ok then return false end
  109. end
  110. -- get data
  111. local vas = { ... }
  112. return self.redis_instance[v](self.redis_instance, ...)
  113. end
  114. end
  115. return true
  116. end
  117. -- do construct
  118. self:construct(h, name)
  119. return self
  120. end
  121. local self = {}
  122. self.pool = {} -- redis client name pool
  123. -- construct
  124. -- you can put your own construct code here.
  125. self.construct = function()
  126. return
  127. end
  128. -- spawn
  129. -- @param string name, redis database serial name
  130. -- @return boolean result
  131. -- @return userdata redis
  132. self.spawn = function(_, name)
  133. if self.pool[name] == nil then
  134. ngx.ctx[name] = h.spawn_client(h, name)
  135. self.pool[name] = true
  136. return true, ngx.ctx[name]
  137. else
  138. return true, ngx.ctx[name]
  139. end
  140. end
  141. -- destruct
  142. -- @return boolean allok, set_keepalive result
  143. self.destruct = function()
  144. local allok = true
  145. for name, _ in pairs(self.pool) do
  146. local ok, msg = ngx.ctx[name].redis_instance:set_keepalive(
  147. h.cosocket_pool.max_idel, h.cosocket_pool.size
  148. )
  149. if not ok then allok = false end
  150. end
  151. return allok
  152. end
  153. -- do construct
  154. self.construct()
  155. return self
  156. end
  157. return redis_factory

config_constant.lua:

  1. config = {}
  2. config.redisConfig = {
  3. redis_a = { -- your connection name
  4. --ip
  5. host = 'xxxxx',
  6. --端口
  7. port = 6379,
  8. --密码
  9. pass = 'xxxxx',
  10. --超时时间,如果是测试环境debug的话,这个值可以给长一点;如果是正式环境,可以设置为200
  11. timeout = 120000,
  12. --redis的库
  13. database = 0,
  14. },
  15. -- redis_b = {
  16. -- -- host = '127.0.0.1',
  17. -- -- port = 6379,
  18. -- -- pass = '',
  19. -- -- timeout = 200,
  20. -- -- database = 0,
  21. -- -- },
  22. }
  23. return config

redistest.lua:

  1. --平台公共的配置文件常量
  2. local config = require "testcode.config_constant"
  3. --redis连接池工厂
  4. local redis_factory = require('testcode.redis_factory')(config.redisConfig) -- import config when construct
  5. --获取redis的连接实例
  6. local ok, redis_a = redis_factory:spawn('redis_a')
  7. --用于接收前端数据的对象
  8. local args=nil
  9. --获取前端的请求方式 并获取传递的参数
  10. local request_method = ngx.var.request_method
  11. --判断是get请求还是post请求并分别拿出相应的数据
  12. if"GET" == request_method then
  13. args = ngx.req.get_uri_args()
  14. elseif "POST" == request_method then
  15. ngx.req.read_body()
  16. args = ngx.req.get_post_args()
  17. --兼容请求使用post请求,但是传参以get方式传造成的无法获取到数据的bug
  18. if (args == nil or args.data == null) then
  19. args = ngx.req.get_uri_args()
  20. end
  21. end
  22. --获取前端传递的key
  23. local key = args.key
  24. --在redis中获取key对应的值
  25. local va = redis_a:get(key)
  26. --响应前端
  27. ngx.say('{"key":"'..key..'","va":"'..va..'"}')

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/991848
推荐阅读
相关标签
  

闽ICP备14008679号