redis 老版本

来源:互联网 发布:怎么自己做软件 编辑:程序博客网 时间:2024/04/28 11:23

maven引入

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <description>redis配置</description>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="jedisConfig" />
        <constructor-arg index="1" value="${redis.host}" />
        <constructor-arg index="2" value="${redis.port}" />
        <constructor-arg index="3" value="${redis.timeout}" />
    </bean>
    <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive" value="${redis.maxActive}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="maxWait" value="${redis.maxWait}" />
    </bean>
</beans>

代碼使用

import com.tiefan.fbs.fsp.base.core.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

import javax.annotation.Resource;
import java.util.Set;

/**
 * CacheUtil
 */
@Service
public class CacheUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(CacheUtil.class);
    @Resource
    private JedisPool jedisPool;

    /**
     * 添加
     *
     * @param key key
     * @param value value
     * @return Long
     */
    public Long sadd(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.sadd(key, value);
        } catch (JedisConnectionException e) {
            LOGGER.error("sadd({},{})", new Object[]{key, value});
            LOGGER.error("redis 异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
        return null;
    }
    public Long incr(String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.incr(key);
        } catch (JedisConnectionException e) {
            LOGGER.error("incr({},{})", new Object[]{key});
            LOGGER.error("redis 异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
        return null;
    }
    /**
     * 查询集合
     *
     * @param key key
     * @return Set
     */
    public Set<String> smembers(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.smembers(key);
        } catch (JedisConnectionException e) {
            LOGGER.error("smembers({},{},{})", new Object[]{key});
            LOGGER.error("redis 异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
        return null;
    }

    /**
     * 删除集合
     *
     * @param key key
     * @param value value
     * @return Long
     */
    public Long srem(String key, String[] value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.srem(key, value);
        } catch (JedisConnectionException e) {
            LOGGER.error("smembers({},{},{})", new Object[]{key});
            LOGGER.error("redis 异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
        return null;
    }

    /**
     * 校验key在redis是否存在
     *
     * @param key 缓存的key
     * @return true is exists,if key is an empty string,return true
     */
    public boolean existsCache(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (JedisConnectionException e) {
            LOGGER.error("getCache({})", new Object[]{key});
            LOGGER.error("redis 异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
        return false;
    }

    /**
     * * 得到key在redis的value(String字符串),然后转化为T对象
     *
     * @param key 缓存的key
     * @param cls 欲转换的类
     * @param <T> t
     * @return  t
     */
    public <T> T getCache(String key, Class<T> cls) {
        try {
            return JsonUtil.toBean(this.getCache(key), cls);
        } catch (Exception e) {
            LOGGER.error("redis 异常 ", e);
        }
        return null;
    }

    /**
     * 得到key在redis的value(String字符串)
     *
     * @param key 缓存的key
     * @return String
     */
    public String getCache(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } catch (JedisConnectionException e) {
            LOGGER.error("getCache({})", new Object[]{key});
            LOGGER.error("redis获取缓存异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
        return null;
    }

    /**
     * 此方法描述的是:设置缓存
     *
     * @param key    键
     * @param value  值
     * @param expire 有效时间
     */
    public void setCache(String key, Object value, int expire) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.setex(key, expire, JsonUtil.toString(value));
            // jedis.set(key, value.toString());
            //jedis.expire(key, expire);
        } catch (JedisConnectionException e) {
            LOGGER.error("setCache({},{},{})", new Object[]{key, value, expire});
            LOGGER.error("redis 设置缓存异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
    }

    /**
     * 此方法描述的是:设置缓存
     *
     * @param key   键
     * @param value 值
     */
    public void setCache(String key, Object value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, JsonUtil.toString(value));
        } catch (JedisConnectionException e) {
            LOGGER.error("setCache({},{})", new Object[]{key, value});
            LOGGER.error("redis 设置缓存异常", e);
            broken(jedis);
        } finally {
            // 释放jedis资源
            release(jedis);
        }
    }

    /**
     * 此方法描述的是:缓存删除
     *
     * @param key key
     */
    public void delCache(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (JedisConnectionException e) {
            LOGGER.error("setCache({})", new Object[]{key});
            LOGGER.error("redis 缓存删除异常", e);
            broken(jedis);
        } finally {
            release(jedis);
        }
    }

    /**
     * 连接释放
     *
     * @param jedis jedis
     */
    private void release(Jedis jedis) {
        if (jedis != null) {
            try {
                jedisPool.returnResource(jedis);
            } catch (Exception e) {
                LOGGER.error("redis 连接释放异常", e);
            }
        }
    }

    /**
     * 连接销毁
     *
     * @param jedis jedis
     */
    private void broken(Jedis jedis) {
        if (jedis != null) {
            try {
                jedisPool.returnBrokenResource(jedis);
            } catch (Exception e) {
                LOGGER.error("redis 连接销毁异常", e);
            }
        }
    }

}


原创粉丝点击