Copyright belongs to the author, any form of reprint please contact the author to obtain authorization and indicate the source.

How about executing the lua script on RedisTemplate? Check out the following method code, which introduces the Lua content script into the DefaultRedisScript class of RedisScript, which is the script support class provided by Redis. See redis Script Reference for more information

protected RedisScript<Long> getRedisLockScript() {
        String script = "local key = ARGV[1]; local expiration = ARGV[2]; local value = 1;";
        script += "if redis.call('EXISTS', key) == 1 then return -1 else redis.call('SET', key, value); redis.call('EXPIRE', key, expiration); return 1; end";
        return new DefaultRedisScript<>(script, Long.class);
    }
Copy the code

You can see the executing source flow here by looking at the execute method:

RedisTemplate.java
execute()

  1. execute()Three-parameter method
  2. execute()Five parameter method
  3. execute()A method body with five parameters
  4. The lasteval()Method to execute the redis script command

    I’m going to go intoeval()Method corresponding implementation classDefaultScriptExecuteIf no preloaded lua script is found in Redis, an error is reported, and the whole script is serialized to Redis for execution:

Let’s look at the following script serialization execution code scriptBytes(), which corresponds to the StringRedisSerializer class

StringRedisSerializer

At this point, the RedisScript source analysis of RedisTemplate in SpringBoot is over