如何在Spring框架中使用redis?

首先需要在pom.xml中导入redis的相关依赖

<dependency>

      <groupId>redis.clients</groupId>

      <artifactId>jedis</artifactId>

      <version>2.1.0</version>

</dependency>

然后在applicationContext.xml中添加redis相关的配置

<!--redis 配置 开始-->

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

        <property name="maxActive" value="300"/>

        <property name="maxIdle" value="100"/>

        <property name="maxWait" value="1000"/>

        <property name="testOnBorrow" value="true"/>

    </bean>

    <!-- Config poolConfig, String host, int port, int timeout, String password, int database-->

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">

        <constructor-arg ref="jedisPoolConfig"/>

        <constructor-arg value="127.0.0.1"/>

        <constructor-arg value="6379"/>

        <constructor-arg value="3000"/>

        <constructor-arg value="123456"/>

        <constructor-arg value="0"/>

    </bean>

    <bean id="redisAPI" class="com.xc.util.RedisAPI">

        <property name="jedisPool" ref="jedisPool"/>

    </bean>

最后创建redis的工具类

public class RedisAPI {
public static JedisPool jedisPool;
    
    public JedisPool getJedisPool() {
        return jedisPool;
    }
    public void setJedisPool(JedisPool jedisPool) {
        RedisAPI.jedisPool = jedisPool;
    }
    /**
     * set key and value to redis
     * @param key
     * @param value
     * @return
     */
    public static boolean set(String key,String value){
        try{
            Jedis jedis = jedisPool.getResource();
            jedis.set(key, value);
            return true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * set key and value to redis
     * @param key
     * @param seconds 有效期
     * @param value
     * @return
     */
    public static boolean set(String key,int seconds,String value){
        try{
            Jedis jedis = jedisPool.getResource();
            jedis.setex(key, seconds, value);
            return true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * 判断某个key是否存在
     * @param key
     * @return
     */
    public boolean exist(String key){
        try{
            Jedis jedis = jedisPool.getResource();
            return jedis.exists(key);
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * 返还到连接池
     * @param pool
     * @param redis
     */
    public static void returnResource(JedisPool pool,Jedis redis){
        if(redis != null){
            pool.returnResource(redis);
        }
    }
    
    /**
     * 获取数据
     * @param key
     * @return
     */
    public String get(String key){
        String value = null;
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //返还到连接池
            returnResource(jedisPool, jedis);
        }
        
        return value;
    }
    
    /**
     * 查询key的有效期,当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以秒为单位,返回 key 的剩余生存时间。
     * 注意:在 Redis 2.8 以前,当 key 不存在,或者 key 没有设置剩余生存时间时,命令都返回 -1 。
     * @param key
     * @return 剩余多少秒
     */
    public Long ttl(String key){
        try{
            Jedis jedis = jedisPool.getResource();
            return jedis.ttl(key);
        }catch(Exception e){
            e.printStackTrace();
        }
        return (long) -2;
    }
    
    /**
     * 删除
     * @param key
     */
    public void delete(String key){
        try{
            Jedis jedis = jedisPool.getResource();
            jedis.del(key);
        }catch(Exception e){
            e.printStackTrace();
        }        
    }
}

在需要的时候调用对应的方法,就可以进行对redis中数据的操作

posted @ 2019-09-10 16:30  Java精进之路  阅读(646)  评论(0编辑  收藏  举报