"Reprint" in Java using Jedis operation Redis

Source: Internet
Author: User
Tags redis server

1 package com.test;  2 3 Import Java.util.HashMap;  4 Import Java.util.Iterator;  5 Import Java.util.List;  6 Import Java.util.Map;  7 8 Import Org.junit.Before; 9 Import Org.junit.Test; Ten import Redis.clients.jedis.Jedis;  public class Testredis {Jedis Jedis; @Before-public void Setup () {18//Connect Redis server, 192.168.0.100:6379 Jedis = new Jedi S ("192.168.0.100", 6379);   20//Authority Authentication Jedis.auth ("admin"); /** * Redis Storage String 29 */@Test public void teststring () {//-- ---add Data----------jedis.set ("name", "Xinxin");//Put value-->xinxin SYSTEM.OUT.PR in Key-->name Intln (Jedis.get ("name"));//Execution Result: Xinxin jedis.append ("name", "Is My Lover");  Stitching System.out.println (Jedis.get ("name"));  Jedis.del ("name"); Delete a key PNs System.out.println (Jedis.get ("name")); 38        Set multiple key-value pairs of jedis.mset ("name", "LiuLing", "Age", "Max", "QQ", "476777XXX"); JEDIS.INCR ("Age"); Add 1 operation to System.out.println (Jedis.get ("name") + "-" + jedis.get ("age") + "-" + jedis.get ("QQ");         /** * Redis Operation map */@Test * * TestMap () {50 -----Add Data----------Wuyi map<string, string> Map = new hashmap<string, string> (); Map.put ("name", "Xinxin"); Map.put ("Age", "22"); Map.put ("QQ", "123456"); Jedis.hmset ("user", map); 56//Remove name from user, execute result:[minxr]--> Note that the result is a generic list 57///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 Ke Y can be followed by multiple, variable-parameter list<string> Rsmap = Jedis.hmget ("User", "name", "Age", "QQ");   System.out.println (RSMAP); 60 61//Delete a key value in map Jedis.hdel ("User", "age"); System.out.println (Jedis.hmget ("User", "age"); Because it was deleted, the return is N.Ull System.out.println (Jedis.hlen ("user")); Returns the number of values stored in the key for user 2 System.out.println (jedis.exists ("user")), or the existence of a key for user's record returns true for the System.  Out.println (Jedis.hkeys ("user"));//returns all keys in the Map Object System.out.println (jedis.hvals ("user"));//returns all the value in the Map object   Iterator<string> Iter=jedis.hkeys ("user"). Iterator ();   (Iter.hasnext ()) {String key = Iter.next ();   System.out.println (key+ ":" +jedis.hmget ("User", key)); /** * Jedis Operation list */@Test-public void testlist   () {81//before starting, remove all content from the Jedis.del ("Java framework");   System.out.println (Jedis.lrange ("Java framework", 0,-1));   84//First store three data Jedis.lpush ("Java framework", "Spring") in key Java framework;   Jedis.lpush ("Java framework", "struts"); Jedis.lpush ("Java framework", "Hibernate"); 88//Remove all data jedis.lrange is out by range, 89//First is key, second is start position, third is end position, Jedis.llen gets length-1 means get all   Tem.out.println (Jedis.lrange ("Java framework", 0,-1)); Jedis.del ("Java framework");   Jedis.rpush ("Java framework", "Spring");   94 Jedis.rpush ("Java framework", "struts");  Jedis.rpush ("Java framework", "Hibernate"); System.out.println (Jedis.lrange ("Java framework", 0,-1));         98/** Jedis Operation Set 101 */102 @Test 103 public void Testset () {104  Added Jedis.sadd ("User", "liuling");  106 Jedis.sadd ("User", "xinxin");  107 Jedis.sadd ("User", "Ling");  108 Jedis.sadd ("User", "zhangxinxin"); 109 jedis.sadd ("user", "who");  110//Removal of Noname 111 Jedis.srem ("user", "who"); System.out.println (jedis.smembers ("user"));//Get all the added value 113 System.out.println (Jedis.sismember ("Use R "," who "));/Determine if who is the element of user collection System.out.println (jedis.srandmember ("user")); System.out.println (Jedis.scard ("user"));//Returns the number of elements in the collection of 117 118 @Test 119 public void Test ( ) throws Interruptedexception {//jedis Sort 121//note that the Rpush and Lpush here are the operations of the list.  is a doubly linked list (but from the performance point of view) 122 Jedis.del ("a"),///First clear the data, then add data to test 123 Jedis.rpush ("A", "1");  124 Jedis.lpush ("A", "6");  Jedis.lpush ("A", "3");  126 Jedis.lpush ("A", "9"); 127 System.out.println (Jedis.lrange ("a", 0,-1));//[9, 3, 6, 1] (System.out.println ("a"));  [1, 3, 6, 9]//input sorted results 129 System.out.println (Jedis.lrange ("a", 0,-1)); 131 @Test133 public void Testredispool () {134 Redisutil.getjedis (). Set ("NewName", "Chinese test "); 135 System.out.println (Redisutil.getjedis (). Get (" NewName ")); 136}137}

Redis Connection Pool:

 1 package com.test; 2 3 Import Redis.clients.jedis.Jedis; 4 Import Redis.clients.jedis.JedisPool; 5 Import Redis.clients.jedis.JedisPoolConfig;      6 7 Public final class Redisutil {8 9//redis server IP10 private static String ADDR = "192.168.0.100"; 11 12     Redis port number private static int port = 6379;14 15//access password + private static String AUTH = "admin"; 17 18//The maximum number of available connection instances, the default value is 8;19//If the assignment is 1, it is unrestricted, and if the pool is already assigned a maxactive Jedis instance, the state of the pool is exhausted (exhausted) at this time. The private static int max_active = 1024;21 22//controls the maximum number of instances of a pool with an idle (idle) Jedis instance, and the default value is 8. Full private static int max_idle = 200;24 25//The maximum time to wait for an available connection, in milliseconds, the default value is-1, which means never time out.  If the wait time is exceeded, the direct throw jedisconnectionexception;26 private static int max_wait = 10000;27-private static int TIMEOUT = 10000;29 30//When borrow an Jedis instance, the validate operation is performed in advance, and if true, the resulting Jedis instance is available; private static Boolean test_on _borrow = true;32 private static Jedispool Jedispool = NuLL;34 35/**36 * Initializing the Redis connection pool PNs */38 Static {# try {jedispoolconfig config =             New Jedispoolconfig (); config.setmaxactive (max_active); Config.setmaxidle (Max_idle); 43 Config.setmaxwait (max_wait); Config.settestonborrow (test_on_borrow); jedispool = new Jedi SPool (config, ADDR, PORT, TIMEOUT, AUTH), and a catch (Exception e) {e.printstacktrace (); 48} 49}50 51/**52 * Get Jedis instance * @return54 */55 public synchronized static Jedis Getjedis ()                 {Jedispool = null) {Jedis resource = Jedispool.getresource (); 59 Return resource;60} else {null;62}63} catch (Exc Eption e) {e.printstacktrace (), null;66 return}67}68 69/**70 * Release Jedis Resources * @paramJEDIS72 */73 public static void Returnresource (final Jedis Jedis) {if (Jedis! = null) {75 Jedispool.returnresource (Jedis); 76}77}78}

"Reprint" in Java using Jedis 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.