Skip to content

Commit

Permalink
Lua的相对路径和绝对路径的应用以及Redis的二次封装库的应用
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Apr 14, 2017
1 parent 3708c4f commit 9bd0cd1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 19 deletions.
44 changes: 37 additions & 7 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 51 additions & 2 deletions Openresty/lua-common-package/lua-require.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
![Markdown](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Images/nginx-hls-locations.png)
### Lua require 相对路径
+ [Lua require 相对路径 博客园详解](http://www.cnblogs.com/smallboat/p/5552407.html)
+ 亲自试验总结
+ 相对路径总结
+ **当前目录**`/opt/openresty/nginx/conf/Lua`
+ 注意:如果在当前没有了,以下的代码运行是没有问题的
+ 就是所有的lua脚本代码全部写在Lua文件夹下面去
+ 拼接路径:`package.path = package.path ..';..\\?.lua';`
+ require 直接这样应用就是`Lua.functions`
+ 目录结构
```
root@tinywan:/opt/openresty/nginx/conf/Lua# pwd
Expand Down Expand Up @@ -54,4 +59,48 @@
root@tinywan:/opt/openresty/nginx/conf/Lua# curl '127.0.0.1:80/api'
Hello
102
```
```
+ 绝对路径总结
+ 目录结果:`/opt/openresty/nginx/lua/resty`
+ 包路径:`package.path = package.path ..'/opt/openresty/nginx/lua/resty/?.lua;/opt/openresty/lualib/?.lua';`
+ 加载绝对路径的Redis的二次封装库,redis_iresty.lua文件:
```
package.path = package.path ..'/opt/openresty/nginx/lua/resty/?.lua;/opt/openresty/lualib/?.lua';
local redis_c = require "resty.redis"
local ok, new_tab = pcall(require, "table.new")
if not ok or type(new_tab) ~= "function" then
new_tab = function (narr, nrec) return {} end
end
```
+ Redis的二次封装库的应用,test_redis2.lua文件:
```
package.path = '/opt/openresty/nginx/lua/?.lua;';
local redis = require("resty.redis_iresty")
local red = redis:new()
local ok, err = red:set("Redis1999", "Redis is an animal 1999")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
```
+ nginx.conf 配置文件(注意以下的目录路径和上面的不一样)
```
location /api {
lua_code_cache off;
content_by_lua_file /opt/openresty/nginx/lua/test_redis2.lua;
}
```
+ CURL 请求结果
```
root@tinywan:/opt/openresty/nginx/conf/Lua# curl '127.0.0.1:80/api'
Hello
102
```
+ Redis 数据库结果
```
127.0.0.1:6379> keys *
1) "dog"
2) "Redis2222222222"
3) "Redis1999"
```
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,18 @@
```
.
├── conf
│ ├── nginx.conf
│ ├── nginx.conf -- Nginx 配置文件
├── logs
│ ├── error.log
│ ├── error.log -- Nginx 错误日子
│ └── nginx.pid
├── lua
│ ├── access_check.lua
│ ├── addition.lua
│ ├── subtraction.lua
│ ├── multiplication.lua
── division.lua
│ └── comm
│ └── param.lua
│ ├── access_check.lua -- 权限验证文件
│ ├── business_redis.lua -- 业务 Redis 处理文件
│ ├── business_redis.lua -- 业务 Redis 处理文件
│ ├── ...
── resty -- 存放Lua 的所有公共、封装好的库
└── redis_iresty.lua -- Redis 接口的二次封装
│ └── param.lua -- 参数过滤库
└── sbin
└── nginx
```
Expand Down Expand Up @@ -327,7 +327,7 @@
The man name is Tinywan
The man name is Phalcon
```
+ [Lua require 相对路径(一个文件引入另外一个文件的Function),已经解决](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-common-package/lua-require.md)
+ [Lua require 绝对和相对路径问题(一个文件引入另外一个文件的Function),已经解决](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-common-package/lua-require.md)
+ lua-resty-redis 扩展
+ 代码引入:`lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;";`
+ **Lua脚本实现一个CDN的反向代理功能(智能查找CDN节点)(测试成功,可上线)**
Expand Down

0 comments on commit 9bd0cd1

Please sign in to comment.