1.下载相关jar包,并引入工程:
jedis-2.4.2.jar
commons-pool2-2.0.jar
2.将以下XML配置引入spring
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="jedisPoolConfig"/> <constructor-arg index="1"> <list> <bean name="slaver" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis.slaver.host}"/> <constructor-arg index="1" value="${redis.slaver.port}" type="int"/> </bean> <bean name="master" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis.master.host}"/> <constructor-arg index="1" value="${redis.master.port}" type="int"/> </bean> </list> </constructor-arg> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="2048" /> <property name="maxIdle" value="200" /> <property name="numTestsPerEvictionRun" value="1024"/> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="minEvictableIdleTimeMillis" value="-1" /> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <property name="maxWaitMillis" value="1500"/> <property name="testOnBorrow" value="true" /> <property name="testWhileIdle" value="true"/> <property name="testOnReturn" value="false"/> <property name="jmxEnabled" value="true"/> <property name="jmxNamePrefix" value="youyuan"/> <property name="blockWhenExhausted" value="false"/> </bean>
3.将shardedJedisPool注入相关的类中即可使用
@Resource private ShardedJedisPool shardedJedisPool; /** * 设置一个key的过期时间(单位:秒) * @param key key值 * @param seconds 多少秒后过期 * @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间 */ public long expire(String key, int seconds) { if (key==null || key.equals("")) { return 0; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.expire(key, seconds); } catch (Exception ex) { logger.error("EXPIRE error[key=" + key + " seconds=" + seconds + "]" + ex.getMessage(), ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return 0; } /** * 设置一个key在某个时间点过期 * @param key key值 * @param unixTimestamp unix时间戳,从1970-01-01 00:00:00开始到现在的秒数 * @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间 */ public long expireAt(String key, int unixTimestamp) { if (key==null || key.equals("")) { return 0; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.expireAt(key, unixTimestamp); } catch (Exception ex) { logger.error("EXPIRE error[key=" + key + " unixTimestamp=" + unixTimestamp + "]" + ex.getMessage(), ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return 0; } /** * 截断一个List * @param key 列表key * @param start 开始位置 从0开始 * @param end 结束位置 * @return 状态码 */ public String trimList(String key, long start, long end) { if (key == null || key.equals("")) { return "-"; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.ltrim(key, start, end); } catch (Exception ex) { logger.error("LTRIM 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage() , ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return "-"; } /** * 检查Set长度 * @param key * @return */ public long countSet(String key){ if(key == null ){ return 0; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.scard(key); } catch (Exception ex) { logger.error("countSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return 0; } /** * 添加到Set中(同时设置过期时间) * @param key key值 * @param seconds 过期时间 单位s * @param value * @return */ public boolean addSet(String key,int seconds, String... value) { boolean result = addSet(key, value); if(result){ long i = expire(key, seconds); return i==1; } return false; } /** * 添加到Set中 * @param key * @param value * @return */ public boolean addSet(String key, String... value) { if(key == null || value == null){ return false; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.sadd(key, value); return true; } catch (Exception ex) { logger.error("setList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * @param key * @param value * @return 判断值是否包含在set中 */ public boolean containsInSet(String key, String value) { if(key == null || value == null){ return false; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.sismember(key, value); } catch (Exception ex) { logger.error("setList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * 获取Set * @param key * @return */ public Set<String> getSet(String key){ ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.smembers(key); } catch (Exception ex) { logger.error("getList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } /** * 从set中删除value * @param key * @return */ public boolean removeSetValue(String key,String... value){ ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.srem(key, value); return true; } catch (Exception ex) { logger.error("getList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * 从list中删除value 默认count 1 * @param key * @param values 值list * @return */ public int removeListValue(String key,List<String> values){ return removeListValue(key, 1, values); } /** * 从list中删除value * @param key * @param count * @param values 值list * @return */ public int removeListValue(String key,long count,List<String> values){ int result = 0; if(values != null && values.size()>0){ for(String value : values){ if(removeListValue(key, count, value)){ result++; } } } return result; } /** * 从list中删除value * @param key * @param count 要删除个数 * @param value * @return */ public boolean removeListValue(String key,long count,String value){ ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.lrem(key, count, value); return true; } catch (Exception ex) { logger.error("getList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * 截取List * @param key * @param start 起始位置 * @param end 结束位置 * @return */ public List<String> rangeList(String key, long start, long end) { if (key == null || key.equals("")) { return null; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.lrange(key, start, end); } catch (Exception ex) { logger.error("rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage() , ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } /** * 检查List长度 * @param key * @return */ public long countList(String key){ if(key == null ){ return 0; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.llen(key); } catch (Exception ex) { logger.error("countList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return 0; } /** * 添加到List中(同时设置过期时间) * @param key key值 * @param seconds 过期时间 单位s * @param value * @return */ public boolean addList(String key,int seconds, String... value){ boolean result = addList(key, value); if(result){ long i = expire(key, seconds); return i==1; } return false; } /** * 添加到List * @param key * @param value * @return */ public boolean addList(String key, String... value) { if(key == null || value == null){ return false; } ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.lpush(key, value); return true; } catch (Exception ex) { logger.error("setList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * 添加到List(只新增) * @param key * @param value * @return */ public boolean addList(String key, List<String> list) { if(key == null || list == null || list.size() == 0){ return false; } for(String value : list){ addList(key, value); } return true; } /** * 获取List * @param key * @return */ public List<String> getList(String key){ ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.lrange(key, 0, -1); } catch (Exception ex) { logger.error("getList error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } /** * 设置HashSet对象 * * @param domain 域名 * @param key 键值 * @param value Json String or String value * @return */ public boolean setHSet(String domain, String key, String value) { if (value == null) return false; ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.hset(domain, key, value); return true; } catch (Exception ex) { logger.error("setHSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * 获得HashSet对象 * * @param domain 域名 * @param key 键值 * @return Json String or String value */ public String getHSet(String domain, String key) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.hget(domain, key); } catch (Exception ex) { logger.error("getHSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } /** * 删除HashSet对象 * * @param domain 域名 * @param key 键值 * @return 删除的记录数 */ public long delHSet(String domain, String key) { ShardedJedis shardedJedis = null; long count = 0; try { shardedJedis = shardedJedisPool.getResource(); count = shardedJedis.hdel(domain, key); } catch (Exception ex) { logger.error("delHSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return count; } /** * 删除HashSet对象 * * @param domain 域名 * @param key 键值 * @return 删除的记录数 */ public long delHSet(String domain, String... key) { ShardedJedis shardedJedis = null; long count = 0; try { shardedJedis = shardedJedisPool.getResource(); count = shardedJedis.hdel(domain, key); } catch (Exception ex) { logger.error("delHSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return count; } /** * 判断key是否存在 * * @param domain 域名 * @param key 键值 * @return */ public boolean existsHSet(String domain, String key) { ShardedJedis shardedJedis = null; boolean isExist = false; try { shardedJedis = shardedJedisPool.getResource(); isExist = shardedJedis.hexists(domain, key); } catch (Exception ex) { logger.error("existsHSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return isExist; } /** * 全局扫描hset * * @param match field匹配模式 * @return */ public List<Map.Entry<String, String>> scanHSet(String domain, String match) { ShardedJedis shardedJedis = null; try { int cursor = 0; shardedJedis = shardedJedisPool.getResource(); ScanParams scanParams = new ScanParams(); scanParams.match(match); Jedis jedis = shardedJedis.getShard(domain); ScanResult<Map.Entry<String, String>> scanResult; List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(); do { scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams); list.addAll(scanResult.getResult()); cursor = Integer.parseInt(scanResult.getStringCursor()); } while (cursor > 0); return list; } catch (Exception ex) { logger.error("scanHSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } /** * 返回 domain 指定的哈希集中所有字段的value值 * * @param domain * @return */ public List<String> hvals(String domain) { ShardedJedis shardedJedis = null; List<String> retList = null; try { shardedJedis = shardedJedisPool.getResource(); retList = shardedJedis.hvals(domain); } catch (Exception ex) { logger.error("hvals error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return retList; } /** * 返回 domain 指定的哈希集中所有字段的key值 * * @param domain * @return */ public Set<String> hkeys(String domain) { ShardedJedis shardedJedis = null; Set<String> retList = null; try { shardedJedis = shardedJedisPool.getResource(); retList = shardedJedis.hkeys(domain); } catch (Exception ex) { logger.error("hkeys error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return retList; } /** * 返回 domain 指定的哈希key值总数 * * @param domain * @return */ public long lenHset(String domain) { ShardedJedis shardedJedis = null; long retList = 0; try { shardedJedis = shardedJedisPool.getResource(); retList = shardedJedis.hlen(domain); } catch (Exception ex) { logger.error("hkeys error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return retList; } /** * 设置排序集合 * * @param key * @param score * @param value * @return */ public boolean setSortedSet(String key, long score, String value) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.zadd(key, score, value); return true; } catch (Exception ex) { logger.error("setSortedSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * 获得排序集合 * * @param key * @param startScore * @param endScore * @param orderByDesc * @return */ public Set<String> getSoredSet(String key, long startScore, long endScore, boolean orderByDesc) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); if (orderByDesc) { return shardedJedis.zrevrangeByScore(key, endScore, startScore); } else { return shardedJedis.zrangeByScore(key, startScore, endScore); } } catch (Exception ex) { logger.error("getSoredSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } /** * 计算排序长度 * * @param key * @param startScore * @param endScore * @return */ public long countSoredSet(String key, long startScore, long endScore) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); Long count = shardedJedis.zcount(key, startScore, endScore); return count == null ? 0L : count; } catch (Exception ex) { logger.error("countSoredSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return 0L; } /** * 删除排序集合 * * @param key * @param value * @return */ public boolean delSortedSet(String key, String value) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); long count = shardedJedis.zrem(key, value); return count > 0; } catch (Exception ex) { logger.error("delSortedSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } /** * 获得排序集合 * * @param key * @param startRange * @param endRange * @param orderByDesc * @return */ public Set<String> getSoredSetByRange(String key, int startRange, int endRange, boolean orderByDesc) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); if (orderByDesc) { return shardedJedis.zrevrange(key, startRange, endRange); } else { return shardedJedis.zrange(key, startRange, endRange); } } catch (Exception ex) { logger.error("getSoredSetByRange error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } /** * 获得排序打分 * * @param key * @return */ public Double getScore(String key, String member) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.zscore(key, member); } catch (Exception ex) { logger.error("getSoredSet error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return null; } public boolean set(String key, String value, int second) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.setex(key, second, value); return true; } catch (Exception ex) { logger.error("set error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } public boolean set(String key, String value) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.set(key, value); return true; } catch (Exception ex) { logger.error("set error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } public String get(String key, String defaultValue) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.get(key) == null?defaultValue:shardedJedis.get(key); } catch (Exception ex) { logger.error("get error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return defaultValue; } public boolean del(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); shardedJedis.del(key); return true; } catch (Exception ex) { logger.error("del error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return false; } public long incr(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.incr(key); } catch (Exception ex) { logger.error("incr error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return 0; } public long decr(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = shardedJedisPool.getResource(); return shardedJedis.decr(key); } catch (Exception ex) { logger.error("incr error.", ex); returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedis); } return 0; } private void returnBrokenResource(ShardedJedis shardedJedis) { try { shardedJedisPool.returnBrokenResource(shardedJedis); } catch (Exception e) { logger.error("returnBrokenResource error.", e); } } private void returnResource(ShardedJedis shardedJedis) { try { shardedJedisPool.returnResource(shardedJedis); } catch (Exception e) { logger.error("returnResource error.", e); } }
相关推荐
在本案例中,我们将深入探讨如何在Spring框架中整合Redis数据存储系统,使用Jedis作为客户端库。Redis是一个高性能的键值对存储系统,适用于缓存、消息队列等多种应用场景。而Spring是Java领域广泛使用的应用程序...
在本项目中,我们关注的是 Redis 的客户端连接、Spring Boot 整合以及分布式锁的实现。 首先,Redis 客户端连接是与 Redis 服务器进行交互的基础。常见的 Redis 客户端库有 Jedis 和 Lettuce。Jedis 是一个用 Java ...
在Maven项目中,可以在pom.xml文件中添加`spring-data-redis`和`jedis`库,这两个库分别提供了Spring对Redis的支持和Jedis,一个Java客户端连接Redis。 ```xml <groupId>org.springframework.data <artifactId>...
1. **相关Jar文件**:为了实现SSH与Redis的整合,需要引入Redis客户端Jedis的jar包(如jedis-2.3.1.jar)以及Spring对Redis支持的相关jar包(如spring-data-redis-1.3.4.RELEASE.jar)。此外,还需要连接池管理的库...
本项目结合了Apache Shiro、JSON Web Token (JWT)、SpringBoot、MySQL数据库以及Redis缓存技术(通过Jedis客户端)来实现这一机制。下面我们将详细探讨这些组件在实现无状态鉴权中的作用。 **Apache Shiro** Apache...
在Spring框架中整合Redis,首先需要引入`spring-data-redis`的依赖,这个jar包包含了Spring对Redis的各种支持。Jedis是Redis官方推荐的Java客户端,所以在这个压缩包中包含的Jedis文件是连接和操作Redis服务器所必需...
1. `jedis.jar`: Jedis是Java的一个Redis客户端,提供了连接Redis服务器、执行命令、处理数据类型等功能。 2. `spring-data-redis.jar`: Spring Data Redis是Spring框架的一部分,它为Redis提供了Spring的模板和...
在“spring-data集成jedis测试代码”中,我们可以期待看到如何配置Spring Data Redis来使用Jedis客户端。这通常涉及以下步骤: 1. 添加Jedis和Spring Data Redis依赖到项目构建文件(如pom.xml或build.gradle)。 2....
将Redis与Spring整合,可以充分利用Redis的高速缓存能力,提升应用程序的性能。本文将详细介绍如何在Spring应用中集成Redis,以及如何操作String、list、set、map四种基本数据类型。 一、整合步骤 1. **环境配置**...
它支持Jedis和Lettuce两种Redis客户端库,提供了Template和Reactive API供开发者选择。 二、Spring Boot与Redis集成 在Spring Boot项目中整合Redis更加简便,只需在`pom.xml`或`build.gradle`文件中添加相关依赖,...
在Spring项目中,首先需要在pom.xml或build.gradle文件中引入Spring Data Redis和Jedis(或Lettuce)客户端库的依赖。例如,对于Maven项目: ```xml <groupId>org.springframework.boot <artifactId>spring-boot...
- 提供了对不同Redis客户端的整合(Lettuce和Jedis) - 提供了RedisTemplate统一API来操作Redis - 支持Redis的发布订阅模型 - 支持Redis哨兵和Redis集群 - 支持基于Lettuce的响应式编程 - 支持基于JDK、JSON、字符串...
在Java应用中,我们可以使用Jedis或者Lettuce等客户端库与Redis通信。在SSH框架中,Redis常用于缓存session、查询结果或者频繁更新的数据,提高数据读取速度。 5. **整合过程**: - 配置Redis连接:在Spring配置...
Redis 和 Spring 的整合主要涉及到如何在 Spring 应用中利用 Redis 实现缓存功能,以减轻数据库的负担。本文将详细介绍这一过程。 首先,我们来理解整合的目标。使用 Redis 缓存数据的主要目的是减少对数据库的频繁...
2. **RedisConnectionFactory**:Spring会使用这些配置创建`RedisConnectionFactory`,它是连接到Redis服务器的工厂类,可以配置为使用Jedis或Lettuce客户端。 3. **RedisTemplate**或`StringRedisTemplate`:基于`...
4. `jedis`: Redis的Java客户端库,Spring Data Redis通过Jedis与Redis服务器通信。 5. `redis-server`: Redis服务器本身,需要运行在本地或远程服务器上。 6. `lettuce`: 另一个可选的Redis Java客户端,也可以与...
Jedis是早期常用的Redis客户端,而Lettuce则提供了更现代的、基于Netty的非阻塞I/O支持,为高并发环境提供了更好的性能。 2. **Reactive支持**:1.7.6版本开始,Spring Data Redis引入了对Reactive编程模型的支持,...
Redis客户端则是与服务端交互的工具,Java中常见的客户端有Jedis和Lettuce,它们提供了丰富的API用于执行各种Redis命令。 四、Spring整合Redis 在Java应用中,Spring框架提供了方便的Redis集成方式: 1. 添加Spring...
以上就是关于Spring整合Redis的详细解析,涵盖了Spring模板、连接池、JSON序列化和Redis集群的配置与使用。在实际项目中,理解并熟练掌握这些知识点,能帮助我们构建出高效、稳定的Redis集成方案。
1. **添加依赖**:在项目中引入Spring Data Redis和Redis客户端库(如Jedis或Lettuce)的依赖。在Maven项目中,可以在pom.xml文件中添加对应的dependency。 2. **配置Redis**:在Spring的配置文件(如...