Skip to content

Commit 9bd0cd1

Browse files
committed
Lua的相对路径和绝对路径的应用以及Redis的二次封装库的应用
1 parent 3708c4f commit 9bd0cd1

File tree

3 files changed

+98
-19
lines changed

3 files changed

+98
-19
lines changed

.idea/workspace.xml

+37-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Openresty/lua-common-package/lua-require.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
![Markdown](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Images/nginx-hls-locations.png)
22
### Lua require 相对路径
33
+ [Lua require 相对路径 博客园详解](http://www.cnblogs.com/smallboat/p/5552407.html)
4-
+ 亲自试验总结
4+
+ 相对路径总结
5+
+ **当前目录**`/opt/openresty/nginx/conf/Lua`
6+
+ 注意:如果在当前没有了,以下的代码运行是没有问题的
7+
+ 就是所有的lua脚本代码全部写在Lua文件夹下面去
8+
+ 拼接路径:`package.path = package.path ..';..\\?.lua';`
9+
+ require 直接这样应用就是`Lua.functions`
510
+ 目录结构
611
```
712
root@tinywan:/opt/openresty/nginx/conf/Lua# pwd
@@ -54,4 +59,48 @@
5459
root@tinywan:/opt/openresty/nginx/conf/Lua# curl '127.0.0.1:80/api'
5560
Hello
5661
102
57-
```
62+
```
63+
+ 绝对路径总结
64+
+ 目录结果:`/opt/openresty/nginx/lua/resty`
65+
+ 包路径:`package.path = package.path ..'/opt/openresty/nginx/lua/resty/?.lua;/opt/openresty/lualib/?.lua';`
66+
+ 加载绝对路径的Redis的二次封装库,redis_iresty.lua文件:
67+
```
68+
package.path = package.path ..'/opt/openresty/nginx/lua/resty/?.lua;/opt/openresty/lualib/?.lua';
69+
local redis_c = require "resty.redis"
70+
local ok, new_tab = pcall(require, "table.new")
71+
if not ok or type(new_tab) ~= "function" then
72+
new_tab = function (narr, nrec) return {} end
73+
end
74+
```
75+
+ Redis的二次封装库的应用,test_redis2.lua文件:
76+
```
77+
package.path = '/opt/openresty/nginx/lua/?.lua;';
78+
local redis = require("resty.redis_iresty")
79+
local red = redis:new()
80+
local ok, err = red:set("Redis1999", "Redis is an animal 1999")
81+
if not ok then
82+
ngx.say("failed to set dog: ", err)
83+
return
84+
end
85+
```
86+
+ nginx.conf 配置文件(注意以下的目录路径和上面的不一样)
87+
```
88+
location /api {
89+
lua_code_cache off;
90+
content_by_lua_file /opt/openresty/nginx/lua/test_redis2.lua;
91+
}
92+
```
93+
+ CURL 请求结果
94+
```
95+
root@tinywan:/opt/openresty/nginx/conf/Lua# curl '127.0.0.1:80/api'
96+
Hello
97+
102
98+
```
99+
+ Redis 数据库结果
100+
```
101+
127.0.0.1:6379> keys *
102+
1) "dog"
103+
2) "Redis2222222222"
104+
3) "Redis1999"
105+
```
106+

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,18 @@
272272
```
273273
.
274274
├── conf
275-
│ ├── nginx.conf
275+
│ ├── nginx.conf -- Nginx 配置文件
276276
├── logs
277-
│ ├── error.log
277+
│ ├── error.log -- Nginx 错误日子
278278
│ └── nginx.pid
279279
├── lua
280-
│ ├── access_check.lua
281-
│ ├── addition.lua
282-
│ ├── subtraction.lua
283-
│ ├── multiplication.lua
284-
── division.lua
285-
│ └── comm
286-
│ └── param.lua
280+
│ ├── access_check.lua -- 权限验证文件
281+
│ ├── business_redis.lua -- 业务 Redis 处理文件
282+
│ ├── business_redis.lua -- 业务 Redis 处理文件
283+
│ ├── ...
284+
── resty -- 存放Lua 的所有公共、封装好的库
285+
└── redis_iresty.lua -- Redis 接口的二次封装
286+
│ └── param.lua -- 参数过滤库
287287
└── sbin
288288
└── nginx
289289
```
@@ -327,7 +327,7 @@
327327
The man name is Tinywan
328328
The man name is Phalcon
329329
```
330-
+ [Lua require 相对路径(一个文件引入另外一个文件的Function),已经解决](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-common-package/lua-require.md)
330+
+ [Lua require 绝对和相对路径问题(一个文件引入另外一个文件的Function),已经解决](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-common-package/lua-require.md)
331331
+ lua-resty-redis 扩展
332332
+ 代码引入:`lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;";`
333333
+ **Lua脚本实现一个CDN的反向代理功能(智能查找CDN节点)(测试成功,可上线)**

0 commit comments

Comments
 (0)