Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are Redis transactions and pipleline

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The editor will share with you what Redis transactions and pipleline are. I hope you will gain a lot after reading this article. Let's discuss it together.

1. Reidis transaction

Redis transactions can execute more than one command at a time with three important guarantees:

Bulk operations are cached in queues before sending EXEC commands. After receiving the EXEC command, enter the transaction execution, any command execution in the transaction fails, and the rest of the commands are still executed. During transaction execution, command requests submitted by other clients are not inserted into the transaction execution command sequence.

A transaction goes through the following three stages from start to execution:

Start the business. Order to join the team. Execute the transaction.

MULTI starts a transaction, then queues multiple commands into the transaction, and finally the transaction is triggered by the EXEC command, executing all the commands in the transaction at the same time:

1. Transaction execution

2 、 watch

Monitor one (or more) key, and if the key (or these) is altered by other commands before the transaction is executed, the transaction will be interrupted.

3 、 discard

Cancels the transaction and discards all commands within the transaction block.

2. Jedis code 1. Configure to support transaction template.setEnableTransactionSupport (true)

2. Code:

RedisTemplate.opsForValue (). Set ("aaa", 321); redisTemplate.watch ("aaa"); redisTemplate.multi (); redisTemplate.opsForValue (). Set ("aaa", 123); redisTemplate.opsForValue (). Set ("bbb", 123); redisTemplate.exec (); 3. Piplelinejedis code:

Map map = new HashMap (); map.put ("aaa", 111); map.put ("bbb", 2222); map.put ("ccc", 3333); List list = redisTemplate.executePipelined (new RedisCallback () {@ Override public Object doInRedis (RedisConnection redisConnection) throws DataAccessException {redisConnection.openPipeline () For (Map.Entry 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")) After reading this article, I believe you have a certain understanding of Redis affairs and what pipleline is, want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report