Java Operations Redis

Source: Internet
Author: User

1. Create project, project name Redisdemo, directory structure

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8F/47/wKiom1jZxe7Bc1B0AAA43V0Hf3U853.png-wh_500x0-wm_ 3-wmp_4-s_4106024192.png "title=" Qq20170328100902.png "alt=" Wkiom1jzxe7bc1b0aaa43v0hf3u853.png-wh_50 "/>


The contents of the 2.RedisJava file are as follows
package com.demo;import java.util.hashmap;import java.util.iterator;import java.util.list; import java.util.map;import redis.clients.jedis.jedis;import redis.clients.jedis.jedispool; Import redis.clients.jedis.jedispoolconfig;public class redisjava {public static  void main (String[] args) {jedis jedis = null;//server ipstring add =  "127.0.01";//port number int port = 6379;//maximum available quantity int max_active = 1024;//idle Jedis instance number int  max_idle = 200;//wait time, number of milliseconds int max_wait = 10000;//time-out int timeout =  10000;//Verify the operation, the validated Jedis are available boolean test_on_borrow = true;//connection pool objects Jedispool jedispool  = null;//Configuration Connection Pool jedispoolconfig config = new jedispoolconfig (); Config.setmaxactive (max_active); Config.setmaxidle (Max_idle); config.setmaxwait (max_wait); Config.setTestOnBorrow (Test_on_borrow); jedispool = new&nbSp Jedispool (config,add,port,timeout); if (jedispool != null) {Jedis = jedispool.getresource ();} if (Jedis != null) {/* *  the action string  *///1. Add Data Jedis.set ("name",  "Xiaoxiao");// Gets the data System.out.println (Jedis.get ("name"));//2. Splicing jedis.append to the existing key ("name",  " i love you" ); System.out.println (Jedis.get ("name"));//3. Delete Keyjedis.del ("name"); System.out.println (Jedis.get ("name"));//4. Set multiple key-value pairs jedis.msetnx ("name", "Xiaoxiao", "Age", "E", "email", "[email  protected] "); System.out.println (Jedis.get ("name")); System.out.println (Jedis.get ("Age")); System.out.println (Jedis.get ("email"))//5. Add 1 Operation JEDIS.INCR ("age") to Key age; System.out.println (Jedis.get ("Age")); System.out.println ("-----------------------------------I am the dividing line---------------------------------");/* *   Operations Collection MAP&NBSP;*///1. Add Data map<string,string> maps = new hashmap<string,string > ("name",  "Xiaoxiao"), Maps.put ("Age",  "Maps.put"), Maps.put ("Email ", " [email protected] "), Jedis.hmset (" Mymap ",  maps);//Get data from map list<string>  Lists = jedis.hmget ("Mymap",  "name", "Age", "email"); SYSTEM.OUT.PRINTLN (lists)//2. Delete the key values in the map Jedis.hdel ("Mymap",  "email"); System.out.println (Jedis.hmget ("Mymap",  "email"))//3. Get the number of elements Mymap (System.out.println ("Jedis.hlen")) ;//4. Determine whether Mymap exists System.out.println (jedis.exists ("Mymap"));//5. Gets all Mymap in KeySystem.out.println (Jedis.hkeys (" Mymap ")//6. Gets all the values in Mymap System.out.println (jedis.hvals (" Mymap "));//7. Traversal mymapiterator<string> it  = jedis.hkeys ("Mymap"). Iterator ();while  (It.hasnext ())  {String key =  It.next (); System.out.println (key +  "="  + jedis.hget ("Mymap",  key));} System.out.println ("-----------------------------------I am the dividing line---------------------------------");/* *   Operation LIST&NBSP;*///1. Add Data Jedis.lpush to list ("MyList",  "Redis"), Jedis.lpush ("MyList",  "git"); Jedis.lpush ("MyList", " maven "); System.out.println (Jedis.lrange ("MyList",  0, -1))//2. Delete Listjedis.del ("MyList");//3. Inserts one or more values into the tail of the list Jedis.rpush ("MyList",  "Redis"), Jedis.rpush ("MyList",  "git"), Jedis.rpush ("MyList",   "Gradle");//4. All elements in the output mylist System.out.println (Jedis.lrange ("MyList",  0, -1)); System.out.println ("-----------------------------------I am the dividing line---------------------------------");/* *   Operation SET&NBSP;*///1. Add Data Jedis.sadd ("MySet",  "Gaoneng"), Jedis.sadd ("MySet",  "Banya"), Jedis.sadd (" MySet ", " Houzi ") Jedis.sadd (" MySet ", " Pangzi ")//2. Get all Values System.out.println (Jedis.smembers (" MySet ")) //3. Determine whether an element exists System.out.println (Jedis.sismember ("MySet",  "Gaoneng"));//4. Delete element Jedis.srem ("MySet",  " Gaoneng "); System.out.println (Jedis.smembers ("MySet"))//5. Returns a random element in the collection System.out.println (Jedis.srandmember ("MySet"));// 6. Returns the number of elements in the set System.out.println (Jedis.scard ("MySet")); System.out.println ("-----------------------------------I am the dividing line---------------------------------");/* * list  sort  */jedis.del (" orderlist "); Jedis.rpush (" OrderList ", " 1 "); Jedis.rpush ("OrderList",  "9"), Jedis.rpush ("OrderList",  "3"), Jedis.lpush ("OrderList",  "6"); Jedis.lpush ("OrderList",  "4"); Jedis.lpush ("OrderList",  "8"); System.out.println (Jedis.lrange ("OrderList",  0, -1));//output sorted results System.out.println (Jedis.sort (" OrderList ")); System.out.println (Jedis.lrange ("OrderList",  0, -1));}}}


3. Run the program execution results

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/45/wKioL1jZxlLS8X4GAABpqFQRDiA511.png-wh_500x0-wm_ 3-wmp_4-s_3270445383.png "title=" Qq20170328101043.png "alt=" Wkiol1jzxlls8x4gaabpqfqrdia511.png-wh_50 "/>

This article from the "Vegetarian Yan" blog, declined to reprint!

Java Operations Redis

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.