Jedis是Redis的Java版本的客户端实现。 一、我们需要导入jar包(下面是最新版本,可根据情况自行选择版本) [html] view plain copy <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> 也可自行下载 jedisAPI文档:http://tool.oschina.NET/uploads/apidocs/ 二、配置spring-redis.xml配置文件 1.spring-redis.xml [html] view plain copy <?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-lazy-init="false"> <description>REDIS缓存配置</description> <!-- JEDIS SHARD 池配置 --> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <ref bean="jedis.shardInfo.default" /> </list> </constructor-arg> </bean> <!-- JEDIS连接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="100" /> <property name="maxIdle" value="20" /> <property name="maxWaitMillis" value="5000" /> <property name="testOnBorrow" value="true" /> </bean> <!-- JEDIS SHARD信息配置[START] 在此添加REDIS集群配置 --> <bean id="jedis.shardInfo.default" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="192.168.1.21" /> <constructor-arg index="1" type="int" value="6379" /> <constructor-arg index="2" type="java.lang.String" value="192.168.1.21" /> </bean> <!-- JEDIS SHARD信息配置 [END] --> </beans> 2.spring-core.xml [html] view plain copy <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <import resource="spring-redis.xml"/> </beans> 个人习惯将spring配置文件按需求划分开,然后在配置在一个spring中实现,这样可以更好的维护每个spring模块 3.将shardedJedisPool注入相关的类中即可使用 [plain] view plain copy public class RedisTest { @Autowired private ShardedJedisPool shardedJedisPool; @Test public void testSet() { ShardedJedis jedis = shardedJedisPool.getResource(); jedis.set("name", "test"); } @Test public void testGet() { ShardedJedis jedis = shardedJedisPool.getResource(); String name = jedis.get("name"); System.out.println(name); } } 4.实现redis的封装 我们每次将shardedJedisPool注入到相关类中会比较麻烦,我们需要集成一个封装方法,方便redis的实现 在spring-redis中添加 [html] view plain copy <!-- 缓存代理,使用缓存内容请使用本代理进行操作 请使用接口进行注入 --> <bean id="cacheProxy" class="com.yuanding.common.cache.CacheProxy"> <property name="shardedJedisPool"> <ref bean="shardedJedisPool" /> </property> </bean> 实现cacheProxy类继承ShardedRedisProxy类 [java] view plain copy package com.yuanding.common.cache; /** * * 缓存代理 * */ public class CacheProxy{ //TODO 这里可以继承ShardedJedisPool的实现类 ... } 三、shardedJedisPool的java实现与jedis客户端的封装类 1.IRedisProxy缓存代理接口 [java] view plain copy package com.yuanding.common.cache; import java.util.List; import java.util.Map; import java.util.Set; /** * * 缓存代理接口 * <p> * 提供缓存内容操作接口,包括key以及value的操作,本代理只提供缓存基本操作,不提供涉及连接等连接池操作。 * <P> * 1、以H开头的为HASH操作命令; * <P> * 2、以L开头的为LIST操作命令; * <P>; 3、以S开头的为SET(集合)操作命令; * <P> * 4、以Z开头的为ZSET(有序集合)操作命令 * * @author eko */ public interface IRedisProxy { /** * toString * @return 值 */ public String toString(); /** * 将字符串值value关联到key。 如果key已经持有其他值,SET就覆写旧值,无视类型。 * * @param key key * @param value value * @return string */ public String set(String key, String value); /** * 返回key所关联的字符串值 * <p> * 假如key储存的值不是字符串类型,返回一个错误,因为GET只能用于处理字符串值 * * @param key key * @return String */ public String get(String key); /** * 检查给定key是否存在。 * <p> * * @param key key * @return 是否 */ public Boolean exists(String key); /** * 返回key所储存的值的类型 * * @param key key * @return String */ public String type(String key); /** * 为给定key设置生存时间。单位秒 当key过期时,它会被自动删除。 * * @param key key * @param seconds seconds * @return Long */ public Long expire(String key, int seconds); /** * 以UNIX时间戳为key设置生存时间。 EXPIREAT命令接受的时间参数是UNIX时间戳(unix timestamp)。 * * @param key key * @param unixTime unixTime * @return Long */ public Long expireAt(String key, long unixTime); /** * 返回给定key的剩余生存时间(time to live)(以秒为单位)。 * * @param key key * @return Long */ public Long ttl(String key); /** * 用value参数覆写(Overwrite)给定key所储存的字符串值,从偏移量offset开始。 不存在的key当作空白字符串处理。 * * @param key key * @param offset offset * @param value value * @return long */ public long setrange(String key, long offset, String value); /** * 返回key中字符串值的子字符串,字符串的截取范围由startOffset和endOffset两个偏移量决定(包括startOffset和endOffset在内)。 * * @param key key * @param startOffset startOffset * @param endOffset endOffset * @return 子字符串 */ public String getrange(String key, long startOffset, long endOffset); /** * 将给定key的值设为value,并返回key的旧值。 当key存在但不是字符串类型时,返回一个错误。 * * @param key key * @param value value * @return value */ public String getSet(String key, String value); /** * 设置过期时间 * @param key key * @param value value * @return Long */ public Long setnx(String key, String value); /** * 将值value关联到key,并将key的生存时间设为seconds(以秒为单位)。 如果key 已经存在,SETEX命令将覆写旧值。 * * @param key key * @param seconds seconds * @param value value * @return 值 */ public String setex(String key, int seconds, String value); /** * 将key所储存的值减去减量integer。 如果key不存在,以0为key的初始值,然后执行DECRBY操作。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 * 本操作的值限制在64位(bit)有符号数字表示之内。 * * @param key key * @param integer integer * @return 值 */ public Long decrBy(String key, long integer); /** * 将key中储存的数字值减一。 如果key不存在,以0为key的初始值,然后执行DECR操作。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 * * @param key key * @return 值 */ public Long decr(String key); /** * 将key所储存的值加上增量increment。 如果key不存在,以0为key的初始值,然后执行INCRBY命令。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误 * * @param key key * @param integer integer * @return 值 */ public Long incrBy(String key, long integer); /** * 将key中储存的数字值增一。 如果key不存在,以0为key的初始值,然后执行INCR操作。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 * * @param key key * @return Long */ public Long incr(String key); /** * 如果key已经存在并且是一个字符串,APPEND命令将value追加到key原来的值之后。 如果key不存在,APPEND就简单地将给定key设为value,就像执行SET key value一样。 * * @param key key * @param value value * @return 值 */ public Long append(String key, String value); /** * 截取子串 * @param key key * @param start start * @param end end * @return 子串 */ public String substr(String key, int start, int end); /** * 将哈希表key中的域field的值设为value。 如果key不存在,一个新的哈希表被创建并进行HSET操作。 如果域field已经存在于哈希表中,旧值将被覆盖。 * * @param key key * @param field field * @param value value * @return 值 */ public Long hset(String key, String field, String value); /** * 返回哈希表key中给定域field的值。 * * @param key key * @param field field * @return 值 */ public String hget(String key, String field); /** * 将哈希表key中的域field的值设置为value,当且仅当域field不存在。 若域field已经存在,该操作无效。 如果key不存在,一个新哈希表被创建并执行HSETNX命令。 * * @param key key * @param field field * @param value value * @return value */ public Long hsetnx(String key, String field, String value); /** * 同时将多个field - value(域-值)对设置到哈希表key中。 此命令会覆盖哈希表中已存在的域。 * * @param key key * @param hash hash * @return 值 */ public String hmset(String key, Map<String, String> hash); /** * 返回哈希表key中,一个或多个给定域的值。 * * @param key key * @param fields fields * @return 值 */ public List<String> hmget(String key, String... fields); /** * hincrBy * @param key key * @param field field * @param value value * @return Long */ public Long hincrBy(String key, String field, long value); /** * 为哈希表key中的域field的值加上增量increment。 增量也可以为负数,相当于对给定域进行减法操作。 如果key不存在,一个新的哈希表被创建并执行HINCRBY命令。 * 如果域field不存在,那么在执行命令前,域的值被初始化为0。 * * @param key key * @param field field * @return 是否 */ public Boolean hexists(String key, String field); /** * 移除给定的一个或多个key。 如果key不存在,则忽略该命令。 * * @param key key * @return Long */ public Long del(String key); /** * 删除哈希表key中的一个指定域,不存在的域将被忽略 * * @param key key * @param field field * @return Long */ public Long hdel(String key, String field); /** * 返回哈希表key中域的数量。 * * @param key key * @return Long */ public Long hlen(String key); /** * 返回哈希表key中的所有域 * * @param key key * @return Set */ public Set<String> hkeys(String key); /** * 返回哈希表key中的所有值。 * * @param key key * @return List */ public List<String> hvals(String key); /** * 返回哈希表key中,所有的域和值。 在返回值里,紧跟每个域名(field name)之后是域的值(value),所以返回值的长度是哈希表大小的两倍。 * * @param key key * @return Map */ public Map<String, String> hgetAll(String key); /** * 将一个值value插入到列表key的表尾。 * * @param key key * @param string string * @return Long */ public Long rpush(String key, String string); /** * 将一个值value插入到列表key的表头。 * * @param key key * @param string string * @return Long */ public Long lpush(String key, String string); /** * 返回列表key的长度。 如果key不存在,则key被解释为一个空列表,返回0. 如果key不是列表类型,返回一个错误。 * * @param key key * @return Long */ public Long llen(String key); /** * 返回列表key中指定区间内的元素,区间以偏移量start和end指定。 下标(index)参数start和end都以0为底,也就是说,以0表示列表的第一个元素,以1表示列表的第二个元素,以此类推。 * 你也可以使用负数下标,以-1表示列表的最后一个元素,-2表示列表的倒数第二个元素,以此类推。 * * @param key key * @param start start * @param end end * @return List */ public List<String> lrange(String key, long start, long end); /** * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 * * @param key key * @param start start * @param end end * @return String */ public String ltrim(String key, long start, long end); /** * 返回列表key中,下标为index的元素。 * * @param key key * @param index index * @return String */ public String lindex(String key, long index); /** * 将列表key下标为index的元素的值设置为value。 当index参数超出范围,或对一个空列表(key不存在)进行LSET时,返回一个错误。 * * @param key key * @param index index * @param value value * @return String */ public String lset(String key, long index, String value); /** * 根据参数count的值,移除列表中与参数value相等的元素。 * <P> * count的值可以是以下几种: * <P> * count > 0: 从表头开始向表尾搜索,移除与value相等的元素,数量为count。 * <P> * count < 0: 从表尾开始向表头搜索,移除与value相等的元素,数量为count的绝对值。 * <P> * count = 0: 移除表中所有与value相等的值。 * * @param key key * @param start start * @param end end * @return Long */ public Long lrem(String key, long count, String value); /** * 移除并返回列表key的头元素。 * * @param key key * @return String */ public String lpop(String key); /** * 移除并返回列表key的尾元素。 * * @param key key * @return String */ public String rpop(String key); /** * 将一个或多个member元素加入到集合key当中,已经存在于集合的member元素将被忽略。 假如key不存在,则创建一个只包含member元素作成员的集合。 * * @param key key * @param member member * @return Long */ public Long sadd(String key, String member); /** * 返回集合key中的所有成员。 * * @param key key * @return Set */ public Set<String> smembers(String key); /** * 移除集合key中的一个或多个member元素,不存在的member元素会被忽略。 当key不是集合类型,返回一个错误。 * * @param key key * @param member member * @return Long */ public Long srem(String key, String member); /** * 移除并返回集合中的一个随机元素。 * * @param key key * @return String */ public String spop(String key); /** * 返回集合key的基数(集合中元素的数量)。 * * @param key key * @return Long */ public Long scard(String key); /** * 判断member元素是否是集合key的成员。 * * @param key key * @param member member * @return 是否 */ public Boolean sismember(String key, String member); /** * 返回集合中的一个随机元素。 * * @param key key * @return String */ public String srandmember(String key); /** * 将一个member元素及其score值加入到有序集key当中。 * * @param key key * @param score score * @param member member * @return Long */ public Long zadd(String key, double score, String member); /** * 返回有序集key中,指定区间内的成员。 其中成员的位置按score值递增(从小到大)来排序。 * * @param key key * @param start start * @param end end * @return Set */ public Set<String> zrange(String key, int start, int end); /** * 移除有序集key中的一个或多个成员,不存在的成员将被忽略。 当key存在但不是有序集类型时,返回一个错误。 * * @param key key * @param member member * @return Long */ public Long zrem(String key, String member); /** * 为有序集key的成员member的score值加上增量increment。 * * @param key key * @param score score * @param member member * @return Double */ public Double zincrby(String key, double score, String member); /** * 返回有序集key中成员member的排名。其中有序集成员按score值递增(从小到大)顺序排列。 排名以0为底,也就是说,score值最小的成员排名为0。 * * @param key key * @param member member * @return Long */ public Long zrank(String key, String member); /** * 返回有序集key中成员member的排名。其中有序集成员按score值递减(从大到小)排序。 排名以0为底,也就是说,score值最大的成员排名为0。 * * @param key key * @param member member * @return Long */ public Long zrevrank(String key, String member); /** * 返回有序集key中,指定区间内的成员。 其中成员的位置按score值递减(从大到小)来排列。 * * @param key key * @param start * @param end * @return */ public Set<String> zrevrange(String key, int start, int end); /** * 返回有序集key的基数。 * * @param key * @return */ public Long zcard(String key); /** * 返回有序集key中,成员member的score值。 如果member元素不是有序集key的成员,或key不存在,返回null。 * * @param key * @param member * @return */ public Double zscore(String key, String member); /** * 排序 * @param key * @return */ public List<String> sort(String key); /** * 返回有序集key中,score值在min和max之间(默认包括score值等于min或max)的成员。 * * @param key * @param min * @param max * @return */ public Long zcount(String key, double min, double max); public Set<String> zrangeByScore(String key, double min, double max); /** * 返回有序集key中,score值介于max和min之间(默认包括等于max或min)的所有的成员。有序集成员按score值递减(从大到小)的次序排列 * * @param key * @param max * @param min * @return */ public Set<String> zrevrangeByScore(String key, double max, double min); /** * 返回有序集key中,所有score值介于min和max之间(包括等于min或max)的成员。有序集成员按score值递增(从小到大)次序排列。 具有相同score值的成员按字典序(lexicographical * order)来排列(该属性是有序集提供的,不需要额外的计算)。 * * @param key * @param min * @param max * @param offset * @param count * @return */ public Set<String> zrangeByScore(String key, double min, double max, int offset, int count); /** * zrevrangeByScore * @param key key * @param max max * @param min min * @param offset offset * @param count count * @return Set */ public Set<String> zrevrangeByScore(String key, double max, double min, int offset, int count); /** * zremrangeByRank * @param key key * @param start start * @param end end * @return Long */ public Long zremrangeByRank(String key, int start, int end); /** * zremrangeByScore * @param key key * @param start start * @param end end * @return zremrangeByScore */ public Long zremrangeByScore(String key, double start, double end); } 2.ShardedRedisProxy接口的实现类 [java] view plain copy package com.yuanding.common.cache; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; /** * * 本实现为分片Redis缓存服务代理 * * @author eko */ public class ShardedRedisProxy implements IRedisProxy { /** * 日志 */ protected Log log = LogFactory.getLog(this.getClass()); /** * 缓存连接池 */ private ShardedJedisPool shardedJedisPool; public Long append(String key, String value) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.append(key, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long decr(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.decr(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long decrBy(String key, long integer) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.decrBy(key, integer); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long del(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.del(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Boolean exists(String key) { ShardedJedis shardedJedis = null; Boolean ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.exists(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long expire(String key, int seconds) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.expire(key, seconds); return ret; } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long expireAt(String key, long unixTime) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.expireAt(key, unixTime); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String get(String key) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.get(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String getSet(String key, String value) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.getSet(key, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String getrange(String key, long startOffset, long endOffset) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.getrange(key, startOffset, endOffset); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long hdel(String key, String field) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hdel(key, field); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Boolean hexists(String key, String field) { ShardedJedis shardedJedis = null; Boolean ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hexists(key, field); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String hget(String key, String field) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hget(key, field); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Map<String, String> hgetAll(String key) { ShardedJedis shardedJedis = null; Map<String, String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hgetAll(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long hincrBy(String key, String field, long value) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hincrBy(key, field, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> hkeys(String key) { ShardedJedis shardedJedis = null; Set<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hkeys(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long hlen(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hlen(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public List<String> hmget(String key, String... fields) { ShardedJedis shardedJedis = null; List<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hmget(key, fields); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String hmset(String key, Map<String, String> hash) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hmset(key, hash); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long hset(String key, String field, String value) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hset(key, field, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long hsetnx(String key, String field, String value) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hsetnx(key, field, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public List<String> hvals(String key) { ShardedJedis shardedJedis = null; List<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.hvals(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long incr(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.incr(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long incrBy(String key, long integer) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.incrBy(key, integer); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String lindex(String key, long index) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.lindex(key, index); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long llen(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.llen(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String lpop(String key) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.lpop(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long lpush(String key, String string) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.lpush(key, string); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public List<String> lrange(String key, long start, long end) { ShardedJedis shardedJedis = null; List<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.lrange(key, start, end); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long lrem(String key, long count, String value) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.lrem(key, count, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String lset(String key, long index, String value) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.lset(key, index, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String ltrim(String key, long start, long end) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.ltrim(key, start, end); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String rpop(String key) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.rpop(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long rpush(String key, String string) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.rpush(key, string); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long sadd(String key, String member) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.sadd(key, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long scard(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.scard(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String set(String key, String value) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.set(key, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String setex(String key, int seconds, String value) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.setex(key, seconds, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long setnx(String key, String value) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.setnx(key, value); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public long setrange(String key, long offset, String value) { ShardedJedis shardedJedis = null; long ret = 0; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.setrange(key, offset, value); return ret; } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Boolean sismember(String key, String member) { ShardedJedis shardedJedis = null; Boolean ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.sismember(key, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> smembers(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); Set<String> ret = shardedJedis.smembers(key); return ret; } catch (Exception e) { log.error(this, e); return null; } finally { shardedJedisPool.returnResource(shardedJedis); } } public List<String> sort(String key) { ShardedJedis shardedJedis = null; List<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.sort(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String spop(String key) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.spop(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String srandmember(String key) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.srandmember(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long srem(String key, String member) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.srem(key, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String substr(String key, int start, int end) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.substr(key, start, end); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long ttl(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.ttl(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public String type(String key) { ShardedJedis shardedJedis = null; String ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.type(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zadd(String key, double score, String member) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zadd(key, score, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zcard(String key) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zcard(key); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zcount(String key, double min, double max) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zcount(key, min, max); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Double zincrby(String key, double score, String member) { ShardedJedis shardedJedis = null; Double ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zincrby(key, score, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> zrange(String key, int start, int end) { ShardedJedis shardedJedis = null; Set<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrange(key, start, end); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> zrangeByScore(String key, double min, double max) { ShardedJedis shardedJedis = null; Set<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrangeByScore(key, min, max); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> zrangeByScore(String key, double min, double max, int offset, int count) { ShardedJedis shardedJedis = null; Set<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrangeByScore(key, min, max, offset, count); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zrank(String key, String member) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrank(key, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zrem(String key, String member) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrem(key, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zremrangeByRank(String key, int start, int end) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zremrangeByRank(key, start, end); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zremrangeByScore(String key, double start, double end) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zremrangeByScore(key, start, end); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> zrevrange(String key, int start, int end) { ShardedJedis shardedJedis = null; Set<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrevrange(key, start, end); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> zrevrangeByScore(String key, double max, double min) { ShardedJedis shardedJedis = null; Set<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrevrangeByScore(key, max, min); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Set<String> zrevrangeByScore(String key, double max, double min, int offset, int count) { ShardedJedis shardedJedis = null; Set<String> ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrevrangeByScore(key, max, min, offset, count); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Long zrevrank(String key, String member) { ShardedJedis shardedJedis = null; Long ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zrevrank(key, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } public Double zscore(String key, String member) { ShardedJedis shardedJedis = null; Double ret = null; try { shardedJedis = shardedJedisPool.getResource(); ret = shardedJedis.zscore(key, member); } catch (Exception e) { log.error(this, e); } finally { shardedJedisPool.returnResource(shardedJedis); } return ret; } /** * @return the shardedJedisPool */ public ShardedJedisPool getShardedJedisPool() { return shardedJedisPool; } /** * @param shardedJedisPool * the shardedJedisPool to set */ public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) { this.shardedJedisPool = shardedJedisPool; } } CacheProxy继承接口IRedisProxy [java] view plain copy package com.yuanding.common.cache; /** * * 缓存代理 * */ public class CacheProxy extends ShardedRedisProxy{ //TODO 继承ShardedRedisProxy缓存代理的方法 ... } 这样我们就可以实现封装的接口 使用封装接口的好处就是ShardedJedisPool缓存池执行的时候总会出现一些问题,抛出一些bug,不使用try catch将错误抛出,就会出现莫名其妙的问题
时间: 2024-10-17 00:36:11