redis3.0.5集群与spring集成
2017-08-31 15:02
381 查看
Redis3.0.5 Jedis封装
本文主要描述redis3.0.5集群与spring集成,以及部分接口的封装。
环境:
-
spring 3.1.2
- Jedis 2.8.0
- JDK 1.6.045
一、 Spring配置
二、编写Redis常用接口
public interface SimpleCache { /** * 添加一个缓冲数据,如果已存在则返回失败 * @param key 字符串的缓存key * @param value 缓冲的缓存数据 * @return 成功返回true 如果存在返回 false * @throws Exception */ boolean addStr(String key, String value) throws Exception; /** * 缓存一个数据,并指定缓存过期时间,如果添加的时候key已存在或者设置过期时间的时候key不存在则返回失败 * @param key * @param value * @param seconds 单位:秒 * @return 成功返回true 如果存在返回 false * @throws Exception */ boolean addStr(String key, String value, int seconds) throws Exception; /** * 根据key获取记录 * @param key 字符串的缓存key * @return 成功返回value 失败返回null * @throws Exception */ String getStr(String key) throws Exception; /** * 添加一个缓冲对象,如果已存在则返回失败 * @param key 对象的缓存key * @param value 缓冲的缓存对象 * @return 成功返回true 如果存在返回 false * @throws Exception */ public boolean addObject(String key, Object value) throws Exception; /** * 缓存一个对象,并指定缓存过期时间,如果添加的时候key已存在或者设置过期时间的时候key不存在则返回失败 * @param key * @param value * @param seconds 单位:秒 * @return 成功返回true 如果存在返回 false * @throws Exception */ public boolean addObject(String key, Object value, int seconds) throws Exception; /** * 根据key获取对象 * @param key 字符串的缓存key * @return 成功返回value 失败返回null * @throws Exception */ public <T> T getObject(String key, Class<T> valueType) throws Exception; /** * 通过key向指定的value值追加值 * @param key 字符串的缓存key * @param value 追加值 * @return 成功返回添加后value的长度,失败返回添加的 value的长度 * @throws Exception */ Long appendStr(String key, String value) throws Exception; /** * 批量的设置key:value,如果key存在,则该记录添加失败 * @param keysvalues * @return 返回成功更新的个数 * @throws Exception */ Long addMstr(String... keysvalues) throws Exception; /** * 更新一个缓冲数据 * @param key 字符串的缓存key * @param value 缓冲的缓存数据 * @return 不存在则创建,成功返回true 如果存在覆盖返回true,失败返回false * @throws Exception */ boolean updateStr(String key, String value) throws Exception; /** * 更新一个缓存数据,并指定缓存过期时间 * @param key * @param value * @param seconds * @return 不存在则创建,成功返回true 如果存在覆盖返回true,失败返回false * @throws Exception */ boolean updateStr(String key, String value, int seconds) throws Exception; /** * 批量的设置key:value,若是没有key则插入 * @param keysvalues * @return 返回更新成功的个数 * @throws Exception */ Long updateMstr(String... keysvalues) throws Exception; /** * 批量获取记录,如果key不存在,则返回null * @param key 字符串的缓存key * @return value列表 * @throws Exception */ List<String> getMstr(String... keys) throws Exception; /** * 删除一个数据问题,不存在则忽略 * @param key 字符串的缓存key * @return * @throws Exception */ boolean deleteKey(String key) throws Exception; /** * 删除多个数据问题 * @param key 字符串的缓存key * @return 返回删除成功的个数 * @throws Exception */ Long deleteKeys(String...keys) throws Exception; /** * 判断指定key是否在缓存中已经存在 * @param key 字符串的缓存key * @return true OR false * @throws Exception */ boolean exists(String key) throws Exception; /** * 更新过期时间,如果key不存在返回false * @param key 字符串的缓存key * @param seconds 过期时间 * @return 成功返回1,失败返回0 * @throws Exception */ boolean updateExpireTime(String key, int seconds) throws Exception; /** * 如果该key不存在,它被设置为0执行操作之前。如果key包含了错误类型的值或包含不能被表示为整数,字符串,则返回错误 * @param key * @return 加值后的结果 * @throws Exception */ Long increment(String key) throws Exception; /** * 通过key给指定的value加值,如果key不存在,则这时value为该值 * @param key * @param integer * @return * @throws Exception */ Long incrementBy(String key,Long integer) throws Exception; /** * 对key的值做减减操作,如果key不存在,则设置key为-1 * @param key * @return */ Long decrement(String key)throws Exception; /** * 减去指定的值如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECRBY 操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 * @param key * @param integer * @return * @throws Exception */ Long decrementBy(String key,Long integer) throws Exception; /** * 通过key获取value值的长度 * @param key * @return 失败返回null * @throws Exception */ Long valueStrlen(String key) throws Exception; /** * 通过key向指定的set中添加value * @param key * @param members 可以是一个String 也可以是一个String数组 * @return 添加成功的个数 */ Long addSet(String key,String...members) throws Exception; /** * 通过key删除set中对应的value值 * @param key * @param members 可以是一个String 也可以是一个String数组 * @return 删除的个数 */ Long deleteSet(String key,String...members) throws Exception; Set<String> getSetMembers(String key) throws Exception; /** * 通过key向list尾部添加字符串 * @param key * @param strs 可以使一个string 也可以使string数组 * @return 返回list的value个数 */ Long addList(String key ,String...strs) throws Exception; /** * 通过key获取list指定下标位置的value * 如果start 为 0 end 为 -1 则返回全部的list中的value * @param key * @param start * @param end * @return */ List<String> getListMembers(String key,long start,long end) throws Exception; /** * 通过key返回list的长度 * @param key * @return */ Long getListLen(String key) throws Exception; /** * @Description 缓存二进制数据,并指定缓存过期时间 * @param key * @param value * @param seconds * 单位:秒 * @return 成功返回true 如果存在返回 false * @throws Exception */ boolean addBytes(byte[] key, byte[] value) throws Exception; /** * @Description 缓存二进制数据,并指定缓存过期时间,如果添加的时候key已存在或者设置过期时间的时候key不存在则返回失败 * @param key * @param value * @param seconds * 单位:秒 * @return 成功返回true 如果存在返回 false * @throws Exception */ boolean addBytes(byte[] key, byte[] value, int seconds) throws Exception; /** * @Description 根据key获取二进制数据 * @param key * 二进制 * @return * @throws Exception */ byte[] getBytes(byte[] key) throws Exception; /** * @Description 更新过期时间 * @param key * 二进制 * @param seconds * 过期时间 * @return 成功返回true,失败返回false * @throws Exception */ boolean updateExpireTime(byte[] key, int seconds) throws Exception; /** * @Description 通过key删除set中对应的value值 * @param key * @return 成功返回true,失败返回false * @throws Exception */ boolean deleteKey(byte[] key) throws Exception; }1
2
3
4
5
三、接口实现,需要序列化JsonUtil
public class SmartCacheDAO implements SimpleCache { protected final Loggerx logger = Loggerx.getLogger("dao"); protected JedisCluster jedisCluster; public JedisCluster getJedisCluster() { return jedisCluster; } public void setJedisCluster(JedisCluster jedisCluster) { this.jedisCluster = jedisCluster; } @Override public boolean addStr(String key, String value) throws Exception { Long res = jedisCluster.setnx(key, value); return 1L == res; } @Override public boolean addStr(String key, String value, int seconds) throws Exception { Long res = jedisCluster.setnx(key, value); if (1L == res) { if (jedisCluster.expire(key, seconds) != 1L) { if (jedisCluster.del(key) != 1L) { throw new Exception("redis delete key failed, key:" + key); } return false; } return true; } else { return false; } } @Override public boolean addObject(String key, Object value) throws Exception { Long res = jedisCluster.setnx(key, JsonUtil.toJSONString(value)); return 1L == res; } @Override public boolean addObject(String key, Object value, int seconds) throws Exception { return this.addStr(key, JsonUtil.toJSONString(value), seconds); } @Override public <T> T getObject(String key, Class<T> valueType) throws Exception { String res = jedisCluster.get(key); T resObj = JsonUtil.toBean(res, valueType); return resObj; } @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) { for (int i = 0; i < keysvalues.length; i += 2) { boolean temp = this.addStr(keysvalues[i], keysvalues[i + 1]); if (temp) { rescount++; } } } else { throw new IllegalArgumentException("arguments invalid!"); } return rescount; } @Override public boolean updateStr(String key, String value) throws Exception { String temp = jedisCluster.set(key, value); return "OK".equals(temp); } @Override public boolean updateStr(String key, String value, int seconds) throws Exception { String res = jedisCluster.setex(key, seconds, value); return "OK".equals(res); } @Override public Long updateMstr(String... keysvalues) throws Exception { Long rescount = 0L; if (keysvalues.length % 2 == 0) { for (int i = 0; i < keysvalues.length; i += 2) { boolean temp = this.updateStr(keysvalues[i], keysvalues[i + 1]); if (temp) { rescount++; } } } else { throw new IllegalArgumentException("arguments invalid!"); } return rescount; } @Override public String getStr(String key) throws Exception { return jedisCluster.get(key); } @Override public List<String> getMstr(String... keys) throws Exception { List<String> values = new ArrayList<String>(); for (int i = 0; i < keys.length; i++) { values.add(this.getStr(keys[i])); } return values; } @Override public boolean deleteKey(String key) throws Exception { Long res = jedisCluster.del(key); return 1L == res; } @Override public Long deleteKeys(String... keys) throws Exception { Long rescount = 0L; for (int i = 0; i < keys.length; i++) { boolean temp = this.deleteKey(keys[i]); if (temp) { rescount++; } } return rescount; } @Override public boolean exists(String key) throws Exception { return jedisCluster.exists(key); } @Override public boolean updateExpireTime(String key, int seconds) throws Exception { Long res = jedisCluster.expire(key, seconds); return 1L == res; } @Override public Long increment(String key) throws Exception { Long res = jedisCluster.incr(key); return res; } @Override public Long incrementBy(String key, Long integer) throws Exception { Long res = jedisCluster.incrBy(key, integer); return res; } @Override public Long decrement(String key) throws Exception { Long res = jedisCluster.decr(key); return res; } @Override public Long decrementBy(String key, Long integer) throws Exception { Long res = jedisCluster.decrBy(key, integer); return res; } @Override public Long valueStrlen(String key) throws Exception { Long res = jedisCluster.strlen(key); return res; } @Override public Long addSet(String key, String... members) throws Exception { Long res = jedisCluster.sadd(key, members); return res; } @Override public Long deleteSet(String key, String... members) { Long res = jedisCluster.srem(key, members); return res; } @Override public Set<String> getSetMembers(String key) throws Exception { Set<String> res = jedisCluster.smembers(key); return res; } @Override public Long addList(String key, String... strs) throws Exception { Long res = jedisCluster.rpush(key, strs); return res; } @Override public List<String> getListMembers(String key, long start, long end) throws Exception { List<String> res = jedisCluster.lrange(key, start, end); return res; } @Override public Long getListLen(String key) throws Exception { Long res = jedisCluster.llen(key); return res; } @Override public boolean addBytes(byte[] key, byte[] value, int seconds) throws Exception { Long res = jedisCluster.setnx(key, value); if (1L == res) { if (jedisCluster.expire(key, seconds) != 1L) { if (jedisCluster.del(key) != 1L) { throw new Exception("redis delete key failed, key:" + new String(key)); } return false; } return true; } else { return false; } } @Override public boolean addBytes(byte[] key, byte[] value) throws Exception { Long res = jedisCluster.setnx(key, value); return 1L == res; } @Override public byte[] getBytes(byte[] key) throws Exception { return jedisCluster.get(key); } @Override public boolean updateExpireTime(byte[] key, int seconds) throws Exception { Long res = jedisCluster.expire(key, seconds); return 1L == res; } @Override public boolean deleteKey(byte[] key) throws Exception { Long res = jedisCluster.del(key); return 1L == res; } }1
2
3
4
相关文章推荐
- IDEA springboot集成ehcache单节点和redis集群
- spring集成 JedisCluster 连接 redis3.0 集群
- spring集成 JedisCluster 联接 redis3.0 集群
- Spring + Jedis集成Redis(集群redis数据库)
- Redis-3.0.6 集群部署集成SpringJava工程-----环境搭建
- redis集群的Cluster方式配置以及spring的集成
- SpringBoot集成redis集群存储字符串和对象
- spring集成 JedisCluster 连接 redis3.0 集群
- Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
- Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
- spring集成redis,集成redis集群
- Redis-3.0.6 集群部署集成SpringJava工程-----spring集成
- Spring集成Redis集群(含spring集成redis代码)
- spring集成 JedisCluster 连接 redis3.0 集群
- spring集成 JedisCluster 连接 redis3.0 集群
- spring集成 JedisCluster 连接 redis3.0 集群
- 基于哨兵【sentinel】模式的redis服务集群并与spring集成
- spring集成 JedisCluster 联接 redis3.0 集群
- java代码中操作Redis:单机redis、集群redis(spring+redis集成)
- Spring集成Redis集群