spring + Jedis 的方式使用缓存

spring + Jedis 的方式使用缓存

  1. jar 包

    <dependency>
       <groupId>redis.clientsgroupId>
       <artifactId>jedisartifactId>
       <version>2.8.1version>
    dependency>
  2. 配置文件

    config.properties

    redis.host=127.0.0.1
    redis.port=6379
    redis.password=foobared
    redis.timeout=60000
    redis.database=0
    redis.pool.maxActive=300
    redis.pool.maxIdle=100
    redis.pool.maxWait=2000

    spring-jedis.xml

    
    <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">
    
       <context:property-placeholder location="classpath:config.properties"/>
    
       <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
           <property name="maxTotal" value="${redis.pool.maxActive}" />
           <property name="maxIdle" value="${redis.pool.maxIdle}" />
           <property name="maxWaitMillis" value="${redis.pool.maxWait}" />
           <property name="testOnBorrow" value="false" />
           <property name="testOnReturn" value="false" />
       bean>
    
       <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
           <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
           <constructor-arg name="host" value="${redis.host}" />
           <constructor-arg name="port" value="${redis.port}" type="int" />
           <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
           <constructor-arg name="password" value="${redis.password}" />
           <constructor-arg name="database" value="${redis.database}" type="int" />
       bean>
    beans>
    

  3. 测试

    package com.susq.jedis;
    
    import lombok.extern.slf4j.Slf4j;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    /**
    * @author susq
    * @since 2018-05-04-10:17
    */
    @Slf4j
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(value = "classpath:spring-jedis.xml")
    public class JedisTest {
    
       @Autowired
       private JedisPool jedisPool;
    
       @Test
       public void setTest() {
           Jedis jedis = jedisPool.getResource();
           String codeReply = jedis.set("key2","hello jedis one");
           jedis.close();
           log.info(codeReply);
       }
    
       @Test
       public void getTest() {
           Jedis jedis = jedisPool.getResource();
           String value = jedis.get("key2");
           log.info(value);
       }
    
       @Test
       public void del() {
           Jedis jedis = jedisPool.getResource();
           jedis.set("key2","hello jedis one");
           log.info(jedis.get("key2"));
           jedis.del("key2");
           log.info(jedis.get("key2"));
       }
    }

    参考: JedisAPI

你可能感兴趣的:(缓存)