Redis系列八 使用Jedis
使用Jedis jar操作Redis
1、配置redis.conf文件,修改
2、建java工程,加入 jedis jar包
3、代码示例:
1 package com.ntjr.redis; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Set; 8 9 import org.junit.Before; 10 import org.junit.Test; 11 12 import redis.clients.jedis.Jedis; 13 14 public class TestApi { 15 private Jedis jedis; 16 17 /** 18 * 连接192.168.15.128:6379 Redis 19 */ 20 @Before 21 public void before() { 22 jedis = new Jedis("192.168.15.128", 6379); 23 } 24 25 26 /** 27 * 测试set get 及 过期时间,返回-1代表永不过期 28 */ 29 @Test 30 public void testSetKey() { 31 jedis.set("k1", "v1"); 32 jedis.set("k2", "v2"); 33 jedis.set("k3", "v3"); 34 System.out.println(jedis.get("k3")); 35 System.out.println(jedis.ttl("k1")); 36 } 37 38 39 /** 40 * 获取所有的key 41 */ 42 @Test 43 public void testGet() { 44 Set<String> keys = jedis.keys("*"); 45 for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) { 46 String key = (String) iterator.next(); 47 System.out.println(key); 48 } 49 } 50 51 /** 52 * 是否存在k2 53 */ 54 @Test 55 public void testExists() { 56 System.out.println(jedis.exists("k2")); 57 } 58 59 /** 60 * string 追加 61 */ 62 @Test 63 public void testAppend() { 64 jedis.append("k1", "myredis"); 65 System.out.println(jedis.get("k1")); 66 } 67 68 /** 69 * 设置多个键值对 70 */ 71 @Test 72 public void testMSET() { 73 jedis.mset("str1", "v1", "str2", "v2", "str3", "v3"); 74 System.out.println(jedis.mget("str1", "str2", "str3")); 75 } 76 77 /** 78 * 测试为list设置值,获取list集合 79 */ 80 @Test 81 public void testLPUSH() { 82 jedis.lpush("mylist", "v1", "v2", "v3", "v4", "v5"); 83 List<String> myList = jedis.lrange("mylist", 0, -1); 84 for (String element : myList) { 85 System.out.println(element); 86 } 87 } 88 89 /** 90 * Set集合的添加、遍历、删除 91 */ 92 @Test 93 public void testSet() { 94 jedis.sadd("orders", "jd001"); 95 jedis.sadd("orders", "jd002"); 96 jedis.sadd("orders", "jd003"); 97 Set<String> orders = jedis.smembers("orders"); 98 for (Iterator<String> iterator = orders.iterator(); iterator.hasNext();) { 99 System.out.println(iterator.next()); 100 } 101 jedis.srem("orders", "jd002"); 102 System.out.println(jedis.smembers("orders").size()); 103 } 104 105 /** 106 * Map集合操作,获取多个值 107 */ 108 @Test 109 public void testHash() { 110 jedis.hset("hash1", "uername", "lisi"); 111 System.out.println(jedis.hget("hash1", "uername")); 112 Map<String, String> map = new HashMap<>(); 113 map.put("telphone", "13811814763"); 114 map.put("address", "atguigu"); 115 map.put("email", "abc@163.com"); 116 jedis.hmset("hash1", map); 117 List<String> result = jedis.hmget("hash1", "telphone", "email"); 118 for (String element : result) { 119 System.out.println(element); 120 } 121 } 122 123 /** 124 * Zset设置,为每个数据设置 score 125 */ 126 @Test 127 public void testZSET() { 128 jedis.zadd("zset01", 60d, "v1"); 129 jedis.zadd("zset01", 70d, "v2"); 130 jedis.zadd("zset01", 80d, "v3"); 131 jedis.zadd("zset01", 90d, "v4"); 132 Set<String> zset = jedis.zrange("zset01", 0, -1); 133 for(Iterator<String> iterator = zset.iterator();iterator.hasNext(); ){ 134 System.out.println(iterator.next()); 135 } 136 } 137 138 }
4、使用JedisPool连接池
1 package com.ntjr.redis; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 public class JedisPoolUtils { 8 9 private static volatile JedisPool jedisPool = null; 10 11 private JedisPoolUtils() { 12 } 13 14 public static JedisPool getJedisPoolInstance() { 15 synchronized (JedisPoolUtils.class) { 16 if (jedisPool == null) { 17 synchronized (JedisPoolUtils.class) { 18 if (jedisPool == null) { 19 JedisPoolConfig poolConfig = new JedisPoolConfig(); 20 poolConfig.setMaxActive(1000); 21 poolConfig.setMaxIdle(30); 22 poolConfig.setMaxWait(100*1000); 23 poolConfig.setTestOnBorrow(true); 24 jedisPool = new JedisPool(poolConfig, "192.168.15.128", 6379); 25 } 26 } 27 } 28 } 29 return jedisPool; 30 } 31 32 33 34 public static void release(JedisPool jedisPool,Jedis jedis){ 35 if(jedis!=null){ 36 jedisPool.returnResourceObject(jedis); 37 } 38 } 39 40 }
5、JedisPool配置参数
JedisPool的配置参数大部分是由JedisPoolConfig的对应项来赋值的。
maxActive:控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted。
maxIdle:控制一个pool最多有多少个状态为idle(空闲)的jedis实例;
whenExhaustedAction:表示当pool中的jedis实例都被allocated完时,pool要采取的操作;默认有三种。
WHEN_EXHAUSTED_FAIL --> 表示无jedis实例时,直接抛出NoSuchElementException;
WHEN_EXHAUSTED_BLOCK --> 则表示阻塞住,或者达到maxWait时抛出JedisConnectionException;
WHEN_EXHAUSTED_GROW --> 则表示新建一个jedis实例,也就说设置的maxActive无用;
maxWait:表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛JedisConnectionException;
testOnBorrow:获得一个jedis实例的时候是否检查连接可用性(ping());如果为true,则得到的jedis实例均是可用的
testOnReturn:return 一个jedis实例给pool时,是否检查连接可用性(ping());
testWhileIdle:如果为true,表示有一个idle object evitor线程对idle object进行扫描,如果validate失败,此object会被从pool中drop掉;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
timeBetweenEvictionRunsMillis:表示idle object evitor两次扫描之间要sleep的毫秒数;
numTestsPerEvictionRun:表示idle object evitor每次扫描的最多的对象数;
minEvictableIdleTimeMillis:表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
softMinEvictableIdleTimeMillis:在minEvictableIdleTimeMillis基础上,加入了至少minIdle个对象已经在pool里面了。如果为-1,evicted不会根据idle time驱逐任何对象。如果minEvictableIdleTimeMillis>0,则此项设置无意义,且只有在timeBetweenEvictionRunsMillis大于0时才有意义;
lifo:borrowObject返回对象时,是采用DEFAULT_LIFO(last in first out,即类似cache的最频繁使用队列),如果为False,则表示FIFO队列;
==================================================================================================================
其中JedisPoolConfig对一些参数的默认设置如下:
testWhileIdle=true
minEvictableIdleTimeMills=60000
timeBetweenEvictionRunsMillis=30000
numTestsPerEvictionRun=-1
6、链接集群
第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。
第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
第三步:打印结果
第四步:系统关闭前,关闭JedisCluster对象。
public void testJedisCluster() throws Exception { // 第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。 Set<HostAndPort> nodes = new HashSet<>(); nodes.add(new HostAndPort("192.168.25.153", 7001)); nodes.add(new HostAndPort("192.168.25.153", 7002)); nodes.add(new HostAndPort("192.168.25.153", 7003)); nodes.add(new HostAndPort("192.168.25.153", 7004)); nodes.add(new HostAndPort("192.168.25.153", 7005)); nodes.add(new HostAndPort("192.168.25.153", 7006)); JedisCluster jedisCluster = new JedisCluster(nodes); // 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。 jedisCluster.set("hello", "100"); String result = jedisCluster.get("hello"); // 第三步:打印结果 System.out.println(result); // 第四步:系统关闭前,关闭JedisCluster对象。 jedisCluster.close(); }
7、使用Redis向业务逻辑中添加缓存
1、接口封装:常用的操作redis的方法提取出一个接口,分别对应单机版和集群版创建两个实现类。
2、接口定义
public interface JedisClient { String set(String key, String value); String get(String key); Boolean exists(String key); Long expire(String key, int seconds); Long ttl(String key); Long incr(String key); Long hset(String key, String field, String value); String hget(String key, String field); Long hdel(String key, String... field); }
3、单机版实现
public class JedisClientPool implements JedisClient { @Autowired private JedisPool jedisPool; @Override public String set(String key, String value) { Jedis jedis = jedisPool.getResource(); String result = jedis.set(key, value); jedis.close(); return result; } @Override public String get(String key) { Jedis jedis = jedisPool.getResource(); String result = jedis.get(key); jedis.close(); return result; } @Override public Boolean exists(String key) { Jedis jedis = jedisPool.getResource(); Boolean result = jedis.exists(key); jedis.close(); return result; } @Override public Long expire(String key, int seconds) { Jedis jedis = jedisPool.getResource(); Long result = jedis.expire(key, seconds); jedis.close(); return result; } @Override public Long ttl(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.ttl(key); jedis.close(); return result; } @Override public Long incr(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.incr(key); jedis.close(); return result; } @Override public Long hset(String key, String field, String value) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hset(key, field, value); jedis.close(); return result; } @Override public String hget(String key, String field) { Jedis jedis = jedisPool.getResource(); String result = jedis.hget(key, field); jedis.close(); return result; } @Override public Long hdel(String key, String... field) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hdel(key, field); jedis.close(); return result; } }
4、集群版实现
package cn.e3mall.jedis; import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.JedisCluster; public class JedisClientCluster implements JedisClient { @Autowired private JedisCluster jedisCluster; @Override public String set(String key, String value) { return jedisCluster.set(key, value); } @Override public String get(String key) { return jedisCluster.get(key); } @Override public Boolean exists(String key) { return jedisCluster.exists(key); } @Override public Long expire(String key, int seconds) { return jedisCluster.expire(key, seconds); } @Override public Long ttl(String key) { return jedisCluster.ttl(key); } @Override public Long incr(String key) { return jedisCluster.incr(key); } @Override public Long hset(String key, String field, String value) { return jedisCluster.hset(key, field, value); } @Override public String hget(String key, String field) { return jedisCluster.hget(key, field); } @Override public Long hdel(String key, String... field) { return jedisCluster.hdel(key, field); } }
5、spring配置单机版
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd"> <!-- 配置单机版的连接 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="192.168.25.153"></constructor-arg> <constructor-arg name="port" value="6379"></constructor-arg> </bean> <bean id="jedisClientPool" class="cn.e3mall.jedis.JedisClientPool"/> </beans>
6、spring集群版配置
<!-- 集群版的配置 --> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </constructor-arg> </bean> <bean id="jedisClientCluster" class="cn.e3mall.jedis.JedisClientCluster"/>
注意:单机版和集群版不能共存,使用单机版时注释集群版的配置。使用集群版,把单机版注释。