Redis Common operations

Source: Internet
Author: User

This includes data structures such as commonly used lists, collections (set), ordered collections (sorted set), hash tables (hash), and other feature support.

One, Redis storage string
Jedis.Set("name","Xinxin");//put the value-->xinxin into the Key-->nameJedis.append ("name","Is my lover");//StitchingJedis.del ("name");//Delete a key//set multiple key-value pairsJedis.mset ("name","liuling"," Age"," at","QQ","476777XXX"); JEDIS.INCR (" Age");//to add 1 operations

Second, Redis operation map

//-----Add Data----------map<string, string> map =NewHashmap<string, string>(); Map.put ("QQ","123456"); Jedis.hmset ("User", map);//remove the name from the user and execute the result:[minxr]--> note that the result is a generic list//The first parameter is the key to the Map object in Redis, followed by the key of the object placed in the map, followed by the key can be more than a variable parameterlist<string> Rsmap = Jedis.hmget ("User","name"," Age","QQ");//Delete a key value from a mapJedis.hdel ("User"," Age"); //Delete a key value from a mapJedis.hdel ("User"," Age"); Jedis.hmget ("User"," Age"));//because it was deleted, NULL is returned .Jedis.hlen ("User"));//returns the number of values stored in the key for user 2Jedis.exists ("User"));//whether there is a record of key for user returns trueJedis.hkeys ("User"));//returns all keys in the map objectJedis.hvals ("User"));//returns all the value in the Map objectIterator<String> Iter=jedis.hkeys ("User"). iterator ();  while(Iter.hasnext ()) {String key=Iter.next (); System. out. println (key+":"+jedis.hmget ("User", key)); }  

Third, Jedis operation list

You can use the list to simulate queues, stacks, and support bidirectional operations (L or R)
//Add DataJedis.lpush ("Java Framework","Spring"); //Take out all the data jedis.lrange is out by range,//The first one is key, the second is the starting position, the third is the end position, Jedis.llen gets the length-1 means get allJedis.lrange ("Java Framework",0,-1) Jedis.rpush ("a","1"); //Right into queue Jedis.lpush ("a","6"); //Left into queue //Note that the Rpush and Lpush here are the operations of the list. is a doubly linked list//Sort Positive OrderJedis.sort ("a")

//Set: New value at position 1
Jedis. LSet ("Nick Xu");

Return Length:

Size = Jedis.llen ("userlist");

To crop: Contains

Jedis. LTrim (2); 

Four, Jedis operation set

The elements in the collection are unordered, and therefore the elements cannot be duplicated.
//AddJedis.sadd ("User","W.H.O."); //Remove NonameJedis.srem ("User","W.H.O."); Jedis.smembers ("User"));//get all the added valueJedis.sismember ("User","W.H.O."));//determine if who is an element of the user collectionJedis.srandmember ("User")); Jedis.scard ("User"));//returns the number of elements in a collection

  To traverse a collection:

set<string> fruit = jedis.smembers ("fruit");

  Operation of the collection: includes the intersection of the set (sinter), the difference set (Sdiff), and the set (Sunion)

Jedis.sadd ("milk"); set<string> fruitfood = jedis.sunion ("food");   

Five, Sorted set:

The ordered set is based on the set and adds a parameter for sorting. // 1 . Ordered collection: Sorts according to "second parameter". Jedis.zadd ("User", A,"James"); // 2 . Add again: The element is updated to the current weight at the same time. Jedis.zadd ("User", -,"James"); //  3. Scope of Zset: found from 0 to- 1 of all elements. Set<String> user = Jedis.zrange ("User",0, -1); // 4 Description: We may also have a doubt about how the collection is organized. In fact, the data type of the above user is Java.util.LinkedHashSet 

Vi. some other operations

//1 operation on key://Fuzzy query for key:Set<String> keys = Jedis.keys ("*"); Set<String> keys = Jedis.keys ("user.userid.*"); //@ Delete key:Jedis.del (" City"); //@ is present: Boolean isexists= Jedis.exists ("user.userid.14101"); //  2 . Expiry time://@ Expire: Time is 5sJedis.setex ("user.userid.14101",5,"James"); //@ Live Time (TTL): Times to Live Long seconds= Jedis.ttl ("user.userid.14101"); //@ Remove Key's expire setting: no longer has expiration time jedis.persist ("user.userid.14101"); // 3 . Self-increment integer://@ int type is stored in string type:Jedis.Set("Amount", -+""); //@ Increment or decrement: incr () / decr ()jedis.incr ("Amount"); //@ Increase or Decrease: Incrby () / Decrby ()Jedis.incrby ("Amount", -); // 4 . Data emptying://@ Empty current db:jedis.flushdb (); //@ empty All db:Jedis.flushall (); //  5 . Transaction support://@ Get transaction:Transaction TX=Jedis.multi (); //@ Bulk Operation: TX with Jedis consistent API interface  for(inti =0; I <Ten; i + +) {Tx.Set("Key"+ I,"value"+i); System. out. println ("--------Key"+i); Thread.Sleep ( +); } //@ Execute transaction: Returns the result of its execution for each operation, and success is OKList<Object> results = tx.exec ();

Redis Common operations

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.