Java中使用jedis操作redis

 

jedis 封装了 redis 原有的操作命令,使用起来很简单。

 

只需要添加依赖即可操作:

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>

 

Java 中的代码:

Jedis jedis = new Jedis(LOCALHOST,PORT);// 设置地址和端口
        jedis.auth(PASSWORD);// 如果 redis 服务器配置了需要密码,此处必须设置
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存储集合到redis,并取出</span>
    jedis.lpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mylist</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">admin</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">tom</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">jack</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);

    System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.print(jedis.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mylist</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 128, 1)">0</span>,-<span style="color: rgba(128, 0, 128, 1)">1</span>));</pre>

 

客户端插入后,获取后打印输出

 

 

redis 服务器端查看:

 

 

 

使用流水线 Pipeline 模式测试向 redis 服务器每秒可写多少数据

/**
     * 测试 redis 每秒可写入数据
     */
    @Test
    public void test01()
    {
        //参数 1:主机地址,参数 2:端口号
        Jedis jedis = new Jedis(LOCALHOST,PORT);//连接 redis
        //如果服务器的配置文件设置了需要密码验证
        jedis.auth(PASSWORD);//输入密码验证
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开启流水线</span>
    Pipeline pipeline =<span style="color: rgba(0, 0, 0, 1)"> jedis.pipelined();

    </span><span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
    {
        </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
        </span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
        {
            </span><span style="color: rgba(0, 0, 255, 1)">long</span> end =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当大于等于1秒时,结束操作</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span>(end-start&gt;=<span style="color: rgba(128, 0, 128, 1)">1000</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
            }
            i</span>++<span style="color: rgba(0, 0, 0, 1)">;
            pipeline.</span><span style="color: rgba(0, 0, 255, 1)">set</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">"</span>+i,i+<span style="color: rgba(128, 0, 0, 1)">""</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用流水线方式向redis数据库插入数据
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis.set("test"+i,i+"");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">向redis数据库插入数据</span>

}
}
finally {
jedis.close();
//关闭连接
}

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印1秒内对redis操作的次数</span>
    System.<span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">redis每秒操作</span><span style="color: rgba(128, 0, 0, 1)">"</span>+i+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">次</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);

}</span></pre>

 

 

可以看到,每秒可写 142611 次,redis 服务器查看,数据插入成功。

 

 

 

 

 

 

下面在 springMVC 中整合 redis,使用 redisUtils 工具类操作 redis 更简单

 

第一步:添加依赖:

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>

 

第二步:添加属性文件:redis.properties

redis.host = xxx
redis.port = 6379
redis.password = xxx
redis.timeout = 5000
redis.maxTotal = 100
redis.maxIdle = 20
redis.minIdle = 5

 

第三步:添加 xml 配置文件 spring-redis.xml(在 springmvc 中集成的)

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
&lt;!--加载属性文件--&gt;
&lt;context:property-placeholder location=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">classpath:redis.properties</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;

&lt;!--Spring整合配置,连接池配置--&gt;
&lt;bean id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">poolConfig</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">redis.clients.jedis.JedisPoolConfig</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
    &lt;property name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">maxTotal</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${redis.maxTotal}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
    &lt;property name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">maxIdle</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${redis.maxIdle}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
    &lt;property name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">minIdle</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${redis.minIdle}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
&lt;/bean&gt;

&lt;bean id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">jedisPool</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">redis.clients.jedis.JedisPool</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
    &lt;constructor-arg name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">poolConfig</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">ref</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">poolConfig</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
    &lt;constructor-arg name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">host</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${redis.host}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
    &lt;constructor-arg name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">port</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${redis.port}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
    &lt;constructor-arg name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">timeout</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${redis.timeout}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
    &lt;constructor-arg name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${redis.password}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
&lt;/bean&gt;

&lt;bean <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">com.supin51.redis.test.SpringBeanHolder</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;

</beans>

 

第四步:添加 SpringBeanHolder 类加载 bean

 

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/

  • @Author:ShaoJiang

  • @description:

  • @Date: created in 下午 10:49 2019/2/12

  • @Modified By:
    */
    public class SpringBeanHolder implements ApplicationContextAware {

    private static ApplicationContext ac;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    ac
    = applicationContext;
    }

    public static Object getBean(String beanName)
    {
    return ac.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz)
    {
    return ac.getBean(clazz);
    }
    }

 

第五步:添加 redisUtils 工具类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/

  • @Author:ShaoJiang

  • @description:

  • @Date: created in 下午 10:48 2019/2/12

  • @Modified By:
    */
    public class RedisUtils {

    private static JedisPool jedisPool = null;

    private static ApplicationContext context = null;

    static
    {
    //加载 xml 文件
    context = new ClassPathXmlApplicationContext("classpath:spring-redis.xml");
    //获取 jedis 连接池
    jedisPool = (JedisPool)SpringBeanHolder.getBean("jedisPool");
    }

    public static void set(String key ,String value)
    {
    //
    Jedis jedis = null;
    try {
    if(jedis==null){
    jedis
    = jedisPool.getResource();
    jedis.
    set(key,value);
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    jedis.close();
    }
    }

    public static String get(String key)
    {
    Jedis jedis
    = null;
    try {
    if(jedis==null){
    jedis
    = jedisPool.getResource();
    return jedis.get(key);
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    jedis.close();
    }
    return null;
    }

}

 

 

最后测试:

 

/**
     * 使用 redisUtils 工具类操作 redis
     */
    @Test
    public void test03()
    {
        RedisUtils.set("name","tom");
        System.out.println(RedisUtils.get("name"));}

 

客户端测试类,插入数据后,获取输出。

 

 

 

redis 服务器查看: