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不同请求之间共享数据的更多相关文章

  1. Android应用程序组件Content Provider在应用程序之间共享数据的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6967204 在Android系统中,不同的应用 ...

  2. 多线程(三) 实现线程范围内模块之间共享数据及线程间数据独立(ThreadLocal)

    ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.JDK 1.2的版本中就提供java.lang.ThreadLocal,使用这个工具类可以很简洁地编写出优美的多线程程序,Threa ...

  3. 学习笔记4_ServletContext(重要整个Web应用的动态资源之间共享数据)

    ServletContext(重要) 一个项目只有一个ServletContext对象! 我们可以在N多个Servlet中来获取这个唯一的对象,使用它可以给多个Servlet传递数据! 与天地同寿!! ...

  4. VC++共享数据段实现进程之间共享数据

    当我写了一个程序,我希望当这个程序同时运行两遍的时候,两个进程之间能共享一些全局变量,怎么办呢?很简单,使用VC\VC++的共享数据段.; #pragma data_seg()//恢复到正常段继续编程 ...

  5. 多线程(四) 实现线程范围内模块之间共享数据及线程间数据独立(Map集合)

    多个线程访问共享对象和数据的方式 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 2.如果每个线程执行的代码 ...

  6. Python 进程之间共享数据

    最近遇到多进程共享数据的问题,到网上查了有几篇博客写的蛮好的,记录下来方便以后查看. 一.Python multiprocessing 跨进程对象共享  在mp库当中,跨进程对象共享有三种方式,第一种 ...

  7. 让AngularJS的controllers之间共享数据

    如何让controller之间共享数据呢?大致是让不同controller中的变量指向同一个实例. 通过service创建一个存放共享数据的对象. .service("greeting&qu ...

  8. Python 进程之间共享数据(全局变量)

    进程之间共享数据(数值型): import multiprocessing def func(num): num.value=10.78 #子进程改变数值的值,主进程跟着改变 if __name__= ...

  9. Response ServletContext 中文乱码 Request 编码 请求行 共享数据 转发重定向

    Day35  Response 1.1.1 ServletContext概念 u 项目的管理者(上下文对象),服务器启动时,会为每一个项目创建一个对应的ServletContext对象. 1.1.2  ...

随机推荐

  1. PostgreSQL 序列

    PostgreSQL 中的序列是一个数据库对象,本质上是一个自增器.因此,序列在其他同类型数据库软件中以 autoincrment 值的形式存在.在一张表需要非随机,唯一标实符的场景下,Sequenc ...

  2. get_mysql_conn_info.py

    #!/usr/bin/env python#-*- encoding: utf8 -*- import xlrd """此模块作用:从excel文件获取数据库连接信息,第 ...

  3. P2P网贷第三方托管模式存在5大缺陷,托管机构才是最大赢家

    1.注册开户需要2次,用户体验很差劲儿.   理财人和借款人,首先在平台注册,然后还要在第三方托管账户注册.   很多类似的地方,用户体验非常差劲.   比如,密码4个.   平台:登录密码.交易密码 ...

  4. MFC注册窗口类以及FindWindow按窗口类名查询(避免用#32770获取窗口句柄)

    呵呵,最近在研究SendMessage函数,其中需要用到m_hWnd,之后延伸着又尝试获得窗口的句柄,于是遇到了FindWindow函数,原型如下: HWND FindWindow ( LPCSTR ...

  5. 关于spyder的一些用法

    目前还不会用spyder,感觉不习惯,也没怎么用MATLAB 能记住几点算几点吧 1,双击程序左侧栏,加断点 1,按住Ctrl,点击函数,进入函数

  6. 【15.07%】【codeforces 625A】Guest From the Past

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. [CSS] Conditionally Apply Styles Using Feature Queries @supports

    While browsers do a great job of ignoring styles they don’t understand, it can be useful to provide ...

  8. maven pom.xml基本使用方法

    pom.xml文件是Maven进行工作的主要配置文件.在这个文件里我们能够配置Maven项目的groupId.artifactId和version等Maven项目必须的元素:能够配置Maven项目须要 ...

  9. php-post模拟登录,同步登录(摘自网络)

    这也是个老生常谈的话题了,上午花了点时间把这个问题整理了一下. 一般来说用PHP来模拟post提交数据有三种方法,file_get_contents.curl和socket. 写了个公用函数,专门用来 ...

  10. C#验证手机号

    using System.Text.RegularExpressions; private bool IsMobile(string phoneNo) { return Regex.IsMatch(p ...