redis在Java web项目的简单应用(转载)
看到一篇关于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
redis在Java web项目的简单应用(转载)的更多相关文章
- java web 项目中 简单定时器实现 Timer
java web 项目中 简单定时器实现 Timer 标签: Java定时器 2016-01-14 17:28 7070人阅读 评论(0) 收藏 举报 分类: JAVA(24) 版权声明:本文为博 ...
- java web项目最简单的结构
为了解“徒手”建立一个web应用,此博客建立简单过程 1.在任意一个目录下,建立一个文件夹,取名字 webDemo .这个应用名字. 2.在 webDemo 内建立一个 WEB-INF 文件夹,此处大 ...
- IDEA与Tomcat创建并运行Java Web项目及servlet的简单实现
创建Web项目 1. File -> New Project ,进入创建项目窗口 2.在 WEB-INF 目录下点击右键, New -> Directory ,创建 classes 和 ...
- 在cmd命令行使用Maven Archetype插件 generate命令创建简单的java web项目
前提: 1.下载apache-maven:https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache ...
- Idea创建简单Java Web项目并部署Servlet
1.打开Idea,创建JAVA Web项目 在WEB-INF目录下创建classes和lib文件夹 配置编译输出路径为刚才新建的classes文件夹 配置依赖jar包加载路径 添加tomcat ser ...
- 一个简单的Java Web项目搭建流程
今天试图在服务器上搭建一个web服务器,顺便回顾了java web项目的入门,使用Servlet处理HTTP请求,并记录日志等操作.当很久没有做过web项目时,有些东西还是很容易忘记的. Maven配 ...
- 一个完整Java Web项目背后的密码
前言 最近自己做了几个Java Web项目,有公司的商业项目,也有个人做着玩的小项目,写篇文章记录总结一下收获,列举出在做项目的整个过程中,所需要用到的技能和知识点,带给还没有真正接触过完整Java ...
- 做一个完整的Java Web项目需要掌握的技能[转]
转自:http://blog.csdn.net/JasonLiuLJX/article/details/51494048 最近自己做了几个Java Web项目,有公司的商业项目,也有个人做着玩的小项目 ...
- 一款基于SSM框架技术的全栈Java web项目(已部署可直接体验)
概述 此项目基于SSM框架技术的Java Web项目,是全栈项目,涉及前端.后端.插件.上线部署等各个板块,项目所有的代码都是自己编码所得,每一步.部分都有清晰的注释,完全不用担心代码混乱,可以轻松. ...
随机推荐
- jQuery的选择器中的通配符[id^='code']或[name^='code']
这两天在做一个专题的时候遇到了一个通配符的问题 //弹层操作$(function(){ //视频播放 $("a[href^='#video']").each(function(in ...
- php输出金字塔
<?php for($i=0;$i<10;$i++){ for($j=1;$j<=$i;$j++){ echo $i.'*'.$j.'|'; } echo '<br>'; ...
- 【转】Objective-C中一种消息处理方法performSelector: withObject:
原文 : http://www.cnblogs.com/buro79xxd/archive/2012/04/10/2440074.html Objective-C中调用函数的方法是“消息传递”,这 ...
- UIGestureRecognizer手势识别
UIGestureRecognizer 1.#import "ViewController.h"2.3.@interface ViewController ()<UIGest ...
- [个人原创]关于java中对象排序的一些探讨(二)
2. 使用Collections.sort()方法 Collections类中提供了诸多静态方法,诸如addAll(),max()等等.当自己相对Collection接口下的类处理的时候,可以看看这 ...
- 重新开始学习javase_Exception
“违例”(Exception)这个词表达的是一种“例外”情况,亦即正常情况之外的一种“异常”.在问题发生的时候,我们可能不知具体该如何解决,但肯定知道已不能不顾一切地继续下去.此时,必须坚决地停下来, ...
- VBoxManage 命令行使用
原文地址:http://cnjun939.blog.163.com/blog/static/78144538201251474311135/ 由于最近需研究virtualbox,看好看到上面的网址有, ...
- bash shell学习-实践 (自己实现一些小工具)
The poor starve while the rich feast. "穷人饥肠辘辘,富人大吃大喝" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux S ...
- 对于方法 String.Contains,只支持可在客户端上求值的参数。
var ProjectLevel_XJJS = "06,07,08,09"; p.Where(e =>ProjectLevel_XJJS.Contains(e.LevelCo ...
- js中的 window.location、document.location、document.URL 对像的区别(转载)
原文:http://www.cr173.com/html/18417_1.html 当我们需要对html网页进行转向的时候或是读取当前网页的时候可以用到下面三个对像: window.location. ...