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);
}
}
}
}
- redis 老版本
- redis老版本中的expire问题及新版本的安装测试
- 下载Eclipse 老版本
- 老版本Ubuntu源
- 老版本的jira
- 老版本JDK下载
- 安装老版本flash
- openssl老版本编译
- Eclipse老版本下载地址
- 下载老版本eclipse地址
- 判断老版本iOS SDK
- ORACLE老版本下载地址
- 判断老版本iOS SDK
- 判断老版本iOS SDK
- 新老版本的更换
- 老版本ubuntu更新原
- xcode老版本 照片问题
- 老版本xcode下载地址
- java-io流
- 深入理解Java中的String
- unity使用share_SDK接微信分享和登陆的总结Android
- 持久化存储解决方案MagicalRecord使用入门
- 【多线程简单实例】CreateThread实现
- redis 老版本
- C#中枚举值对应的值相同时的情况
- 洛谷P1902 刺杀大使(二分答案+bfs验证)
- git命令之clone
- java long转换为int 类型 注意事项
- vs2013 SQL server添加约束的语法
- 学习数组与指针
- 朴素贝叶斯
- 模板(一)----->函数模板