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基础命令 >>>>>>>>>>>>>>>> ...
随机推荐
- python ftp 传输文件
# -*- coding: utf-8 -*- # 本地bytes 数据上报服务器同时创建文件from ftplib import FTP import time, _io from constant ...
- 生产与学术之Pytorch模型导出为安卓Apk尝试记录
生产与学术 写于 2019-01-08 的旧文, 当时是针对一个比赛的探索. 觉得可能对其他人有用, 就放出来分享一下 生产与学术, 真实的对立... 这是我这两天对pytorch深度学习->a ...
- TCP/IP数据加密传输及CA简述
TCP/IP跨主机之间的通信数据封装发送的都是明文数据,现代通讯中会有安全问题. 三个安全问题 如:A发送消息给B的三个安全问题机密性:明文传输如:ftp,http,smtp,telnet等完整性:数 ...
- ABP 设置默认为中文
把资源文件 的zh-cn去掉就可以,改成默认文件
- sql语句性能优化
需要的准备知识 1最左前缀匹配 mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹配, 对于where条件 a = 1 and b> 2 and c ...
- list常用方法
1.切片: ①.顾头不顾尾,从头开始取,但不包括最后一个. ②.从左向右数为正,从零开始,从右开始为负,从-1开始 如: names=['1','2','3'] ames[-1]与names[2]效果 ...
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project taotao-manager-pojo: Compilation failure
运行maven项目时报错 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compi ...
- 在Linux下部署mysql时,使用group by碰到的问题
mysql使用group by 的时候报错,错误信息如下: 1055:ER_WRONG_FIELD_WITH_GROUP: Expression #2 of SELECT list is not in ...
- Stanford CS20学习笔记
Lecture Note 2 Tensorboard P3 Data Structures P4 Math Operations P6 Data Types P7 tf native &&am ...
- OpenGL ES中MRT应用
Demo涵盖了OpenGL ES 3.0 的一系列新特性: 1.VAO和VBO 2.帧缓冲对象 3.MRT 效果: 代码: //yf's version #define STB_IMAGE_IMPLE ...