The following column Redis Tutorial will introduce you to Redis transactions and pipeline. I hope it will be helpful to friends in need!
Redis transaction can execute multiple commands at one time, and has the following three important Guarantee:
- Batch operations are put into the queue cache before sending the EXEC command.
- Enter transaction execution after receiving the EXEC command. If any command in the transaction fails to execute, the remaining commands are still executed.
- During the transaction execution process, command requests submitted by other clients will not be inserted into the transaction execution command sequence.
A transaction will go through the following three stages from start to execution:
- Start transaction.
- Command to join the queue.
- Execute transactions.
MULTI Start a transaction, then queue multiple commands into the transaction, and finally the transaction is triggered by the EXEC command, Execute all commands in the transaction together:
1. Transaction execution
2. watch
Monitor one (or more) keys. If this (or these) keys are modified by other commands before the transaction is executed, the transaction will be interrupted.
3. discard
Cancel the transaction and give up execution All commands within the transaction block.
1. Configuration support Transaction
template.setEnableTransactionSupport(true);<br>2. Code:
<br>##
redisTemplate.opsForValue().set("aaa", 321); redisTemplate.watch("aaa"); redisTemplate.multi(); redisTemplate.opsForValue().set("aaa", 123); redisTemplate.opsForValue().set("bbb", 123); redisTemplate.exec();
jedis Code:
<br>
Map<string> map = new HashMap(); map.put("aaa", 111); map.put("bbb", 222); map.put("ccc", 3333); List list = redisTemplate.executePipelined(new RedisCallback<object>() { @Override public Object doInRedis(RedisConnection redisConnection) throws DataAccessException { redisConnection.openPipeline(); for (Map.Entry<string> mapEntry : map.entrySet()) { redisConnection.set(redisTemplate.getKeySerializer().serialize(mapEntry.getKey()), redisTemplate.getValueSerializer().serialize(mapEntry.getValue())); } return null; } }, redisTemplate.getValueSerializer()); System.out.println(redisUtil.get("aaa")); System.out.println(redisUtil.get("bbb")); System.out.println(redisUtil.get("ccc"));</string></object></string>
The above is the detailed content of About Redis transactions and pipeline. For more information, please follow other related articles on the PHP Chinese website!