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用于操作共享数据,实现不同请求之间数据共享。

  1. local _M = {}
  2. local lrucache = require "resty.lrucache"
  3. local c, err = lrucache.new(200)
  4. if not c then
  5. return error("failed to create the cache: " .. (err or "unknown"))
  6. end
  7. function _M.set(key, value, exptime)
  8. if not exptime then
  9. exptime = 0
  10. end
  11. c:set(key, value, exptime)
  12. end
  13. function _M.get(key)
  14. return c:get(key)
  15. end
  16. function _M.delete(key)
  17. c:delete(key)
  18. end
  19. return _M

如果要实现nginx级别、跨worker共享数据,可以用ngx.shared.dict,或者外部的数据服务,例如memcached、redis。如果数据量不大,并且业务允许,推荐用ngx.shared.dict,效率高。

nginx.conf中配置lua_shared_dict my_cache 128m;,http请求或tcp请求都可以用。

  1. local function get_from_cache(key)
  2. local cache_ngx = ngx.shared.my_cache
  3. local value = cache_ngx:get(key)
  4. return value
  5. end
  6. local function set_to_cache(key, value, exptime)
  7. if not exptime then
  8. exptime = 0
  9. end
  10. local cache_ngx = ngx.shared.my_cache
  11. local succ, err, forcible = cache_ngx:set(key, value, exptime)
  12. return succ
  13. 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. (转)Windows Server 2008 R2 域控制器部署指南

    转自:https://technet.microsoft.com/zh-cn/cloud/gg462955.aspx 一.域控制器安装步骤: 1.装 Windows Server 2008 R2并配置 ...

  2. 你说你会C++? —— 智能指针

    智能指针的设计初衷是:      C++中没有提供自己主动回收内存的机制,每次new对象之后都须要手动delete.稍不注意就memory leak. 智能指针能够解决上面遇到的问题. C++中常见的 ...

  3. goland 2018.2 激活

    感谢 http://blog.sina.com.cn/s/blog_1885d23df0102ydjc.html http://www.3322.cc/soft/38102.html 下载   htt ...

  4. JavaEE 技术选型建议,server配置,部署策略

    基础设施环境 # 总体採用 centos6.5 + nginx + tomcat7.0 负载均衡:nginx 配置,使用 nginx 作为负载均衡.权重配置. 在web层做到水平扩展. 以及配置日志格 ...

  5. ios开发之核心动画四:核心动画-Core Animation--CABasicAnimation基础核心动画

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  6. mount新磁盘

    fdisk -l  mkfs.xfs /dev/xvdb  mkdir /data mount /dev/xvdb /data df -h vi /etc/fstab /dev/xvdb /data ...

  7. 【u015】兽径管理

    [问题描述] 约翰农场的牛群希望能够在 N 个(1<=N<=200)草地之间任意移动.草地的编号由 1到N.草地之间有树林隔开.牛群希望能够选择草地间的路径,使牛群能够从任一 片草地移动到 ...

  8. [Ramda] Difference between R.converge and R.useWith

    So what is Point-free function. Take a look this example: const getUpdatedPerson = (person) => R. ...

  9. 【codeforces 768A】Oath of the Night's Watch

    [题目链接]:http://codeforces.com/contest/768/problem/A [题意] 让你统计这样的数字x的个数; x要满足有严格比它小和严格比它大的数字; [题解] 排个序 ...

  10. BZOJ 4264 小c找朋友 - hash

    传送门 题目大意: 给定一张无向图,求满足以下条件的点对 (x,y) 数目:对任意点 z (z!=x,y),边 (x,z) 和 (y,z) 同时存在或同时不存在. 题目分析: 首先要分析的出如果xy满 ...