1.redis-lua related commands
# Lua script execution EVAL Script numkeys Key [Key ...] Arg [Arg ...] EVALSHA sha1 numkeys Key [Key ...] Arg [Arg ...] # View the specified script has already been stored in the cache which . EXISTS Script SCRIPT [Script ...] # remove all scripts from the script cache. FLUSH SCRIPT # kill the currently running Lua script SCRIPT KILL # Lua script to add a memory SCRIPT LOAD script
2.redis execute Lua way
# 1.redis execute the script will be loaded into memory EVAL Script numkeys Key [Key ...] Arg [Arg ...] EVALSHA sha1 numkeys Key [Key ...] Arg [Arg ...] # 2. local execute the script will not be loaded into memory redis-cli --eval test.lua aaa, bbb
3. Implementation of the principle of Lua
Sha1 value is calculated according to a Lua script if there is sha1 redis memory to use memory to see # redis whether a lua script loads over SCRIPT EXISTS sha1 value
4. proof
SHA1 value calculated lua script
package plantomjs;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author
* @since 2020—01-09
*/
public class PlanTomJs {
public static void main(String[] args) throws NoSuchAlgorithmException {
//lua 脚本
String lua =
"local getnum = function( count )\n"
+ "\tif count == false then\n"
+ "\t\treturn 0 ;\n"
+ "\telse\t\n"
+ "\t\treturn count ; \n"
+ "\tend\n"
+ "\treturn 0 ;\n"
+ "end\t\n"
+ "local plan = KEYS[1] ;\n"
+ "local result = redis.call('get', KEYS[2] );\n"
+ "local keywordPlan = KEYS[3] ;\n"
+ "local keywordResult = redis.call('get', KEYS[4] ) ;\n"
+ "\n"
+ "if tonumber(getnum(plan)) > tonumber(getnum(result)) and tonumber(getnum(keywordPlan)) > tonumber(getnum(keywordResult)) then\n"
+ " \tredis.call('incrby', KEYS[2] , 1 );\n"
+ " \tredis.call('incrby', KEYS[4] , 1 );\n"
+ " \tredis.call('expire', KEYS[2] , 60*60*24 );\n"
+ " \tredis.call('expire', KEYS[4] , 60*60*24 );\n"
+ " \treturn '1';\n"
+ " else\n"
+ " \treturn '0';\n"
+ " end\t\t\n"
+ "\n";
System.out.println(getSha1(lua.getBytes()));
}
public static String getSha1(byte[] input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
SHA1 value 8f8b5c2314b53cd128e2ead7b9e5d42f5f1eabe8
Indicating the presence redis memory
5. View redis -lua configuration
#Lua engine used memory size (in bytes) used_memory_lua: 46080 # ditto used_memory_lua_human: 45.00K #Lua script occupied memory size used_memory_scripts: 53784 used_memory_scripts_human: 52.52K # memory loaded Lua script number number_of_cached_scripts: 95
Use redis-lua script (spring-data-redis) 6. Project in
private boolean ipLuaCheckCount(String ip, App app) {
String lua = "\n"
+ "local result = redis.call('hget', KEYS[1] , ARGV[1])\n"
+ "if result == false then \n"
+ "\tresult = 0 ;\n"
+ "end\n"
+ "local num = KEYS[2] \n"
+ "if tonumber(result) < tonumber(num) then\n"
+ "\tredis.call('hincrby', KEYS[1] ,ARGV[1] , 1 )\n"
+ "\tredis.call('expire', KEYS[1] , 60*60*24 )\n"
+ "\treturn '1' ;\n"
+ "else \n"
+ "\treturn '0' ; \n"
+ "end\n"
+ "\n";
DefaultRedisScript<String> rs = new DefaultRedisScript<>();
//设置脚本
rs.setScriptText(lua);
//定义返回类型。注意如果没有这个定义,spring不会返回结果
rs.setResultType(String.class);
RedisSerializer valueSerializer = jsonRedisTemplate.getValueSerializer();
RedisSerializer stringSerializer = jsonRedisTemplate.getStringSerializer();
List<String> keyList = new ArrayList<>();
keyList.add(RedisKeys.ipMonitorResult(app.getId()));
keyList.add(Objects.nonNull(app.getIpMonitorCount()) ? String.valueOf(app.getIpMonitorCount()) : String.valueOf(3));
List<String> valsList = new ArrayList<>();
valsList.add(ip);
String restult = redisTemplate.execute(rs, valueSerializer, stringSerializer, keyList, valsList.toArray());
if ("0".equals(restult)) {
throw new ConnectException(ConnectExceptionEnum.IP_TASK_BE_ALREADY_FULL);
}
return true;
}