Redis客户端之Spring整合Jedis
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis客户端之Spring整合Jedis相关的知识,希望对你有一定的参考价值。
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
以上是关于Redis客户端之Spring整合Jedis的主要内容,如果未能解决你的问题,请参考以下文章 Redis + Jedis + Spring整合遇到的异常(转)