redis在Java web專案的簡單應用
阿新 • • 發佈:2019-01-31
一.準備要使用的的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); } } }
三.配置spring xml檔案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; } }
<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;
}
}
原文