ngx_lua灵活定制fastcgi_cache缓存key
Web开发常见的几种缓存
常用缓存(memcached和redis)
Nginx的缓存(标准模块缓存: proxy_cache和fastcgi_cache / 第三方模块做缓存: ngx_lua)
CDN缓存
浏览器缓存(Cache-Control和LocalStorage)
proxy_cache和fastcgi_cacheproxy_cache和fastcgi_cache都为Nginx的内置缓存,proxy_cache主要用于反向代理时,对后端内容源服务器进行缓存,fastcgi_cache主要用于对FastCGI的动态程序进行缓存。两者相关配置类似,以下为fastcgi_cache举例
原理 针对fastcgi(如:php-fpm)返回的内容缓存为静态文件(文件名是用Md5算法对Key进行哈希后所得,而Key可使用fastcgi_cache的相关指令来进行控制),在用户浏览时,无需重复请求后端fastcgi,而直接返回缓存的内容,减少了后端的语言解析以及数据库连接的消耗。
数据流程图
指令注释nginx的http作用域:fastcgi_cache_path /home/w ...
lua基本语法
注释
1234--[[多行注释--]]
运行方式
交互式运行 命令行下 lua进入交互模式
命令行运行 lua +
-e + "代码块" 直接运行
-l 加载文件
-i 进入交互模式
赋值语句
12345678-- 交换两个变量值 a, b = b, a-- 多个变量赋值 a,b,c = 1,2,3 --按照等号后面的数值位置赋值,如果缺少或者多与参数则赋值为nil-- 按照key赋值 name.key = value-- 可以赋值为函数 a,b = f()
全局变量和局部变量
全局变量
不需要特殊声明 创建或者赋值,只要不为nil都可以
局部变量
关键字local 尽量避免使用全局变量,优点:避免命名冲突和提高访问变量的速度
语句体
条件语句
12345678910111213141516171819--第一种if 判断条件 thenend;-- 第二种if 判断条件 then 执行语句else 执行语句end;-- 第三种if 判断条件 then 执行语句else ...
leetcode 150 evaluate reverse polish notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Note:
Division between two integers should truncate toward zero.
The given RPN expression is always valid. That means the expression would always evaluate to a result and there won’t be any divide by zero operation.
Example 1:
123Input: ["2", "1", "+", "3", "*"]Output: 9Explanation: ((2 + 1) * 3) ...
leetcode.150 evaluate reverse polish notation
题意
计算逆波兰法表示的算式解法
遇到数字存入栈,遇到符号则出栈两个数字,并将结果入栈
class Solution {
Stack<Integer> integers = new Stack<>();
public int evalRPN(String[] tokens) {
for(String token: tokens) {
if ("+".equals(token)) {
int n2 = integers.pop(), n1 = integers.pop();
integers.push(n1 + n2);
} else if ("-".equals(token)) {
int n2 = integers.pop(), n1 = integers.pop();
integers.push(n1 - n2);
} else if ("*".equals(token)) {
int n2 = integers.pop ...
lua string hash 算法
我在前一篇文章介绍过下面这 3 个字符串拥有相同的 hash,会导致 Hash Dos 问题:
"0000000000000000000000000000000000"
"f0l0l0w0m0e0n0t0w0i0t0t0e0r0?0:0)0"
"x0x0x0x0x0x0x0x0x0x0x0x0x0x0x0x0x0"
但是 Lua 并没有将自己的 string hash 算法暴露出来,那应该怎么验证呢?其实翻看 Lua 5.1.4 源码,lstring.c 中关于 string hash 是这么定义的:
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
GCObject *o;
unsigned int h = cast(unsigned int, l); /* seed */
size_t step = (l>>5)+1; /* if string is too long, don't hash all its ...
lua学习笔记:基本语法
JVM学习笔记11:synchronized的实现
编程语言
do something valuable
真诚地重复做不变、简单的事情
evaluate
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples:
[“2”, “1”, “+”, “3”, ““] -> ((2 + 1) 3) -> 9 [“4”, “13”, “5”, “/“, “+”] -> (4 + (13 / 5)) -> 6
javajava的stack中,s.pop()不仅会弹出栈顶元素,还会返回栈顶元素。
String转intInteger.parseIntInteger.valueof()返回的是Integer的对象。Integer.valueof().intValue();返回的也是一个int的值。
123456789101112131415161718192021222324252627282930313233343536373839404 ...
LUA 进阶
环境1lua -e "for k,v in pairs(_G) do print(k,v) end" | sort
lua全局环境变量保存在_G的table中,内容如下:
123456789101112131415161718192021222324252627282930313233343536373839assert function: 0collectgarbage function: 0x99f5bd0coroutine table: 0x99f6718debug table: 0x99f97d0dofile function: 0x99f5c88error function: 0x99f5cc0gcinfo function: 0x99f5c10getfenv function: 0x99f5c48getmetatable function: 0x99f5de0_G table: 0x99f5450io table: 0x99f79e8ipairs function: 0x99f59a0loadfile ...
lua学习笔记:高级概念
JVM学习笔记11:synchronized的实现
编程语言


