Redis Learning Note (7)-Transactions

Source: Internet
Author: User

Package Cn.com;import Java.util.list;import Redis.clients.jedis.jedis;import redis.clients.jedis.Transaction; public class Redis_transactions {public static Jedis Redis = new Jedis ("localhost", 6379);//Connection redis/** * Basic Transaction Usage * Default to User 1,user2 are initialized to 1000 * Open transaction * to User:1:money plus one * to User:2:money minus one * execution transaction * */public static void Base_trans () throws Interrupte    DEXCEPTION{REDIS.FLUSHDB ();//Clear Data Redis.set ("Money", "$"), Redis.set ("Money1", "1000");        Long start = System.currenttimemillis ();     Transaction tx = Redis.multi ();     TX.INCR ("Money"),//to the user user:1 add a dollar tx.decr ("Money1"),//to the user user:1 minus a piece of money list<object> results = tx.exec ();     Long end = System.currenttimemillis ();        System.out.println ("Transaction SET:" + ((End-start)/1000.0) + "seconds");    System.out.println (Redis.get ("money"));    System.out.println (Redis.get ("Money1")); Redis.disconnect (); }/** * User transactions * Default to User1,user2 are initialized to 1000 * Open transaction * to User:1:money plus a * to User:2:money minus one * We user:2:money value is a is unable to perform the operation minus 1 Is that still going to work? * Because Redis transactions are relatively simple, such as the following problem requires the developer to control, Redis transaction is not control * */public static void User_trans () throws Interruptedexcept    ION{REDIS.FLUSHDB ();//Clear Data Redis.set ("User:1:money", "n"), Redis.set ("User:2:money", "a");     Long start = System.currenttimemillis ();          Transaction tx = Redis.multi (); TX.INCR ("User:1:money"),//to the user user:1 add a piece of money TX.DECR ("User:2:money");//to the user user:1 minus a piece of money list<object> results = TX     . exec ();     Long end = System.currenttimemillis ();         System.out.println ("Transaction SET:" + ((End-start)/1000.0) + "seconds");    System.out.println (Redis.get ("User:1:money"));    System.out.println (Redis.get ("User:2:money")); Redis.disconnect ();  }/** * The process of executing a transaction other clients change the key value, resolve the data consistency problem * through watch key monitoring to implement other client modification data, transaction cancellation * Usage first use watch to start the monitoring of key in the open transaction, the order must first monitor in the execution of the transaction * */public static void Changedata_trans () throws Interruptedexception{redis.flushdb ();//Clear Data Redis.set ("User:1:money",    "Redis.set"); ("User:2:money", "1000"); Long start = System.currenttiMemillis ();    Redis.watch ("User:1:money");     Transaction tx = Redis.multi (); TX.INCR ("User:1:money"),//to the user user:1 add a piece of money TX.DECR ("User:2:money"),//to the user user:1 minus a piece of money changedata ();//Change the data method list<        object> results = tx.exec ();     Long end = System.currenttimemillis ();         System.out.println ("Transaction SET:" + ((End-start)/1000.0) + "seconds");    System.out.println (Redis.get ("User:1:money"));    System.out.println (Redis.get ("User:2:money")); Redis.disconnect ();  }/** * Impersonate a new client to modify the element corresponding to the key monitored in the transaction * * **/public static void Changedata () {Jedis redis2 = new Jedis ("localhost", 6379);//  Connect Redis redis2.set ("User:1:money", "8888"); public static void Main (String [] args) throws Interruptedexception{changedata_trans ();}}

  

Redis Learning Note (7)-Transactions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.