使用Jedis jar操作Redis

1、配置redis.conf文件,修改

  

2、建java工程,加入 jedis jar包

  

3、代码示例:

 package com.ntjr.redis;

 import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.junit.Before;
import org.junit.Test; import redis.clients.jedis.Jedis; public class TestApi {
private Jedis jedis; /**
* 连接192.168.15.128:6379 Redis
*/
@Before
public void before() {
jedis = new Jedis("192.168.15.128", 6379);
} /**
* 测试set get 及 过期时间,返回-1代表永不过期
*/
@Test
public void testSetKey() {
jedis.set("k1", "v1");
jedis.set("k2", "v2");
jedis.set("k3", "v3");
System.out.println(jedis.get("k3"));
System.out.println(jedis.ttl("k1"));
} /**
* 获取所有的key
*/
@Test
public void testGet() {
Set<String> keys = jedis.keys("*");
for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
System.out.println(key);
}
} /**
* 是否存在k2
*/
@Test
public void testExists() {
System.out.println(jedis.exists("k2"));
} /**
* string 追加
*/
@Test
public void testAppend() {
jedis.append("k1", "myredis");
System.out.println(jedis.get("k1"));
} /**
* 设置多个键值对
*/
@Test
public void testMSET() {
jedis.mset("str1", "v1", "str2", "v2", "str3", "v3");
System.out.println(jedis.mget("str1", "str2", "str3"));
} /**
* 测试为list设置值,获取list集合
*/
@Test
public void testLPUSH() {
jedis.lpush("mylist", "v1", "v2", "v3", "v4", "v5");
List<String> myList = jedis.lrange("mylist", 0, -1);
for (String element : myList) {
System.out.println(element);
}
} /**
* Set集合的添加、遍历、删除
*/
@Test
public void testSet() {
jedis.sadd("orders", "jd001");
jedis.sadd("orders", "jd002");
jedis.sadd("orders", "jd003");
Set<String> orders = jedis.smembers("orders");
for (Iterator<String> iterator = orders.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
jedis.srem("orders", "jd002");
System.out.println(jedis.smembers("orders").size());
} /**
* Map集合操作,获取多个值
*/
@Test
public void testHash() {
jedis.hset("hash1", "uername", "lisi");
System.out.println(jedis.hget("hash1", "uername"));
Map<String, String> map = new HashMap<>();
map.put("telphone", "13811814763");
map.put("address", "atguigu");
map.put("email", "abc@163.com");
jedis.hmset("hash1", map);
List<String> result = jedis.hmget("hash1", "telphone", "email");
for (String element : result) {
System.out.println(element);
}
} /**
* Zset设置,为每个数据设置 score
*/
@Test
public void testZSET() {
jedis.zadd("zset01", 60d, "v1");
jedis.zadd("zset01", 70d, "v2");
jedis.zadd("zset01", 80d, "v3");
jedis.zadd("zset01", 90d, "v4");
Set<String> zset = jedis.zrange("zset01", 0, -1);
for(Iterator<String> iterator = zset.iterator();iterator.hasNext(); ){
System.out.println(iterator.next());
}
} }

4、使用JedisPool连接池

 package com.ntjr.redis;

 import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class JedisPoolUtils { private static volatile JedisPool jedisPool = null; private JedisPoolUtils() {
} public static JedisPool getJedisPoolInstance() {
synchronized (JedisPoolUtils.class) {
if (jedisPool == null) {
synchronized (JedisPoolUtils.class) {
if (jedisPool == null) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxActive(1000);
poolConfig.setMaxIdle(30);
poolConfig.setMaxWait(100*1000);
poolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(poolConfig, "192.168.15.128", 6379);
}
}
}
}
return jedisPool;
} public static void release(JedisPool jedisPool,Jedis jedis){
if(jedis!=null){
jedisPool.returnResourceObject(jedis);
}
} }

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"/>

注意:单机版和集群版不能共存,使用单机版时注释集群版的配置。使用集群版,把单机版注释。