Using Java operating Redis need jedis – 2.1.0. Jar, download address: files.cnblogs.com/liuling/jed…

If you need to use Redis connection pool, still need to Commons – the pool – 1.5.4. Jar, download address: files.cnblogs.com/liuling/com…

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; 10 11 import redis.clients.jedis.Jedis; 12 13 public class TestRedis { 14 private Jedis jedis; 15 16 @before 17 public void setup() {18 // Connect to redis server, 192.168.0.100:6379 19 jedis = new jedis ("192.168.0.100", 6379); 21 jedis.auth("admin"); 22} 23 24 /** 25 * redis Store string 26 */ 27@test 28 public void testString() {29 //----- Add data ---------- 30 jedis.set("name","xinxin"); // add value to key-->name -->xinxin 31 system.out.println (jedis. // Result: xinxin 32 33 jedis.append("name", "is my lover"); Println (jedis.get("name")); 35 36 jedis.del("name"); 37 system.out. println(jedis.get("name")); 38. / / set up multiple keys for 39 jedis mset (" name ", "liuling", "age", "23", "qq", "476777 XXX"); 40 jedis.incr("age"); / / add 1 operation System. 41 out. The println (jedis. Get (" name ") + "-" + jedis. Get (" age ") + "-" + jedis. Get (" qq ")); 42 43} 44 45 /** 46 * redis operation Map 47 */ 48 @test 49 public void testMap() {50 //----- add data ---------- 51 Map<String, String> map = new HashMap<String, String>(); 52 map.put("name", "xinxin"); 53 map.put("age", "22"); 54 map.put("qq", "123456"); 55 jedis.hmset("user",map); [minxr]--> [minxr]--> [minxr]--> [minxr]--> List<String> rsmap = jedis.hmget("user", "name", "age", "qq"); 59 System.out.println(rsmap); 62 jedis.hdel("user","age"); 63 System.out.println(jedis.hmget("user", "age")); // Null 64 system.out.println (jedis.hlen("user")); 65 system.out. println(jedis.exists("user")); Return true 66 system.out. println(jedis.hkeys("user")); Println (jedis.hvals("user"))); // Return all keys (system.out.println (jedis.hvals("user"))); Iterator<String> iter=jedis.hkeys("user").iterator(); 70 while (iter.hasNext()){ 71 String key = iter.next(); 72 System.out.println(key+":"+jedis.hmget("user",key)); @test 80 public void testList(){81} 74} 75 76 public void testList(){ 82 Jedis.del (" Java Framework "); 83 System.out.println(jedis.lrange("java framework",0,-1)); 85 Jedis.lpush (" Java Framework ","spring"); 86 jedis.lpush("java framework","struts"); 87 jedis.lpush("java framework","hibernate"); Jedis. Lrange = key; lrange = start; lrange = end; Println (jedis.lrange(" Java Framework ",0,-1)); 91 92 jedis.del("java framework"); 93 jedis.rpush("java framework","spring"); 94 jedis.rpush("java framework","struts"); 95 jedis.rpush("java framework","hibernate"); 96 System.out.println(jedis.lrange("java framework",0,-1)); Sadd ("user","liuling"); sadd("user","liuling"); 106 jedis.sadd("user","xinxin"); 107 jedis.sadd("user","ling"); 108 jedis.sadd("user","zhangxinxin"); 109 jedis.sadd("user","who"); 110 // Remove noname 111 jedis.srem("user","who"); 112 System.out.println(jedis.smembers("user")); System.out.println(jedis.sismember("user", "who")); 114 System.out.println(jedis.srandmember("user")); 115 System.out.println(jedis.scard("user")); 117 118@test 119 public void Test () throws InterruptedException {120 Rpush and lpush here are operations on List. Is a bidirectional linked list (but in terms of performance) 122 jedis.del("a"); 123 jedis.rpush("a", "1"); 124 jedis.lpush("a","6"); 125 jedis.lpush("a","3"); 126 jedis.lpush("a","9"); 127 System.out.println(jedis.lrange("a",0,-1)); // [9, 3, 6, 1] 128 System.out.println(jedis.sort("a")); / / [1, 3, 6, 9] / / 129 System. Input sorted results out. The println (jedis. Lrange (" a ", 0, 1)); 130} 131 132 @test 133 public void testRedisPool() {134 redisutil.getjedis ().set("newname", "newname"); 135 System.out.println(RedisUtil.getJedis().get("newname")); 137 136}}Copy the code

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; 7 7 public final class RedisUtil {8 9 private static String ADDR = "192.168.0.100"; Private static int PORT = 6379; 16 private static String AUTH = "admin"; 16 private static String AUTH = "admin"; 17 18 // Maximum number of available connection instances. The default value is 8. 19 // If the value is -1, there is no limit; If a pool has already allocated maxActive jedis instances, the pool is in a exhausted state. 20 private static int MAX_ACTIVE = 1024; 21 22 // Controls the maximum number of jedis instances in a pool whose state is idle. The default value is also 8. 23 private static int MAX_IDLE = 200; 24 25 // Maximum time to wait for available connections, expressed in milliseconds. The default value is -1, which indicates that the connection will never timeout. If the wait time is exceeded, JedisConnectionException is thrown directly; 26 private static int MAX_WAIT = 10000; 27 28 private static int TIMEOUT = 10000; 29 29 // If Jedis instance is borrowed, validate is performed in advance; If true, the resulting Jedis instances are all available; 31 private static boolean TEST_ON_BORROW = true; 32 33 private static JedisPool jedisPool = null; Static {39 try {40 JedisPoolConfig config = new JedisPoolConfig(); static {39 try {40 JedisPoolConfig = new JedisPoolConfig(); 41 config.setMaxActive(MAX_ACTIVE); 42 config.setMaxIdle(MAX_IDLE); 43 config.setMaxWait(MAX_WAIT); 44 config.setTestOnBorrow(TEST_ON_BORROW); 45 jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); 46 } catch (Exception e) { 47 e.printStackTrace(); 55 public synchronized static Jedis getJedis() {56 try {57 if 55 public synchronized static Jedis getJedis() {56 try {57 if (jedisPool ! = null) { 58 Jedis resource = jedisPool.getResource(); 59 return resource; 60 } else { 61 return null; 62 } 63 } catch (Exception e) { 64 e.printStackTrace(); 65 return null; Public static void returnResource(final jedis jedis) {75} public static void returnResource(final jedis jedis) {75} public static void returnResource(final jedis jedis) {75} public static void returnResource(final jedis jedis) {75} public static void returnResource(final jedis jedis) {75 if (jedis ! = null) { 75 jedisPool.returnResource(jedis); 76} 77} 78}Copy the code

Mysql, Netty, Spring, thread, Spring Cloud, JVM, source code, algorithm, etc., also have a detailed learning plan map, interview questions, etc., need to obtain these contents of the friend please add Q: sample: 909038429/./* Welcome to Java chat