Spring集成redis【脱离Spring版本的】
2017-08-26 11:40
197 查看
之前集成redis时因为用到了spring-data-commons-core-*.RELEASE.jar和spring-data-redis-*.RELEASE.jar 两个jar包,所以依赖Spring的版本,之后改版了一下,重新配置了redis,脱离了Spring版本的限制,并且可以支持redis集群部署。配置如下。
jar包下载地址:http://pan.baidu.com/s/1jH6q66y
<bean id="redisShardClient" class="com.bonc.wechat.services.redis.RedisShardClient"
init-method="init" destroy-method="destory">
<property name="addresses" value="${jedis.addresses}" />
<property name="pwd" value="${jedis.pwd}" />
</bean>
}
1、导入jar包
相比于之前的配置,这次只需要两个jar包即可,①jedis-2.9.0-sources.jar ②commons-pool2-2.4.2.jarjar包下载地址:http://pan.baidu.com/s/1jH6q66y
2、将redis交由Spring容器管理
<!-- redis client --><bean id="redisShardClient" class="com.bonc.wechat.services.redis.RedisShardClient"
init-method="init" destroy-method="destory">
<property name="addresses" value="${jedis.addresses}" />
<property name="pwd" value="${jedis.pwd}" />
</bean>
3、在项目中创建redis服务类
这里的类要与交给Spring容器的类对应上。可以根据自己的业务需求完善或者删减类中方法。package com.bonc.wechat.services.redis; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; import com.bonc.wechat.common.util.SerializeUtil; /** * * redis链接管理对象,单例使用,放在初始化对象里<br> * * @date 2017-03-10 */ public class RedisShardClient { private String addresses; // 中间用逗号分隔例如:127.0.0.1:6379,127.0.0.2:6379 // 配置redis集群使用 private String pwd; private List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); ShardedJedisPool pool; /** * 初始化方法,系统启动的时候调用 */ public void init() { String[] host = addresses.split(","); for (String item : host) { String[] tmp = item.split(":"); JedisShardInfo jedisShardInfo = new JedisShardInfo(tmp[0],Integer.parseInt(tmp[1])); jedisShardInfo.setPassword(pwd); shards.add(jedisShardInfo); } GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(500); config.setMinIdle(8); config.setMaxIdle(16); config.setTestOnBorrow(true); config.setTestOnCreate(true); pool = new ShardedJedisPool(config, shards); } public void destory() { pool.destroy(); } // Redis 键(key) API start /** * 移除 * * @param key * @return */ public Long del(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.del(key); } finally { if (resource != null) { resource.close(); } } } /** * 判断是否存在 * * @param key * @return */ public Boolean exists(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.exists(key); } finally { if (resource != null) { resource.close(); } } } /** * 指定过期时间 eg:30 代表30秒 * * @param key * @param seconds * @return */ public Long expire(String key, int seconds) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.expire(key, seconds); } finally { if (resource != null) { resource.close(); } } } /** * @param key * @param unixTime 过期的时间戳 * @return Redis Expireat 命令用于以 UNIX 时间戳(unix timestamp)格式设置 key 的过期时间。key 过期后将不再可用。 */ public Long expireAt (String key ,long unixTime) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.expireAt(key, unixTime); } finally { if (resource != null) { resource.close(); } } } /** * * @param key * @return Redis TTL 命令以秒为单位返回 key 的剩余过期时间。 */ public Long TTL (String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.ttl(key); } finally { if (resource != null) { resource.close(); } } } /** * @param key * @param dbIndex 数据库编号 * @return Redis MOVE 命令用于将当前数据库的 key 移动到给定的数据库 db 当中。 */ public Long Move (String key,int dbIndex) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.move(key, dbIndex); } finally { if (resource != null) { resource.close(); } } } /** * @param key * @return Redis PERSIST 命令用于移除给定 key 的过期时间,使得 key 永不过期。 */ public Long persist(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.persist(key); } finally { if (resource != null) { resource.close(); } } } /** * @param key * @return Redis Type 命令用于返回 key 所储存的值的类型。 */ public String type(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.type(key); } finally { if (resource != null) { resource.close(); } } } // Redis 键(key) API end // Redis 字符串(String) API start /** * 获取一个redis中的一个值 * * @param key * @return */ public String get(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.get(key); } finally { if (resource != null) { resource.close(); } } } public byte[] get(byte[] key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.get(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Getrange 命令用于获取存储在指定 key 中字符串的子字符串。字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。 * @param key * @param startOffset * @param endOffset * @return */ public String getrange(String key,int startOffset, int endOffset ) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.getrange(key, startOffset, endOffset); } finally { if (resource != null) { resource.close(); } } } /** * Redis Getset 命令用于设置指定 key 的值,并返回 key 旧的值。 * @param key * @param value * @return */ public String getSet(String key,Object value ) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.getSet(key, String.valueOf(value)); } finally { if (resource != null) { resource.close(); } } } /** * Redis Setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。 * @param key * @param seconds * @param value * @return */ public String setex(String key, int seconds, Object value) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.setex(key, seconds, String.valueOf(value)); } finally { if (resource != null) { resource.close(); } } } /** * Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。 * @param key * @param value * @return */ public long setnx(String key, Object value) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.setnx(key, String.valueOf(value)); } finally { if (resource != null) { resource.close(); } } } /** * Redis Strlen 命令用于获取指定 key 所储存的字符串值的长度。当 key 储存的不是字符串值时,返回一个错误。 * @param key * @return */ public long strlen(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.strlen(key); } finally { if (resource != null) { resource.close(); } } } /** *Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 *如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。 *果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 *本操作的值限制在 64 位(bit)有符号数字表示之内。 * @param key * @param integer * @return */ public Long incrBy(String key, long integer) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.incrBy(key, integer); } finally { if (resource != null) { resource.close(); } } } /** * Redis Incr 命令将 key 中储存的数字值增一。 * 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。 * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 * 本操作的值限制在 64 位(bit)有符号数字表示之内。 * @param key * @return */ public Long incr(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.incr(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 * 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作。 * @param key * @param integer * @return */ public Double incrByFloat(String key,double integer) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.incrByFloat(key, integer); } finally { if (resource != null) { resource.close(); } } } /** * Redis Decr 命令将 key 中储存的数字值减一。 * 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECR 操作。 * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 * 本操作的值限制在 64 位(bit)有符号数字表示之内。 * @param key * @return */ public Long decr(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.decr(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Decrby 命令将 key 所储存的值减去指定的减量值。 * 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECRBY 操作。 * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 * 本操作的值限制在 64 位(bit)有符号数字表示之内。 * @param key * @param integer * @return */ public Long decrBy(String key,long integer) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.decrBy(key, integer); } finally { if (resource != null) { resource.close(); } } } /** * Redis Append 命令用于为指定的 key 追加值。 * 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 * 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 * @param key * @param value * @return */ public Long append(String key,Object value) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.append(key, String.valueOf(value)); } finally { if (resource != null) { resource.close(); } } } /** * 移除集合key中的一个或多个member元素,不存在的member元素会被忽略。 */ public Long srem(String key, String... members) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.srem(key, members); } finally { if (resource != null) { resource.close(); } } } // Redis 字符串(String) API end // Redis 哈希(Hash) API start /** * Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略。 * @param key * @param fields * @return */ public Long hdel(String key,String ... fields) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hdel(key, fields); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hexists 命令用于查看哈希表的指定字段是否存在。 * @param key * @param fields * @return */ public Boolean hexists(String key,String fields) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hexists(key, fields); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hget 命令用于返回哈希表中指定字段的值。 * @param key * @param field * @return */ public String hget(String key,String field){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hget(key, field); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hget 命令用于返回哈希表中指定值的 byte[]数组; * @param key * @param field * @return */ public byte[] hgetByte(String key,String field){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hget(key.getBytes(), field.getBytes()); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hgetall 命令用于返回哈希表中,所有的字段和值。 * 在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍。语法 * @param key * @return */ public Map<String, String> hgetAll(String key){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hgetAll(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 * 增量也可以为负数,相当于对指定字段进行减法操作。 * 如果哈希表的 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。 * 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。 * 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 * 本操作的值被限制在 64 位(bit)有符号数字表示之内。 * @param key * @param field * @param value * @return */ public Long hincrBy(String key,String field,int value){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hincrBy(key, field, value); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 * 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。 * @param key * @param field * @param value * @return */ public Double hincrByFloat(String key,String field,double value){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hincrByFloat(key, field, value); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hkeys 命令用于获取哈希表中的所有字段名。 * @param key * @return */ public Set<String> hkeys(String key){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hkeys(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hvals 命令返回哈希表所有字段的值。 * @param redisType * @return */ public List<String> hvals(String redisType) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hvals(redisType); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hlen 命令用于获取哈希表中字段的数量。 * @param key * @return */ public long hlen(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hlen(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值 如果指定的字段不存在于哈希表,那么返回一个 nil 值。 * @param key * @param fields * @return */ public List<String> hmget(String key,String[] fields) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hmget(key, fields); } finally { if (resource != null) { resource.close(); } } } /** * Redis Hset 命令用于为哈希表中的字段赋值 。 * 如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作。 * 如果字段已经存在于哈希表中,旧值将被覆盖。 * @param redisType * @param redisKey * @param redisValue * @return */ public Long hset(String key, String filed, String value) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hset(key, filed, value); } finally { if (resource != null) { resource.close(); } } } // Redis 哈希(Hash) API end /** * Redis Blpop 命令移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 * @param key * @return */ public List<String> blpop(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.blpop(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Brpop 命令移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 * @param key * @return */ public List<String> brpop (String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.brpop(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 * @param key * @param index * @return */ public String lindex (String key,long index) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lindex(key, index); } finally { if (resource != null) { resource.close(); } } } /** * Redis Linsert 命令用于在列表的元素前或者后插入元素。 当指定元素不存在于列表中时,不执行任何操作。 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误。 * @param key * @param where * @param pivot * @param value * @return */ public Long lindex (String key,LIST_POSITION where,String pivot,String value){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.linsert(key, where, pivot, value); } finally { if (resource != null) { resource.close(); } } } /** * Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误。 * @param key * @return */ public Long llen (String key){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.llen(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Lpop 命令用于移除并返回列表的第一个元素。 * @param key * @return */ public String lpop (String key){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lpop(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误。 * @param key * @param value * @return */ public long lpush (String key,String[]value){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lpush(key,value); } finally { if (resource != null) { resource.close(); } } } public long lpush (byte[] key,byte[] value){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lpush(key,value); } finally { if (resource != null) { resource.close(); } } } public List<byte[]> lrange (byte[] key,long start,long end){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lrange(key, start, end); } finally { if (resource != null) { resource.close(); } } } public List<String> lrange (String key,long start,long end){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lrange(key, start, end); } finally { if (resource != null) { resource.close(); } } } public List<Object> lrange (String key,long start,long end,Object obj){ ShardedJedis resource = null; List<Object> dataList = new ArrayList<Object>(); try { resource = pool.getResource(); List<byte[]> byteList = lrange(key.getBytes(), start, end); if(byteList.size() > 0){ for (int i = 0; i < byteList.size(); i++) { obj= SerializeUtil.deserialize(byteList.get(i)); dataList.add(obj); } } return dataList; } finally { if (resource != null) { resource.close(); } } } /** * Redis Lpushx 将一个或多个值插入到已存在的列表头部,列表不存在时操作无效。 * @param key * @param value * @return */ public long lpushx (String key,String[]value){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lpushx(key,value); } finally { if (resource != null) { resource.close(); } } } /** * Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 * @param key * @param start * @param end * @return */ public List<String> lpushx (String key,int start,int end){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lrange(key, start, end); } finally { if (resource != null) { resource.close(); } } } /** * Redis Lset 通过索引来设置元素的值。 * 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误。 * @param key * @param index * @param value * @return */ public String lset(String key,int index,Object value){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lset(key, index, String.valueOf(value)); } finally { if (resource != null) { resource.close(); } } } /** * Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 * 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 * @param key * @param start * @param end * @return */ public String ltrim(String key,int start,int end){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.ltrim(key, start, end); } finally { if (resource != null) { resource.close(); } } } /** * Redis Rpop 命令用于移除并返回列表的最后一个元素。 * @param key * @return */ public String rpop (String key){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.rpop(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。 * 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 * @param key * @param values * @return */ public long rpush (String key,String[] values){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.rpush(key, values); } finally { if (resource != null) { resource.close(); } } } /** * Redis Rpushx 命令用于将一个或多个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效。 * @param key * @param values * @return */ public long rpushx (String key,String[] values){ ShardedJedis resource = null; try { resource = pool.getResource(); return resource.rpushx(key, values); } finally { if (resource != null) { resource.close(); } } } // Redis 列表(List) API start // Redis 集合(Set) API start /** * Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 * 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 * 当集合 key 不是集合类型时,返回一个错误。 * 注意:在Redis2.4版本以前, SADD 只接受单个成员值 * @param key * @param members * @return */ public long sadd(String key,String[] members) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.sadd(key, members); } finally { if (resource != null) { resource.close(); } } } /** * Redis Scard 命令返回集合中元素的数量。 * @param key * @return */ public long scard(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.scard(key); } finally { if (resource != null) { resource.close(); } } } /** * Redis Sismember 命令判断成员元素是否是集合的成员。 * @param key * @param member * @return */ public Boolean sismember(String key,String member) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.sismember(key, member); } finally { if (resource != null) { resource.close(); } } } public Set<String> smembers(String key) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.smembers(key); } finally { if (resource != null) { resource.close(); } } } /** * 获取多个值存到map中 * * @param keys * @return */ public HashMap<String, String> get(List<String> keys) { HashMap<String, String> resultHashMap = null; ShardedJedis resource = null; try { resource = pool.getResource(); if (null != keys && keys.size() > 0) { resultHashMap = new HashMap<String, String>(); for (int i = 0; i < keys.size(); i++) { resultHashMap.put(keys.get(i), resource.get(keys.get(i))); } } return resultHashMap; } finally { if (resource != null) { resource.close(); } } } public String set(String key, String value) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.set(key, value); } finally { if (resource != null) { resource.close(); } } } public Long pexpire(String key, long seconds) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.pexpire(key, seconds); } finally { if (resource != null) { resource.close(); } } } public Long zadd(String key, String member) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.zadd(key, 1, member); } finally { if (resource != null) { resource.close(); } } } public Long zadd(String key, double score, String member) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.zadd(key, score, member); } finally { if (resource != null) { resource.close(); } } } public Set<String> zrange(String key, long start, long end) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.zrange(key, start, end); } finally { if (resource != null) { resource.close(); } } } public Set<String> zrevrange(String key, long start, long end) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.zrevrange(key, start, end); } finally { if (resource != null) { resource.close(); } } } public Long hdel(String redisType, String redisKey) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.hdel(redisType, redisKey); } finally { if (resource != null) { resource.close(); } } } public List<String> brpop(int timeOut, String redisKey) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.brpop(timeOut, redisKey); } finally { if (resource != null) { resource.close(); } } } public Long lpush(String redisKey, String data) { ShardedJedis resource = null; try { resource = pool.getResource(); return resource.lpush(redisKey, data); } finally { if (resource != null) { resource.close(); } } } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getAddresses() { return addresses; } public void setAddresses(String addresses) { this.addresses = addresses; }
}
4、配置完成,启动项目即可。
相关文章推荐
- 最简单的spring集成redis
- spring整合redis时 因为版本问题导致bean注入不进去问题
- redis应用之springboot集成redis
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- SSM超详细搭建教程(Spring+SpringMVC+Mybatis)以及集成Redis和Druid
- Spring Boot集成Redis实现缓存
- SpringBoot之集成Redis NoSql数据库
- spring集成 JedisCluster 连接 redis3.0 集群
- spring集成redis
- redis与spring的完全集成
- Spring Boot集成Redis实现缓存机制
- redis 工具类 单个redis、JedisPool 及多个redis、shardedJedisPool与spring的集成配置
- Redis 缓存 + Spring 的集成示例
- Redis 缓存 + Spring 的集成示例
- Spring Boot : 集成redis(九)
- Redis 讲解系列之 与Spring集成(二)
- Spring集成Redis集群(含spring集成redis代码)
- Redis + Spring 的集成示例
- Dubbo 2.5.8版本与高版本Spring 4.3.10集成
- SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)