openresty: nginx worker不同请求之间共享数据
To globally share data among all the requests handled by the same nginx worker process, encapsulate the shared data into a Lua module, use the Lua require builtin to import the module, and then manipulate the shared data in Lua. This works because required Lua modules are loaded only once and all coroutines will share the same copy of the module (both its code and data). Note however that Lua global variables (note, not module-level variables) WILL NOT persist between requests because of the one-coroutine-per-request isolation design.
要在同一个nginx worker进程处理的不同请求间共享数据,可以利用lua的特性,把共享数据封装到lua模块中,然后使用require导入模块。因为require lua模块只会加载一次模块,然后所有的协程共享模块的同一个副本(包括代码和数据)。需要注意的是,lua的全局变量并不会在不同请求之间存在,这是因为每个请求每个协程,相互是隔离的。
openresty提供了lua lru cache作为worker级别的缓存,可以缓存固定数量的key-value,并且由于只能在worker内共享,不会触发锁,效率上有优势。lua lru cache提供的api只有get、set、delete。在具体开发时,可以封装一个module用于操作共享数据,实现不同请求之间数据共享。
local _M = {}
local lrucache = require "resty.lrucache"
local c, err = lrucache.new(200)
if not c then
return error("failed to create the cache: " .. (err or "unknown"))
end
function _M.set(key, value, exptime)
if not exptime then
exptime = 0
end
c:set(key, value, exptime)
end
function _M.get(key)
return c:get(key)
end
function _M.delete(key)
c:delete(key)
end
return _M
如果要实现nginx级别、跨worker共享数据,可以用ngx.shared.dict,或者外部的数据服务,例如memcached、redis。如果数据量不大,并且业务允许,推荐用ngx.shared.dict,效率高。
在nginx.conf中配置lua_shared_dict my_cache 128m;,http请求或tcp请求都可以用。
local function get_from_cache(key)
local cache_ngx = ngx.shared.my_cache
local value = cache_ngx:get(key)
return value
end
local function set_to_cache(key, value, exptime)
if not exptime then
exptime = 0
end
local cache_ngx = ngx.shared.my_cache
local succ, err, forcible = cache_ngx:set(key, value, exptime)
return succ
end
openresty: nginx worker不同请求之间共享数据的更多相关文章
- Android应用程序组件Content Provider在应用程序之间共享数据的原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6967204 在Android系统中,不同的应用 ...
- 多线程(三) 实现线程范围内模块之间共享数据及线程间数据独立(ThreadLocal)
ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.JDK 1.2的版本中就提供java.lang.ThreadLocal,使用这个工具类可以很简洁地编写出优美的多线程程序,Threa ...
- 学习笔记4_ServletContext(重要整个Web应用的动态资源之间共享数据)
ServletContext(重要) 一个项目只有一个ServletContext对象! 我们可以在N多个Servlet中来获取这个唯一的对象,使用它可以给多个Servlet传递数据! 与天地同寿!! ...
- VC++共享数据段实现进程之间共享数据
当我写了一个程序,我希望当这个程序同时运行两遍的时候,两个进程之间能共享一些全局变量,怎么办呢?很简单,使用VC\VC++的共享数据段.; #pragma data_seg()//恢复到正常段继续编程 ...
- 多线程(四) 实现线程范围内模块之间共享数据及线程间数据独立(Map集合)
多个线程访问共享对象和数据的方式 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 2.如果每个线程执行的代码 ...
- Python 进程之间共享数据
最近遇到多进程共享数据的问题,到网上查了有几篇博客写的蛮好的,记录下来方便以后查看. 一.Python multiprocessing 跨进程对象共享 在mp库当中,跨进程对象共享有三种方式,第一种 ...
- 让AngularJS的controllers之间共享数据
如何让controller之间共享数据呢?大致是让不同controller中的变量指向同一个实例. 通过service创建一个存放共享数据的对象. .service("greeting&qu ...
- Python 进程之间共享数据(全局变量)
进程之间共享数据(数值型): import multiprocessing def func(num): num.value=10.78 #子进程改变数值的值,主进程跟着改变 if __name__= ...
- Response ServletContext 中文乱码 Request 编码 请求行 共享数据 转发重定向
Day35 Response 1.1.1 ServletContext概念 u 项目的管理者(上下文对象),服务器启动时,会为每一个项目创建一个对应的ServletContext对象. 1.1.2 ...
随机推荐
- 7.1 基础知识Android消息处理机制
1. Android消息处理机制: Handler, MessageQueue, Looper, Thread 线程概念 : 一个应用程序运行时它的主体被称为进程, 一个进程内部可以有多个线程, 线程 ...
- PHP实现查询两个数组中不同元素的方法
以下实例讲述了PHP实现查询两个数组中不同元素的方法.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- eclipse 更换国内镜像
大家在用eclipse下载插件,或更新插件的时候,有木有觉得速度贼慢,蜗牛似的速度简直让习惯了4G时代的我们抓狂到底,废话不说,先给大家奉献解决办法 网上找到的国内镜像总结: 1.企业贡献: 搜狐开源 ...
- 结合Wireshark捕获分组深入理解TCP/IP协议之IP协议
摘要: 本文简单介绍了网络层理论知识,详细讲解了IP数据报各个字段,并从Wireshark俘获分组中选取IP数据报进行分析,也阐述了分组和分片的区别. 一.IPv4数据报 网络层是 ...
- 3D 应用程序性能
原文:3D 应用程序性能 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/74595999 3 ...
- [Angular2 Form] Model Driven Form Custom Validator
In this tutorial we are going to learn how simple it is to create custom form field driven validator ...
- 使用 Google Guava 美化你的 Java 代码:1~4 【转】
文章转载自:http://my.oschina.net/leejun2005/blog/172328 1.使用Google Collections,Guava,static imports编写漂亮代码 ...
- Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析
原文地址: https://blog.csdn.net/yx0628/article/details/80873774 一个简单的Spring定时任务的 demo,全部代码见下载地址:https:// ...
- Swift 语言概览 -自己在Xcode6 动手写2-tableView
import UIKit class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource { va ...
- php资源集
php资源集 一.php资源网站 1.php中文网(js特效,模板,软件工具下载,课程) www.php.cn js特效下载_js特效代码_js特效大全-php中文网免费下载站http://www.p ...