标签:
本文主要描述redis3.0.5集群与spring集成,以及部分接口的封装。
环境:
- Spring 3.1.2
- Jedis 2.8.0
- JDK 1.6.045
一、 Spring配置
二、编写redis常用接口
import java.util.List;
/**
* @ClassName: SimpleCache
* @Description: 缓存接口
* @date 2016年4月20日 上午11:41:27
*
*/
public interface SimpleCache {
/**
* @Title: addObj
* @Description: 添加一个缓冲对象数据
* @param key 字符串的缓存key
* @param value 缓冲的缓存数据
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long addObj(String key, Object value) throws Exception;
/**
* @Title: add
* @Description: 缓存一个对象数据,并指定缓存过期时间
* @param key
* @param value
* @param seconds 过期时间
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long addObj(String key, Object value, int seconds) throws Exception;
/**
* @Title: updateObj
* @Description: 更新一个缓冲对象数据
* @param key 字符串的缓存key
* @param value 缓冲的缓存数据
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long updateObj(String key, Object value) throws Exception;
/**
* @Title: updateObj
* @Description: 更新一个缓存数据,并指定缓存过期时间
* @param key
* @param value
* @param seconds 过期时间
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long updateObj(String key, Object value, int seconds) throws Exception;
/**
* @Title: getObj
* @Description: 根据key获取到一直值
* @param key 字符串的缓存key
* @return 成功返回Object ,失败返回null
* @throws Exception
*/
Object getObj(String key) throws Exception;
/**
* @Title: addStr
* @Description: 添加一个缓冲数据,如果已存在则返回失败
* @param key 字符串的缓存key
* @param value 缓冲的缓存数据
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long addStr(String key, String value) throws Exception;
/**
* @Title: addStr
* @Description: 缓存一个数据,并指定缓存过期时间,如果已存在则返回失败
* @param key
* @param value
* @param seconds 单位:秒
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long addStr(String key, String value, int seconds) throws Exception;
/**
* @Title: appendStr
* @Description: 通过key向指定的value值追加值
* @param key 字符串的缓存key
* @param value 追加值
* @return 成功返回 添加后value的长度 失败 返回 添加的 value 的长度
* @throws Exception
*/
Long appendStr(String key, String value) throws Exception;
/**
* @Description 批量的设置key:value,可以一个,如果key已经存在则会失败,操作会回滚
* <p>example:</p>
* <p> obj.msetnx(new String[]{"key1","value1","key2","value2"})</p>
* @param keysvalues
* @return 返回成功更新的个数
* @throws Exception
*/
Long addMstr(String... keysvalues) throws Exception;
/**
* @Title: updateStr
* @Description: 更新一个缓冲数据
* @param key 字符串的缓存key
* @param value 缓冲的缓存数据
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long updateStr(String key, String value) throws Exception;
/**
* @Title: updateStr
* @Description: 更新一个缓存数据,并指定缓存过期时间
* @param key
* @param value
* @param seconds
* @return 成功返回1 如果存在返回 0
* @throws Exception
*/
Long updateStr(String key, String value, int seconds) throws Exception;
/**
* @Description 批量的设置key:value,若是没有key则插入
* <p>example:</p>
* <p> obj.msetnx(new String[]{"key1","value1","key2","value2"})</p>
* @param keysvalues
* @return 返回更新成功的个数
* @throws Exception
*/
Long updateMstr(String... keysvalues) throws Exception;
/**
* @Title: getStr
* @Description: 根据key获取到一直值
* @param key 字符串的缓存key
* @return 成功返回value 失败返回null
* @throws Exception
*/
String getStr(String key) throws Exception;
/**
* @Title: getStrs
* @Description: 根据key获取到一直值
* @param key 字符串的缓存key
* @return
* @throws Exception
*/
List<String> getMstr(String... keys) throws Exception;
/**
* @Title: delete
* @Description: 删除一个数据问题,不存在则忽略
* @param key 字符串的缓存key
* @return
* @throws Exception
*/
Long deleteKey(String key) throws Exception;
/**
* @Title: delete
* @Description: 删除多个数据问题
* @param key 字符串的缓存key
* @return 返回删除成功的个数
* @throws Exception
*/
Long deleteKeys(String...keys) throws Exception;
/**
* @Title: exists
* @Description: 判断指定key是否在缓存中已经存在
* @param key 字符串的缓存key
* @return true OR false
* @throws Exception
*/
Boolean exists(String key) throws Exception;
/**
* @Title: updateExpireTime
* @Description: 更新过期时间
* @param key 字符串的缓存key
* @param seconds 过期时间
* @return 成功返回1,失败返回0
* @throws Exception
*/
Long updateExpireTime(String key, int seconds) throws Exception;
/**
* @Description 如果该key不存在,它被设置为0执行操作之前。如果key包含了错误类型的值或包含不能被表示为整数,字符串,则返回错误
* @param key
* @return 加值后的结果
* @throws Exception
*/
Long increment(String key) throws Exception;
/**
* @Description 通过key给指定的value加值,如果key不存在,则这时value为该值
* @param key
* @param integer
* @return
* @throws Exception
*/
Long incrementBy(String key,Long integer) throws Exception;
/**
* @Description 对key的值做减减操作,如果key不存在,则设置key为-1
* @param key
* @return
*/
Long decrement(String key)throws Exception;
/**
* @Description 减去指定的值
* @param key
* @param integer
* @return
* @throws Exception
*/
Long decrementBy(String key,Long integer) throws Exception;
/**
* @Description 通过key获取value值的长度
* @param key
* @return 失败返回null
* @throws Exception
*/
Long valueStrlen(String key) throws Exception;
}
三、接口实现,需要序列化JsonUtil
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import redis.clients.jedis.JedisCluster;
public class SmartCache implements SimpleCache {
protected final Logger logger = Logger.getLogger("dao");
protected JedisCluster jedisCluster;
public JedisCluster getJedisCluster() {
return jedisCluster;
}
public void setJedisCluster(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster;
}
@Override
public Long addObj(String key, Object value) throws Exception {
Long res = 0L;
JsonUtil.DEFAULT_JSON = JsonUtil.FAST_JSON;
String objectToJson = JsonUtil.toJSONString(value);
res = jedisCluster.setnx(key, objectToJson);
return res;
}
@Override
public Long addObj(String key, Object value, int seconds) throws Exception {
Long res = 0L;
JsonUtil.DEFAULT_JSON = JsonUtil.FAST_JSON;
String objectToJson = JsonUtil.toJSONString(value);
String temp = jedisCluster.setex(key, seconds, objectToJson);
temp = jedisCluster.setex(key, seconds, objectToJson);
if (temp.equals("OK")) {
res = 1L;
}
return res;
}
@Override
public Long updateObj(String key, Object value) throws Exception {
Long res = 0L;
boolean exists = jedisCluster.exists(key);
if (exists) {
JsonUtil.DEFAULT_JSON = JsonUtil.FAST_JSON;
String objectToJson = JsonUtil.toJSONString(value);
String temp = jedisCluster.set(key, objectToJson);
if (temp.equals("OK")) {
res = 1L;
}
}
return res;
}
@Override
public Long updateObj(String key, Object value, int seconds) throws Exception {
Long res = 0L;
boolean exists = jedisCluster.exists(key);
if (exists) {
JsonUtil.DEFAULT_JSON = JsonUtil.FAST_JSON;
String objectToJson = JsonUtil.toJSONString(value);
jedisCluster.expire(key, seconds);
String temp = jedisCluster.set(key, objectToJson);
if (temp.equals("OK")) {
res = 1L;
}
}
return res;
}
@Override
public Object getObj(String key) throws Exception {
String res = null;
res = jedisCluster.get(key);
Object object = JsonUtil.toBean(res, Object.class);
return object;
}
@Override
public Long addStr(String key, String value) throws Exception {
Long res = 0L;
res = jedisCluster.setnx(key, value);
return res;
}
@Override
public Long addStr(String key, String value, int seconds) throws Exception {
Long res = 0L;
String temp = jedisCluster.setex(key, seconds, value);
if (temp.equals("OK")) {
res = 1L;
}
return res;
}
@Override
public Long appendStr(String key, String value) throws Exception {
Long res = 0L;
res = jedisCluster.append(key, value);
return res;
}
@Override
public Long addMstr(String... keysvalues) throws Exception {
Long rescount = 0L;
if (keysvalues.length % 2 == 0) {
int i = 0;
for (i = 0; i < keysvalues.length;) {
Long temp = this.addStr(keysvalues[i], keysvalues[i + 1]);
if (temp == 1L) {
rescount++;
}
i = i + 2;
}
}
return rescount;
}
@Override
public Long updateStr(String key, String value) throws Exception {
Long res = 0L;
boolean exists = jedisCluster.exists(key);
if (exists) {
String temp = jedisCluster.set(key, value);
if (temp.equals("OK")) {
res = 1L;
}
}
return res;
}
@Override
public Long updateStr(String key, String value, int seconds) throws Exception {
Long res = 0L;
boolean exists = jedisCluster.exists(key);
if (exists) {
jedisCluster.expire(key, seconds);
String temp = jedisCluster.set(key, value);
if (temp.equals("OK")) {
res = 1L;
}
}
return res;
}
@Override
public Long updateMstr(String... keysvalues) throws Exception {
Long rescount = 0L;
if (keysvalues.length % 2 == 0) {
int i = 0;
for (i = 0; i < keysvalues.length;) {
Long temp = this.updateStr(keysvalues[i], keysvalues[i + 1]);
if (temp == 1L) {
rescount++;
}
i = i + 2;
}
}
return rescount;
}
@Override
public String getStr(String key) throws Exception {
String res = null;
res = jedisCluster.get(key);
return res;
}
@Override
public List<String> getMstr(String... keys) throws Exception {
List<String> values = new ArrayList<String>();
for (int i = 0; i < keys.length; i++) {
String value = "";
if (this.exists(keys[i])) {
value = this.getStr(keys[i]);
if (!value.equals(null)) {
values.add(value);
}
}
}
return values;
}
@Override
public Long deleteKey(String key) throws Exception {
return jedisCluster.del(key);
}
@Override
public Long deleteKeys(String... keys) throws Exception {
Long rescount = 0L;
for (int i = 0; i < keys.length; i++) {
Long temp = this.deleteKey(keys[i]);
if (temp == 1L) {
rescount++;
}
}
return rescount;
}
@Override
public Boolean exists(String key) throws Exception {
return jedisCluster.exists(key);
}
@Override
public Long updateExpireTime(String key, int seconds) throws Exception {
Long res = 0L;
boolean exists = jedisCluster.exists(key);
if (exists) {
res = jedisCluster.expire(key, seconds);
}
return res;
}
@Override
public Long increment(String key) throws Exception {
Long res = 0L;
res = jedisCluster.incr(key);
return res;
}
@Override
public Long incrementBy(String key, Long integer) throws Exception {
Long res = 0L;
res = jedisCluster.incrBy(key, integer);
return res;
}
@Override
public Long decrement(String key) throws Exception {
Long res = 0L;
res = jedisCluster.decr(key);
return res;
}
@Override
public Long decrementBy(String key, Long integer) throws Exception {
Long res = 0L;
res = jedisCluster.decrBy(key, integer);
return res;
}
@Override
public Long valueStrlen(String key) throws Exception {
Long res = 0L;
res = jedisCluster.strlen(key);
return res;
}
}
标签:
原文地址:http://blog.csdn.net/omaverick1/article/details/51324436