Redis Java Operations Client

Source: Internet
Author: User

Jedis Common operations

1. Test connectivity

New Jedis ("192.168.1.201", 6380,10000); System.out.println (Jedis.ping ());

Console printing

PONG

2.5+1 operation

5 Data type operations: String list set hash Zset

 String

String nothing to say, the longest use of a

New Jedis ("192.168.1.201", 6379);        System.out.println (Jedis.ping ());                Jedis.set ("K1", "v1");        Jedis.set ("K2", "V2");        Jedis.set ("K3", "V3");        System.out.println (Jedis.get ("K1"));  // "V1"

 List

Lpush

Jedis.lpush ("list01", "1", "2", "3", "4", "5");  // Lpush for advanced post-out can be so understood, starting from the left into the stack System.out.println (Jedis.lrange ("list01", 0,-1));  // [5, 4, 3, 2, 1]

Rpush

Jedis.rpush ("list02", "1", "2", "3", "4", "5");  Rpush for FIFO System.out.println (Jedis.lrange (//[1, 2, 3, 4, 5]

Lpop

System.out.println (Jedis.lpop ("list01"));  // lpop pop-up stack top data 5 System.out.println (Jedis.lrange ("list01", 0,-1));  // [4, 3, 2, 1]

Rpop

System.out.println (Jedis.rpop ("list02"));  // rpop pop-up stack data // [1, 2, 3, 4]

Lindex

System.out.println (Jedis.lrange ("list02", 0,-1)); System.out.println (Jedis.lindex (///lindex get elements by index subscript (top to bottom)

Llen

// [1, 2, 3, 4] // llen Get length 4

Lrem

Jedis.rpush ("list03", "1", "1", "1", "2", "2", "2", "3", "3", "3", "4", "4", "4", "5"), Jedis.lrem ("list03", 2, "3");  // Delete 2 "3" // [1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5]

LTrim

Jedis.lpush ("list04", "1", "2", "3", "4", "5", "6", "7", "8"); System.out.println (Jedis.lrange (//[8, 7, 6, 5, 4, 3, 2, 1]// intercept the value labeled 3-5, the table below starts with 0, Re-assigned to list01System.out.println (Jedis.lrange ("list04", 0,-1));  // [5, 4, 3]

Rpoplpush

Jedis.lpush ("list01", "1", "2", "3", "4", "5");        System.out.println (Jedis.lrange ("list01", 0,-1));        Jedis.lpush ("list02", "1", "2", "3");        System.out.println (Jedis.lrange ("list02", 0,-1));        Jedis.rpoplpush ("list01", "list02"); // Pop a data, use Rpop pop up the bottom data, Lpush put in another        // [5, 4, 3, 2]        // [1, 3, 2, 1]

LSet

// LSet Key index value assigns a value to the index position value [5, X, 3, 2] System.out.println (Jedis.lrange ("list01", 0,-1));

Linsert

// [5, X, 3, 2] // Insert a Java before X, if the specified value is duplicated, the first one is found System.out.println (Jedis.lrange ("list01", 0,-1));

3. Transaction Submission

Daily:

    

Transaction Transaction = jedis.multi ();        Transaction.set ("K4", "v44");        Transaction.set ("K5", "V55"); //         transaction.exec ();                 Transaction.discard ();

Locking

 Public classTESTTX { Public BooleanTransmethord () {Jedis Jedis=NewJedis ("192.168.1.201", 6380); intBalance//Balance        intDebt;//Amount Owed        intAmttosubtract = 10;//Actual brush AmountJedis.set ("Balance", "100"); Jedis.set ("Debt", "0"); Jedis.watch ("Balance");
Thread.Sleep (7000); Balance= Integer.parseint (Jedis.get ("balance")); if(Balance <amttosubtract) {Jedis.unwatch (); System.out.println ("Modify"); return false; } Else{System.out.println ("*********transaction***********"); Transaction Transaction=Jedis.multi (); Transaction.decrby ("Balance", amttosubtract); Transaction.incrby ("Debt", amttosubtract); Transaction.exec (); Balance= Integer.parseint (Jedis.get ("balance")); Debt= Integer.parseint (Jedis.get ("Debt")); System.out.println ("*******************" +balance); System.out.println ("*******************" +debt); return true; } } /*** Generally speaking, the watch command is to mark a key, if a key is marked, * before committing a transaction if the key was modified by someone else, the transaction will fail, which can usually be tried again in the program. * First marked the key balance, and then check whether the balance is sufficient, not enough to cancel the mark, do not make deductions; * enough to start a transaction for an update operation, if the key balance is modified by someone else during this period, the error occurs when committing the transaction (exec), and the program can usually capture This type of error is re-executed until it succeeds. * @paramargs*/ Public Static voidMain (string[] args) {TESTTX test=NewTesttx (); BooleanRetValue =Test.transmethord (); System.out.println ("Main RetValue---------" +RetValue); }}

If a process modifies the watch's key during transaction execution, the transaction is rolled back

Modify Program Open Comment

Thread.Sleep (7000);

To modify the value of balance during

127.0.0.1:6380> Set balance 5OK

The result of the program running is:

Modify
Main RetValue---------False

4. master-slave replication

6379

127.0.0.1:6379> Info replication# replicationrole:masterconnected_slaves:1slave0:ip= 127.0.0.1,port=6380,state=online,offset=232636,lag=1master_repl_offset:232636repl_backlog_active: 1repl_backlog_size:1048576repl_backlog_first_byte_offset:232609Repl_backlog_histlen :

6380

127.0.0.1:6380> Info replication# replicationrole:slavemaster_host:127.0.0.1master_port:6379 Master_link_status:upmaster_last_io_seconds_ago:5master_sync_in_progress:0Slave_repl_ Offset:232650slave_priority:slave_read_only: 1connected_slaves:0Master_ Repl_offset:0repl_backlog_active:0repl_backlog_size:1048576Repl_backlog_first_ Byte_offset:2Repl_backlog_histlen:

 Public class Testapi {    publicstaticvoid  main (string[] args) {        new Jedis ("192.168.1.201", 6379);         New Jedis ("192.168.1.201", 6380);                Jedis_m.set ("K1", "v1");         = Jedis_s.get ("k1");        System.out.println (v1);  "V1"            }}

Redis Java Operations Client

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.