看到一篇关于redis和spring集成的文章,实际测试后,可以。转载以备用。谢谢 亲昵YY!
html,body { font-size: 15px }
body { font-family: Helvetica, "Hiragino Sans GB", "微软雅黑", "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif; line-height: 1.6; margin: 0; padding: 1.33rem 1rem }
h1,h2,h3,h4,h5,h6 { margin: 1.33rem 0 0.667rem; padding: 0; font-weight: bold }
h1 { font-size: 1.4rem }
h2 { font-size: 1.33rem }
h3 { font-size: 1.2rem }
h4 { font-size: 1.13rem }
h5 { font-size: 1rem }
h6 { font-size: 1rem; color: #777777; margin: 1rem 0 }
div,p,ul,ol,dl,li { margin: 0 }
blockquote,table,pre,code { margin: 8px 0 }
ul,ol { padding-left: 2.13rem }
blockquote { padding: 0 0.8rem }
blockquote>:first-child { margin-top: 0 }
blockquote>:last-child { margin-bottom: 0 }
img { border: 0; max-width: 100%; height: auto !important; margin: 2px 0 }
table { border-collapse: collapse; border: 1px solid #bbbbbb }
td,th { padding: 4px 8px; border-collapse: collapse; border: 1px solid #bbbbbb }
html,body { font-size: 15px }
body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif; line-height: 1.6; color: ; background-color: ; margin: 0; padding: 1.33rem 1rem }
img { max-width: 100% }
html { height: 100% }
body { min-height: 100% }
.wiz-img-resize-handle { position: absolute; z-index: 1000; border: 1px solid black; background-color: white }
.wiz-img-resize-handle { width: 5px; height: 5px }
.wiz-img-resize-handle.lt { cursor: nw-resize }
.wiz-img-resize-handle.tm { cursor: n-resize }
.wiz-img-resize-handle.rt { cursor: ne-resize }
.wiz-img-resize-handle.lm { cursor: w-resize }
.wiz-img-resize-handle.rm { cursor: e-resize }
.wiz-img-resize-handle.lb { cursor: sw-resize }
.wiz-img-resize-handle.bm { cursor: s-resize }
.wiz-img-resize-handle.rb { cursor: se-resize }
.wiz-table-body.wiz-table-moving *,.wiz-table-body.wiz-table-moving *::before,.wiz-table-body.wiz-table-moving *::after { cursor: default !important }
td,th { position: relative }
#wiz-table-range-border { display: none; width: 0; height: 0; position: absolute; top: 0; left: 0; z-index: 105 }
#wiz-table-col-line,#wiz-table-row-line { display: none; background-color: #448aff; position: absolute; z-index: 120 }
#wiz-table-col-line { width: 1px; cursor: col-resize }
#wiz-table-row-line { height: 1px; cursor: row-resize }
#wiz-table-range-border_start,#wiz-table-range-border_range { display: none; width: 0; height: 0; position: absolute }
#wiz-table-range-border_start_top,#wiz-table-range-border_range_top { height: 2px; background-color: #448aff; position: absolute; top: 0; left: 0 }
#wiz-table-range-border_range_top { height: 1px }
#wiz-table-range-border_start_right,#wiz-table-range-border_range_right { width: 2px; background-color: #448aff; position: absolute; top: 0 }
#wiz-table-range-border_range_right { width: 1px }
#wiz-table-range-border_start_bottom,#wiz-table-range-border_range_bottom { height: 2px; background-color: #448aff; position: absolute; top: 0 }
#wiz-table-range-border_range_bottom { height: 1px }
#wiz-table-range-border_start_left,#wiz-table-range-border_range_left { width: 2px; background-color: #448aff; position: absolute; top: 0; left: 0 }
#wiz-table-range-border_range_left { width: 1px }
#wiz-table-range-border_start_dot,#wiz-table-range-border_range_dot { width: 5px; height: 5px; border: 2px solid rgb(255, 255, 255); background-color: #448aff; cursor: crosshair; position: absolute; z-index: 110 }
.wiz-table-tools { display: block; background-color: #fff; position: absolute; left: 0px; border: 1px solid #ddd; z-index: 130 }
.wiz-table-tools ul { list-style: none; padding: 0 }
.wiz-table-tools .wiz-table-menu-item { position: relative; float: left; margin: 5px 2px 5px 8px }
.wiz-table-tools .wiz-table-menu-item .wiz-table-menu-button { font-size: 15px; width: 20px; height: 20px; line-height: 20px; cursor: pointer; position: relative }
.wiz-table-tools i.editor-icon { font-size: 15px; color: #455a64 }
.wiz-table-tools .wiz-table-menu-item .wiz-table-menu-button i#wiz-menu-bg-demo { position: absolute; top: 1px; left: 0 }
.wiz-table-tools .wiz-table-menu-sub { position: absolute; display: none; width: 125px; padding: 5px 0; background: #fff; border: 1px solid #E0E0E0; top: 28px; left: -9px }
.wiz-table-tools .wiz-table-menu-sub>div { font-size: 15px }
.wiz-table-tools .wiz-table-menu-item.active .wiz-table-menu-sub { display: block }
.wiz-table-tools .wiz-table-menu-sub::before,.wiz-table-tools .wiz-table-menu-sub::after { position: absolute; content: " "; border-style: solid; border-color: transparent; border-bottom-color: #cccccc; left: 22px; margin-left: -14px; top: -8px; border-width: 0 8px 8px 8px; z-index: 10 }
.wiz-table-tools .wiz-table-menu-sub::after { border-bottom-color: #ffffff; top: -7px }
.wiz-table-tools .wiz-table-menu-sub-item { padding: 4px 12px; font-size: 14px }
.wiz-table-tools .wiz-table-menu-sub-item.split { border-top: 1px solid #E0E0E0 }
.wiz-table-tools .wiz-table-menu-sub-item:hover { background-color: #ececec }
.wiz-table-tools .wiz-table-menu-sub-item.disabled { color: #bbbbbb; cursor: default }
.wiz-table-tools .wiz-table-menu-sub-item.disabled:hover { background-color: transparent }
.wiz-table-tools .wiz-table-menu-item.wiz-table-cell-bg:hover .wiz-table-color-pad { display: block }
.wiz-table-tools .wiz-table-color-pad { display: none; padding: 10px; width: 85px; height: 88px; background-color: #fff; cursor: default }
.wiz-table-tools .wiz-table-color-pad>div { font-size: 15px }
.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item { display: inline-block; width: 15px; height: 15px; margin-right: 9px; position: relative }
.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item i.pad-demo { position: absolute; top: 3px; left: 0 }
.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item .icon-oblique_line { color: #cc0000 }
.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child { margin-right: 0 }
.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item.active i.editor-icon.icon-box { color: #448aff }
.wiz-table-tools .wiz-table-cell-align { display: none; padding: 10px; width: 85px; height: 65px; background-color: #fff; cursor: default }
.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item { display: inline-block; width: 15px; height: 15px; margin-right: 9px; position: relative }
.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child { margin-right: 0 }
.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item i.valign { position: absolute; top: 3px; left: 0; color: #d2d2d2 }
.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.valign { color: #a1c4ff }
.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.icon-box,.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.align { color: #448aff }
.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child,.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child { margin-right: 0 }
th.wiz-selected-cell-multi,td.wiz-selected-cell-multi { background: rgba(0,102,255,.05) }
th::before,td::before,#wiz-table-col-line::before,#wiz-table-range-border_start_right::before,#wiz-table-range-border_range_right::before { content: " "; position: absolute; top: 0; bottom: 0; right: -5px; width: 9px; cursor: col-resize; background: transparent; z-index: 100 }
th::after,td::after,#wiz-table-row-line::before,#wiz-table-range-border_start_bottom::before,#wiz-table-range-border_range_bottom::before { content: " "; position: absolute; left: 0; right: 0; bottom: -5px; height: 9px; cursor: row-resize; background: transparent; z-index: 100 }
.wiz-table-container { }
.wiz-table-body { position: relative; padding: 0 0 10px }
.wiz-table-body table { margin: 0; outline: none }
td,th { height: 28px; outline: none }
body pre.prettyprint { padding: 0 }
body pre.prettyprint.linenums { overflow: auto }
body pre.prettyprint.linenums ol.linenums { padding: 10px 10px 10px 40px !important }
一.准备要使用的的jar包,我使用了以下jar包,关于json的jar包是方便存储和转换redis用到的值;
二.编写加载redis是要用到的实例类
- package com.redis;
- import redis.clients.jedis.ShardedJedis;
- /**
- * @Title: RedisDataSource.java
- * @copyright
- * @Package com.cy.redis
- * @Description: 该接口用于获取redis客户端实例,通过该实例操作redis数据源
- * @author
- * @date 2016-10-21 下午04:22:41
- */
- public interface RedisDataSource {
- public abstract ShardedJedis getRedisClient();
- public void returnResource(ShardedJedis shardedJedis);
- public void returnResource(ShardedJedis shardedJedis,boolean broken);
- }
- package com.redis;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import redis.clients.jedis.ShardedJedis;
- import redis.clients.jedis.ShardedJedisPool;
- /**
- * @Title: RedisDataSourceImpl.java
- * @copyright
- * @Package com.cy.redis
- * @Description: 获取redis客户端实例,通过该实例操作redis数据源
- * @author
- * @date 2016-10-21 下午04:22:54
- */
- public class RedisDataSourceImpl implements RedisDataSource{
- private static final org.slf4j.Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class);
- @Autowired
- private ShardedJedisPool shardedJedisPool;
- public ShardedJedisPool getShardedJedisPool() {
- return shardedJedisPool;
- }
- public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
- this.shardedJedisPool = shardedJedisPool;
- }
- /**
- * 获取redis客户端
- */
- public ShardedJedis getRedisClient() {
- try {
- ShardedJedis shardedJedis=shardedJedisPool.getResource();
- return shardedJedis;
- } catch (Exception e) {
- log.error("getRedisClient ,error",e);
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 将资源返还给pool
- */
- @SuppressWarnings("deprecation")
- public void returnResource(ShardedJedis shardedJedis) {
- shardedJedisPool.returnResource(shardedJedis);
- }
- /**
- * 出现异常后返回资源给pool
- */
- @SuppressWarnings("deprecation")
- public void returnResource(ShardedJedis shardedJedis, boolean broken) {
- if(broken){
- shardedJedisPool.returnBrokenResource(shardedJedis);
- }else{
- shardedJedisPool.returnResource(shardedJedis);
- }
- }
- }
- package com.redis;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisShardInfo;
- import redis.clients.jedis.ShardedJedis;
- /**
- * @Title: RedisClientTemplate.java
- * @copyright
- * @Package com.cy.redis
- * @Description: 用于操作redis取值、存值操作的工具类
- * @author
- * @date 2016-10-21 下午04:21:59
- */
- public class RedisClientTemplate {
- private static final org.slf4j.Logger log=LoggerFactory.getLogger(RedisClientTemplate.class);
- @Autowired
- private RedisDataSource redisDataSource;
- public RedisDataSource getRedisDataSource() {
- return redisDataSource;
- }
- public void setRedisDataSource(RedisDataSource redisDataSource) {
- this.redisDataSource = redisDataSource;
- }
- public void disconnect(){
- ShardedJedis shardedJedis=redisDataSource.getRedisClient();
- shardedJedis.disconnect();
- }
- /**
- * @Description: 在redis里设置单个值
- * @author Mr.chen
- * @date 2016-10-21 下午04:37:06
- */
- public String set(String key, String value){
- String result=null;
- ShardedJedis shardedJedis=redisDataSource.getRedisClient();
- if(shardedJedis==null){
- return result;
- }
- boolean broken=false;
- try {
- result=shardedJedis.set(key, value);
- } catch (Exception e) {
- broken=true;
- e.printStackTrace();
- }finally{
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * @Description: 获取redis单个值
- * @author Mr.chen
- * @date 2016-10-21 下午04:40:57
- */
- public String get(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.get(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * @Description: 判断redis是否存在key
- * @author Mr.chen
- * @date 2016-10-21 下午04:41:39
- */
- public Boolean exists(String key) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.exists(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * @Description: 获取key返回存储值的类型
- * @author Mr.chen
- * @date 2016-10-21 下午04:42:08
- */
- public String type(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.type(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * @Description: 在某段时间后实现
- * @author Mr.chen
- * @date 2016-10-21 下午04:43:25
- */
- public Long expire(String key, int seconds) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.expire(key, seconds);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * @Description: 在某个时间点失效
- * @author Mr.chen
- * @date 2016-10-21 下午04:43:40
- */
- public Long expireAt(String key, long unixTime) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.expireAt(key, unixTime);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * @Description: 以秒为单位,返回给定 key 的剩余生存时间
- * @author Mr.chen
- * @date 2016-10-21 下午04:44:00
- */
- public Long ttl(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.ttl(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * @Description: 将 key 的值设为 value ,当且仅当 key 不存在
- * @author Mr.chen
- * @date 2016-10-21 下午04:44:17
- */
- public Long setnx(String key, String value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.setnx(key, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)
- * @param key
- * @param seconds
- * @param value
- * @return
- */
- public String setex(String key, int seconds, String value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.setex(key, seconds, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将 key 所储存的值减去减量 integer
- * @param key
- * @param integer
- * @return
- */
- public Long decrBy(String key, long integer) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.decrBy(key, integer);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将 key 中储存的数字值减一。
- * @param key
- * @return
- */
- public Long decr(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.decr(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将 key 所储存的值加上增量 integer
- * @param key
- * @param integer
- * @return
- */
- public Long incrBy(String key, long integer) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.incrBy(key, integer);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将 key 中储存的数字值增一
- * @param key
- * @return
- */
- public Long incr(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.incr(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。
- * 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。
- * @param key
- * @param value
- * @return
- */
- public Long append(String key, String value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.append(key, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回名称为key的string的value的子串
- * @param key
- * @param start
- * @param end
- * @return
- */
- public String substr(String key, int start, int end) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.substr(key, start, end);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将哈希表 key 中的域 field 的值设为 value
- * @param key
- * @param field
- * @param value
- * @return
- */
- public Long hset(String key, String field, String value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hset(key, field, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回哈希表 key 中给定域 field 的值
- * @param key
- * @param field
- * @return
- */
- public String hget(String key, String field) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hget(key, field);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 同时将多个 field-value (域-值)对设置到哈希表 key 中。
- * @param key
- * @param hash
- * @return
- */
- public String hmset(String key, Map<String, String> hash) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hmset(key, hash);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回哈希表 key 中,一个或多个给定域的值
- * @param key
- * @param fields
- * @return
- */
- public List<String> hmget(String key, String... fields) {
- List<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hmget(key, fields);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 为哈希表 key 中的域 field 的值加上增量 value
- * @param key
- * @param field
- * @param value
- * @return
- */
- public Long hincrBy(String key, String field, long value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hincrBy(key, field, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 查看哈希表 key 中,给定域 field 是否存在。
- * @param key
- * @param field
- * @return
- */
- public Boolean hexists(String key, String field) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hexists(key, field);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 删除key
- * @param key
- * @return
- */
- public Long del(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.del(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 删除哈希表 key 中的一个或多个指定域。
- * @param key
- * @param field
- * @return
- */
- public Long hdel(String key, String field) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hdel(key, field);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回哈希表 key 中域的数量。
- * @param key
- * @return
- */
- public Long hlen(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hlen(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回哈希表 key 中的所有域。
- * @param key
- * @return
- */
- public Set<String> hkeys(String key) {
- Set<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hkeys(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回哈希表 key 中所有域的值。
- * @param key
- * @return
- */
- public List<String> hvals(String key) {
- List<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hvals(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回哈希表 key 中,所有的域和值。
- * @param key
- * @return
- */
- public Map<String, String> hgetAll(String key) {
- Map<String, String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hgetAll(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- // ================list ====== l表示 list或 left, r表示right====================
- /**
- * 将一个或多个值 value 插入到列表 key 的表尾(最右边)
- * @param key
- * @param string
- * @return
- */
- public Long rpush(String key, String string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.rpush(key, string);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将一个或多个值 value 插入到列表 key 的表头
- * @param key
- * @param string
- * @return
- */
- public Long lpush(String key, String string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lpush(key, string);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回列表 key 的长度。
- * @param key
- * @return
- */
- public Long llen(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.llen(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定
- * @param key
- * @param start
- * @param end
- * @return
- */
- public List<String> lrange(String key, long start, long end) {
- List<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lrange(key, start, end);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 只保留指定区间内的元素,不在指定区间之内的元素都将被删除
- * @param key
- * @param start
- * @param end
- * @return
- */
- public String ltrim(String key, long start, long end) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.ltrim(key, start, end);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回列表 key 中,下标为 index 的元素。
- * @param key
- * @param index
- * @return
- */
- public String lindex(String key, long index) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lindex(key, index);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将列表 key 下标为 index 的元素的值设置为 value
- * @param key
- * @param index
- * @param value
- * @return
- */
- public String lset(String key, long index, String value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lset(key, index, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 移除并返回列表 key 的头元素
- * @param key
- * @return
- */
- public String lpop(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lpop(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 移除并返回列表 key 的尾元素。
- * @param key
- * @return
- */
- public String rpop(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.rpop(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- //return 1 add a not exist value ,
- //return 0 add a exist value
- /**
- * 将一个或多个 member 元素加入到集合 key 当中
- * @param key
- * @param member
- * @return
- */
- public Long sadd(String key, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.sadd(key, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回集合 key 中的所有成员。
- * @param key
- * @return
- */
- public Set<String> smembers(String key) {
- Set<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.smembers(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回集合 key 的基数(集合中元素的数量)
- * @param key
- * @return
- */
- public Long scard(String key) {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- Long result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.scard(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 将一个或多个 member 元素及其 score 值加入到有序集 key 当中
- * @param key
- * @param score
- * @param member
- * @return
- */
- public Long zadd(String key, double score, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zadd(key, score, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回有序集 key 中,指定区间内的成员
- * @param key
- * @param start
- * @param end
- * @return
- */
- public Set<String> zrange(String key, int start, int end) {
- Set<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zrange(key, start, end);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 移除有序集 key 中的一个或多个成员,不存在的成员将被忽略
- * @param key
- * @param member
- * @return
- */
- public Long zrem(String key, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zrem(key, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 为有序集 key 的成员 member 的 score 值加上增量 member 。
- * @param key
- * @param score
- * @param member
- * @return
- */
- public Double zincrby(String key, double score, String member) {
- Double result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zincrby(key, score, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从小到大)顺序排列
- * @param key
- * @param member
- * @return
- */
- public Long zrank(String key, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zrank(key, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回有序集 key 的基数
- * @param key
- * @return
- */
- public Long zcard(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zcard(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回有序集 key 中,成员 member 的 score 值。
- * @param key
- * @param member
- * @return
- */
- public Double zscore(String key, String member) {
- Double result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zscore(key, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量
- * @param key
- * @param min
- * @param max
- * @return
- */
- public Long zcount(String key, double min, double max) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zcount(key, min, max);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 批量存储
- * @param key
- * @param value
- * @return
- */
- public String set(byte[] key, byte[] value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.set(key, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 获取多个key的值
- * @param key
- * @return
- */
- public byte[] get(byte[] key) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.get(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 判断多个key存在
- * @param key
- * @return
- */
- public Boolean exists(byte[] key) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.exists(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long expire(byte[] key, int seconds) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.expire(key, seconds);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long expireAt(byte[] key, long unixTime) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.expireAt(key, unixTime);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long ttl(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.ttl(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long append(byte[] key, byte[] value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.append(key, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 批量增加到hash
- * @param key
- * @param field
- * @param value
- * @return
- */
- public Long hset(byte[] key, byte[] field, byte[] value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hset(key, field, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 批量获取field域值
- * @param key
- * @param field
- * @return
- */
- public byte[] hget(byte[] key, byte[] field) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hget(key, field);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public String hmset(byte[] key, Map<byte[], byte[]> hash) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hmset(key, hash);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public List<byte[]> hmget(byte[] key, byte[]... fields) {
- List<byte[]> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hmget(key, fields);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Boolean hexists(byte[] key, byte[] field) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hexists(key, field);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 批量删除hash的key
- * @param key
- * @param field
- * @return
- */
- public Long hdel(byte[] key, byte[] field) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hdel(key, field);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long rpush(byte[] key, byte[] string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.rpush(key, string);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long lpush(byte[] key, byte[] string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lpush(key, string);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long llen(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.llen(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public List<byte[]> lrange(byte[] key, int start, int end) {
- List<byte[]> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lrange(key, start, end);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public String lset(byte[] key, int index, byte[] value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lset(key, index, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long lrem(byte[] key, int count, byte[] value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lrem(key, count, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public byte[] lpop(byte[] key) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lpop(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public byte[] rpop(byte[] key) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.rpop(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- /**
- * 批量增加到set
- * @param key
- * @param member
- * @return
- */
- public Long sadd(byte[] key, byte[] member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.sadd(key, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Set<byte[]> smembers(byte[] key) {
- Set<byte[]> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.smembers(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long scard(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.scard(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long zadd(byte[] key, double score, byte[] member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zadd(key, score, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long zcard(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zcard(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public JedisShardInfo getShardInfo(String key) {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- JedisShardInfo result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.getShardInfo(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Collection<JedisShardInfo> getAllShardInfo() {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- Collection<JedisShardInfo> result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.getAllShardInfo();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Collection<Jedis> getAllShards() {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- Collection<Jedis> result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.getAllShards();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- }
三.配置spring xml文件
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
- <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="blockWhenExhausted" value="false"/>
- </bean>
- <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
- <constructor-arg index="0" ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg name="host" value="127.0.0.1" />
- <constructor-arg name="port" value="6379" />
- <constructor-arg name="timeout" value="10000" />
- </bean>
- </list>
- </constructor-arg>
- </bean>
- <bean id="redisDataSource" class="redis.dao.RedisDataSourceImpl">
- <property name="shardedJedisPool" ref="shardedJedisPool"></property>
- </bean>
- <bean id="redisClientTemplate" class="redis.dao.RedisClientTemplate">
- <property name="redisDataSource" ref="redisDataSource"></property>
- </bean>
- </beans>
四.代码中的使用,原则就是在执行service时,先通过key到redis里检测是否存在,如果存在则在redis里去数据,如果没有则到数据库去数据,然后存到redis里
- public class StudentinfoServiceImpl implements StudentinfoService{
- private StudentinfoDao studentinfoDao;
- private RedisClientTemplate redisClientTemplate;
- private static final ObjectMapper mapper=new ObjectMapper();
- public List<Studentinfo> getStudentinfoByStu(Studentinfo stu) {
- String key="getStudentinfoByStu:usernumber:"+stu.getUsernumber()+":name:"+stu.getName()+":idnumber:"+stu.getIdnumber();
- try {
- boolean flag=redisClientTemplate.exists(key);
- if(flag){
- String stuJsonStr=redisClientTemplate.get(key);
- List<Studentinfo> list=mapper.readValue(stuJsonStr, new TypeReference<List<Studentinfo>>() {});
- return list;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- String hql="from Studentinfo where 1=1";
- if(stu!=null){
- if(stu.getUsernumber()!=null){
- hql+=" and usernumber like '%"+stu.getUsernumber()+"%' ";
- }
- }
- List<Studentinfo> list = studentinfoDao.getStudentinfoByStu(hql);
- try {
- String stuJsonStr=mapper.writeValueAsString(list);
- redisClientTemplate.setex(key, 60, stuJsonStr);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return list;
- }
- public StudentinfoDao getStudentinfoDao() {
- return studentinfoDao;
- }
- public void setStudentinfoDao(StudentinfoDao studentinfoDao) {
- this.studentinfoDao = studentinfoDao;
- }
- public RedisClientTemplate getRedisClientTemplate() {
- return redisClientTemplate;
- }
- public void setRedisClientTemplate(RedisClientTemplate redisClientTemplate) {
- this.redisClientTemplate = redisClientTemplate;
- }
- }
来源: http://blog.csdn.net/u012498149/article/details/53170719