Introduction of redis
In simple terms, Redis is a database, but different from traditional databases, redis data is stored in memory, so the reading and writing speed is very fast, so Redis is widely used in the cache direction. In addition, Redis is often used for distributed locks. Redis provides multiple data types to support different business scenarios. In addition, Redis supports transactions, persistence, LUA scripting, LRU driver events, and a variety of clustering schemes.
Redis related tutorials recommended (online viewing and links to web disk)
Online video watching:Complete Redis in 13 hours
Information, source code download:www.bjpowernode.com/javavideo/1…
Use Lua in Redis
There are two ways to execute Lua scripts in Redis: eval and evalsha.
1.1 the eval
The following example uses key lists and parameter lists to provide more flexibility for Lua scripts:
KEYS[1]=”redis”, ARGV[1]=”world”, so return “Hello redisworld”.
If the Lua script is longer, you can also execute the file directly using redis-cli–eval.
Note that you do not need to specify the number of keys in this way. Use a comma to separate keys from args. Note the Spaces around commas.
The eval command is essentially the same as the –eval parameter. If a client wants to execute a Lua script, it first writes the Lua script code on the client and sends the script as a string to the server. The server returns the result to the client.
1.2 evalsha
In addition to using eval, Redis provides the evalsha command to execute Lua scripts.
First, load the Lua script to the Redis server to obtain the SHA1 checksum of the script. The EVALsha command uses SHA1 as a parameter to directly execute the corresponding Lua script, avoiding the cost of sending Lua scripts each time. This way the client doesn’t have to execute the script every time, the script stays on the server, and the script functionality is reused.
Load scripts
Script load command script contents can be loaded into the Redis memory, for example, the following will lua_get. Lua is loaded into the Redis, get SHA1 for: “7413 dc2440db1fea7c0a0bde841fa68eefaf149c”
Execute the script
Evalsha is used as follows, using the SHA1 value. The eval logic is the same as eval.
To call the lua_get. Lua script, do the following:
2. The Lua RedisAPI
Call (); call (); call (); call ();
The effect of Redis is as follows:
In addition, Lua can also use the redis. Pcall function to implement the call to Redis. The difference between redis. However, redis. Pcall will ignore the error and continue to execute the script, so the function selection should be based on the specific application scenario in the actual development.
KEYS can be obtained by KEYS[1] and values can be obtained by ARGV[1].
3. Development tips
Lua can use the redis.log function to output Lua script logs to redis log files, but the log level must be controlled. Redis3.2 provides the Lua Script Debugger for debugging complex Lua scripts (see redis. IO /topics/ LDB).
Others include:
redis.LOG_DEBUG
redis.LOG_VERBOSE
redis.LOG_NOTICE
redis.LOG_WARNING
SpringBoot executes the Lua script via the RedisTemplate
1.RedisScript
First you need to introduce the spring-boot-starter-data-redis dependency, and second place the Lua script in the Resources directory.
This configuration worked fine in Spring Boot2.0, but testing in Spring Boot1.5 failed and required changing list. class to a specific return type (such as long.class).
RedisScript’s getSha1() method gets the script summary.
2. Invoke the script
In case of serialization problems, you can specify the serialization method.