一、背景
在使用redis的过程中,发现有些时候需要原子性去操作redis命令,而redis的lua脚本正好可以实现这一功能。比如: 扣减库存操作、限流操作等等。 redis的pipelining虽然也可以一次执行一组命令,但是如果在这一组命令的执行过程中,需要根据上一步执行的结果做一些判断,则无法实现。
二、使用lua脚本
Redis中使用的是 Lua 5.1 的脚本规范,同时我们编写的脚本的时候,不需要定义 Lua 函数。同时也不能使用全局变量等等。
1、lua脚本的格式和注意事项1、格式
EVAL script numkeys key [key ...] arg [arg ...]
127.0.0.1:6379> eval "return {KEYS[1],ARGV[1],ARGV[2]}" 1 key1 arg1 arg2
1) "key1"
2) "arg1"
3) "arg2"
127.0.0.1:6379>
2、注意事项
Lua脚本中的redis操作的key最好都是通过 KEYS来传递,而不要写死。否则在Redis Cluster的情况下可能有问题.
1、好的写法
127.0.0.1:6379> eval "return redis.call('set',KEYS[1],'zhangsan')" 1 username
OK
127.0.0.1:6379> get username
"zhangsan"
redis命令操作的key是通过KEYS获取的。
2、差的写法
127.0.0.1:6379> eval "return redis.call('set','username','zhangsan')" 0
OK
127.0.0.1:6379> get username
"zhangsan"
redis命令操作的key是直接写死的。
2、将脚本加载到redis中
需求: 此处定义一个lua脚本,将输入的参数的值+1返回。
注意:
当我们把 lua脚本加载到redis中,这个脚本并不会马上执行,而是会缓存起来,并且返回sha1校验和,后期我们可以通过 EVALSHA 来执行这个脚本。
此处我们记住这个脚本加载后返回的hash值,在下一步执行的时候需要用到。
127.0.0.1:6379> script load "return tonumber(KEYS[1])+1"
"ef424d378d47e7a8b725259cb717d90a4b12a0de"
127.0.0.1:6379>
3、执行lua脚本1、通过eval执行
127.0.0.1:6379> eval "return tonumber(KEYS[1]) + 1" 1 100
(integer) 101
127.0.0.1:6379>
2、通过evalsha执行
ef424d378d47e7a8b725259cb717d90a4b12a0de的值为上一步通过 script load加载脚本后获取的。
127.0.0.1:6379> evalsha ef424d378d47e7a8b725259cb717d90a4b12a0de 1 100
(integer) 101
127.0.0.1:6379>
通过 evalsha 执行的好处是可以节省带宽。如果我们的lua脚本比较长,程序在执行的时候将lua脚本发送到redis服务器则可能耗费的带宽多,如果发送的是hash值的话,则耗费的带宽少。
4、判断脚本是否在redis服务器缓存中
127.0.0.1:6379> script load "return tonumber(KEYS[1])+1"
"ef424d378d47e7a8b725259cb717d90a4b12a0de"
127.0.0.1:6379> script exists ef424d378d47e7a8b725259cb717d90a4b12a0de
1) (integer) 1
127.0.0.1:6379> script exists not-exists-sha1
1) (integer) 0
127.0.0.1:6379>
5、清空服务器上的脚本缓存
注意:
我们无法清除某一个脚本的缓存,只可以清楚所有的缓存,一般情况下没有必要清楚,因为即使有大量的脚本也不会太占用服务器内存。
127.0.0.1:6379> script load "return tonumber(KEYS[1])+1"
"ef424d378d47e7a8b725259cb717d90a4b12a0de"
127.0.0.1:6379> script exists ef424d378d47e7a8b725259cb717d90a4b12a0de
1) (integer) 1
127.0.0.1:6379> script flush
OK
127.0.0.1:6379> script exists ef424d378d47e7a8b725259cb717d90a4b12a0de
1) (integer) 0
6、杀死正在运行的脚本
127.0.0.1:6379> script kill
注意:
该命令只可以杀死正在运行的 只读脚本。对于修改了数据的脚本,无法使用此命令杀死,只能使用 shutdown nosave命令。脚本执行的默认超时时间为 5分钟,可以通过redis.conf配置文件的lua-time-limit配置项修改。脚本即使到达了超时时间,也不会停止执行,因为这违反了lua脚本的原子性。三、lua和redis数据类型转换
Lua的数据类型和Redis的数据类型存在一对一的转换关系,如果将Redis类型转换成Lua类型,然后在转换成Redis类型,那么结果和初试值是一致的。
1、类型转换
Redis to Lua conversion table.
Lua to Redis conversion table.
2、额外的转换规则Lua的布尔类型,Lua的True会转换成Redis的13、3个重要规则1. 数字类型
在Lua中,只有一个number类型,整数和浮点数之间没有区别,如果我们在Lua中返回一个浮点数,实际返回的是一个整数,如果要返回浮点数,需要以字符串的方式返回。
127.0.0.1:6379> eval "return 3.98" 0
(integer) 3
127.0.0.1:6379> eval "return '3.98'" 0
"3.98"
2. lua数组存在nil
当 Redis 将 Lua 数组转换为 Redis 协议时,如果遇到 nil,则转换会停止。即 nil 后的值都不会返回。
127.0.0.1:6379> eval "return {1,2,'data',nil,'can not return value','vv'}" 0
1) (integer) 1
2) (integer) 2
3) "data"
127.0.0.1:6379>
3. Lua的Table类型包含建和值
出现这种情况返回的redis的是一个空数组
127.0.0.1:6379> eval "return {key1 ='value1',key2='value2'}" 0
(empty array)
127.0.0.1:6379>
四、lua脚本中输出日志
这个一般调试我们的脚本的时候比较有用。
redis.log(loglevel,message)
loglevel的取值范围:
举例:
五、一个简单限流的案例1、需求
在 1s 之内,方法最大的并发只能是 5。
1s 和 5 当作参数传递。
2、实现步骤1、编写lua脚本
-- 输出用户传递进来的参数
for i, v in pairs(KEYS) do
redis.log(redis.LOG_NOTICE, "limit: key" .. i .. " = " .. v)
end
for i, v in pairs(ARGV) do
redis.log(redis.LOG_NOTICE, "limit: argv" .. i .. " = " .. v)
end
-- 限流的key
local limitKey = tostring(KEYS[1])
-- 限流的次数
local limit = tonumber(ARGV[1])
-- 多长时间过期
local expireMs = tonumber(ARGV[2])
-- 当前已经执行的次数
local current = tonumber(redis.call('get', limitKey) or '0')
-- 设置一个断点
redis.breakpoint()
redis.log(redis.LOG_NOTICE, "limit key: " .. tostring(limitKey) .. " 在[" .. tostring(expireMs) .. "]ms内已经访问了 " .. tostring(current) .. " 次,最多可以访问: " .. limit .. " 次")
-- 限流了
if (current + 1 > limit) then
return { true }
end
-- 未达到访问限制
-- 访问次数+1
redis.call("incrby", limitKey, "1")
if (current == 0) then
-- 设置过期时间
redis.call("pexpire", limitKey, expireMs)
end
return { false }
2、程序中执行lua脚本
完整代码:
六、lua脚本的debug
当我们编写好了lua脚本后,如果在执行的过程中发生了错误,那么我们如何该如何解决呢?此处我们来了解下如何debug lua 脚本。
1、lua脚本中的几个小命令
在 脚本中打一个断点
redis.breakpoint()
2、断点调试1、执行命令
redis-cli --ldb --eval limit.lua invoked , 1 1000
limit.lua 需要debug的lua文件
invoked 为传递到 lua 脚本中 KEYS 的值
1 和 1000 为传递到 lua 脚本中 ARGV 的值
, 分割 出 KEYS 和 ARGV 的值
2、一些debug指令3、debug运行结果
七、参考文档
java常用字节流
常用的字节流有FileInputStream和FileOutputStream、BufferedInputStream和BufferedOutputStream、DataInputStream和DataOutputStream。
FileInputStream和FileOutputStream是基础的字节输入和输出流,通常在创建缓冲流时需要使用到,也可以用来做文件复制的功能。
* fileInputStream和fileOutputStream字节输入输出流实现文件的复制
*
* @author:tuzongxun
* @Title: fileInAndOuTest
* @param
* @return void
* @date Jul 20, 2016 10:21:41 AM
* @throws FileNotFoundException
*/
public static void fileInAndOuTest() throws IOException {
File file = new File("C:\\Users\\tuzongxun123\\Desktop\\ioTest1.txt");
// 创建一个fileInputStream对象
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\tuzongxun123\\Desktop\\ioTest.txt"));
// 获取文件中字符的长度
int leng = fileInputStream.available();
System.out.println(leng);
for (int i = 0; i < leng; i++) {
// 读取每个字节
int in = fileInputStream.read();
System.out.print(in);
// 把读取的字节写入到另一个文件
fileOutputStream.write(in);
}
fileInputStream.close();
// 使文件立即写入到磁盘
fileOutputStream.flush();
fileOutputStream.close();
}
BufferedInputStream和BufferedOutputStream是缓冲字节输入输出流,相对于基础的字节流有更高的效率,而效率更高的原理在于用空间换取时间。
也就是说使用缓冲流的时候,会先把一定的数据放到缓冲区,也就是内存中,然后read的时候直接从缓冲区读取,这样就减少了读写磁盘文件的次数,从而减少读写时间。
/**
* BufferedInputStream和BufferedOutputStream缓冲流复制文件
*
* @author:tuzongxun
* @Title: bufferedTest
* @param @throws IOException
* @return void
* @date Jul 21, 2016 9:05:57 AM
* @throws
*/
public static void bufferedTest() throws IOException {
File file = new File("C:\\Users\\tuzongxun123\\Desktop\\ioTest1.txt");
File file1 = new File("C:\\Users\\tuzongxun123\\Desktop\\ioTest2.txt");
// 缓冲输入流对象,bis和bos作为缓冲区,
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
// 缓冲输出流对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
//声明缓冲区大小
byte[] byte1 = new byte[1024];
while (bis.read(byte1) != -1) {
bos.write(byte1);
} ;
// 将缓冲区中的数据全部写出
bos.flush();
bis.close();
bos.close();
}
DataInputStream和DataOutputStream被称为数据输入输出流,他们因为自己的特性而常被用于网络传输,它可以保证“无论数据来自何种机器,只要使用一个DataInputStream收取这些数据,就可用本机正确的格式保存它们.“
不过现在使用spring做文件上传和下载时我自己使用的是MultipartFile,所以也就没有做DataInputStream和DataOutputStream的例子。
MultipartFile上传文件的例子可以参考:
使用springMVC实现文件上传和下载之环境配置与上传