lua入门demo(HelloWorld+redis读取)
1. lua入门demo
1.1. 入门之Hello World!!
- 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上
- openresty是nginx+lua的各种模块,所以直接docker安装openresty
- 修改nginx.conf配置文件,在http模块中加上
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
- http内的server模块上,在加个
location /lua-file{
default_type 'text/html';
content_by_lua_file /usr/local/openresty/demo/lua-file.lua;
}
这样我可以在指定目录开始编写lua脚本了,在写完脚本后,nginx -s reload 一下就可以通过ip/lua-file访问lua脚本了
我在lua-file.lua内先写上 ngx.say('Hello world!!'),然后reload一下后
访问结果:

1.2. 访问redis
local function close_redis(red)
if not red then
return
end
local pool_max_idle_time = 10000
local pool_size = 100
local ok,err = red:set_keepalive(pool_max_idle_time,pool_size)
if not ok then
ngx.say("set keepalive error:" ,err)
end
end
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000)
local ok,err = red:connect("47.96.64.100",6379)
if not ok then
ngx.say("connect to redis error: ",err)
return close_redis(red)
end
local count,err = red:get_reused_times()
if 0 == count then
ok,err = red:auth("123456")
if not ok then
ngx.say("auth fail")
return
end
elseif err then
ngx.say("failed to get reused times: ",err)
return
end
ngx.say(red:get("dog"))
close_redis(red)
- 当然,我事先redis存放了key为dog的值
- 访问浏览器结果

1.3. 总结
- 通过简单的hello world实例和redis读取,我们基本了解了lua的用法和功能,lua的语法和js类似,部分自己的特色,这里我就抛砖引玉一下了
lua入门demo(HelloWorld+redis读取)的更多相关文章
- lua模块demo(redis,http,mysql,cjson,本地缓存)
1. lua模块demo(redis,http,mysql,cjson,本地缓存) 1.1. 配置 在nginx.conf中设置lua_shared_dict my_cache 128m; 开启ngi ...
- Dubbo(五) Dubbo入门demo——helloworld
前言 前面我已经介绍了dubbo的一些基本工具和知识,让大家简单的了解了下RPC框架和Dubbo.接下来就是重点了,Dubbo的helloworld项目. 一.搭建项目 首先我们新建三个maven项目 ...
- Redis快速入门:初识Redis
[IT168 专稿]在之前的文章中介绍了<Redis快速入门:选择Key-Value Store>,今天给大家介绍Redis的入门知识.Redis是一个开源的使用ANSI C语言编写.支持 ...
- apollo入门demo实战(二)
1. apollo入门demo实战(二) 1.1. 下载demo 从下列地址下载官方脚本和官方代码 https://github.com/nobodyiam/apollo-build-scripts ...
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
C#中缓存的使用 缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可: <%@ Outp ...
- ORM----hibernate入门Demo(无敌详细版)
一.Hibernate(开放源代码的对象关系映射框架)简介: Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全 ...
- storm入门demo
一.storm入门demo的介绍 storm的入门helloworld有2种方式,一种是本地的,另一种是远程. 本地实现: 本地写好demo之后,不用搭建storm集群,下载storm的相关jar包即 ...
- Mybatis入门Demo(单表的增删改查)
1.Mybatis 什么是Mybatis: mybatis是一个持久层框架,用java编写的 它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动.创建连接等繁杂过程 ...
- windows下使用redis,Redis入门使用,Redis基础命令
windows下使用redis,Redis入门使用,Redis基础命令 >>>>>>>>>>>>>>>> ...
随机推荐
- Layui 写一个简单的后台页面
参考如下: 1.layui 官方文档 http://www.layui.com/doc/ 2.https://blog.csdn.net/huyanliang/article/details/7796 ...
- MySQL数据库开发常见问题及几点优化!
从一下三个方面考虑: 库表设计 慢 SQL 问题 误操作.程序 bug 时怎么办 一.库表设计 1.1.引擎选择 在 MySQL5.1 中,引入了新的插件式存储引擎体系结构,允许将存储引擎加载到正在运 ...
- linux ssh反向代理
参考:https://segmentfault.com/a/1190000002718360 内外运行:sshpass -p 123456 ssh -fNR 5000:localhost:22 ser ...
- openstack系列文章(2)dashboard
玩转dashboard之前,考虑一些事情:(1)安全问题:网络访问策略(2)镜像的密码管理:windows或者linux,root或者administrator密码怎么管理(3)怎样创建自己的镜像:w ...
- VNF网络性能提升解决方案及实践
VNF网络性能提升解决方案及实践 2016年7月 作者: 王智民 贡献者: 创建时间: 2016-7-20 稳定程度: 初稿 修改历史 版本 日期 修订人 说明 1.0 20 ...
- weblogic安装升级配置
本次操作是主要围绕如何搭建weblogic服务器升级weblogic软件及配置服务,总共有三大步骤,可划分为六个小步骤: 选取已有环境,准备weblogic压缩包,java包等 准备操作系统环境用户目 ...
- P4178 Tree
最简单的点分治 淀粉质的思想: “分而治之”,缩小问题规模,合并求解: #include<cstdio> #include<cmath> #include<cstring ...
- Flink 报错 "Could not find a suitable table factory for 'org.apache.flink.table.factories.StreamTableSourceFactory' in the classpath"
先上代码: table = tablexx.select('*).tablexx.groupBy('x).select('x, xx.count ) tableEnvironment // decla ...
- temp-重庆银行
重庆现场部署环境(开发环境)说明 : 数据库 172.16.69.95:1521:orcl ilink/ilink123 sys/manager(dba权限) 1, Linux ...
- 神经网络参数与TensorFlow变量
在TensorFlow中变量的作用是保存和更新神经网络中的参数,需要给变量指定初始值,如下声明一个2x3矩阵变量 weights =tf.Variable(tf.random_normal([2,3] ...