Redis batch command-pipeline and Lua script

Redis pipeline, sending multiple commands to the server at one time, saving network overhead

The pipeline does not consider the issues of transaction and consistency. If the pipeline has 10 commands, of which 2-4 are not successful, other commands will be submitted, but the results will be returned.

public class RedisPipeLine {
    public static void main(String[] args) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(5);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.0.109",6379,3000,null);
        Jedis jedis = null;
        jedis = jedisPool.getResource();
        Pipeline piple = jedis.pipelined();
        for (int i = 0; i < 10; i++) {
            piple.incr("helloworld");
            piple.set("Allen"+i, Math.random()+"");
        }
        List list = piple.syncAndReturnAll();
        System.out.println(list);
    }
}

The execution result is:

[1, OK, 2, OK, 3, OK, 4, OK, 5, OK, 6, OK, 7, OK, 8, OK, 9, OK, 10, OK]

Process finished with exit code 0

View the effect of execution in redis:

127.0.0.1:6379> keys Allen*
 1) "Allen"
 2) "Allen1"
 3) "Allen3"
 4) "Allen9"
 5) "Allen8"
 6) "Allen2"
 7) "Allen5"
 8) "Allen6"
 9) "Allen4"
10) "Allen7"
11) "Allen0"
12) "Allen-Lingling"
127.0.0.1:6379> get Allen5
"0.11796745342678339"
127.0.0.1:6379> 

Reids Lua script, can realize the transaction and atomic operation of batch commands

Extensive use of lua scripts will affect the parallelism of redis. Because he treats the script you wrote as a command to execute, the redis command execution is sequential in a single thread.

Distributed locks can be implemented with lua scripts.

Lua script example:

127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second
1) "key1"
2) "key2"
3) "first"
4) "second"
127.0.0.1:6379> 

Use Lua scripts in java:

public class RedisLuaScript {
    public static void main(String[] args) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(5);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.0.109",6379,3000,null);
        Jedis jedis = null;
        jedis = jedisPool.getResource();
        jedis.set("product_Stock","15");
        String luaScript = "local count = redis.call('get',KEYS[1]) "+
                "local a = tonumber(count) "+
                "local b = tonumber(ARGV[1]) "+
                "if a>= b then " +
                "redis.call('set',KEYS[1],a-b) " +
               // "bb==0" +
                "return 1 " +
                "end " +
                "return 0 ";
        Object obj = jedis.eval(luaScript, Arrays.asList("product_Stock"),Arrays.asList("10"));
        System.out.println(obj);
    }
}

Results of the:

1

Process finished with exit code 0

View inventory results in reids:

127.0.0.1:6379> get product_Stock
"5"
127.0.0.1:6379> 

Add a comment in the script: "bb==0", the execution will report an error, and the previously executed inventory reduction operation will be rolled back

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: '=' expected near '==' 
	at redis.clients.jedis.Protocol.processError(Protocol.java:132)
	at redis.clients.jedis.Protocol.process(Protocol.java:166)
	at redis.clients.jedis.Protocol.read(Protocol.java:220)
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:318)
	at redis.clients.jedis.Connection.getOne(Connection.java:296)
	at redis.clients.jedis.Jedis.getEvalResult(Jedis.java:2845)
	at redis.clients.jedis.Jedis.eval(Jedis.java:2779)
	at redis.clients.jedis.Jedis.eval(Jedis.java:2831)
	at allen.redis.RedisLuaScript.main(RedisLuaScript.java:33)

Process finished with exit code 1

View inventory results in reids:

127.0.0.1:6379> get product_Stock
"15"
127.0.0.1:6379> 

 

Guess you like

Origin blog.csdn.net/pengweismile/article/details/112495065