注:原始資料來自于享學(xué)課堂,,加上自己的理解和修改
目錄
1,、基本邏輯分析
1.1 文章投票功能模塊需求
1.2 數(shù)據(jù)庫設(shè)計(jì)
?1.3 使用到的數(shù)據(jù)類型和相關(guān)指令
1.4 key設(shè)計(jì)思路
1.5、記錄已投票用戶,,防重投
1.6 記錄文章分?jǐn)?shù)
1.7,、Redis緩存設(shè)計(jì)實(shí)戰(zhàn)
1.8,、緩存數(shù)據(jù)變化
2,、代碼實(shí)現(xiàn)
2.1文章處理相關(guān)接口
2.2 具體接口實(shí)現(xiàn)
2.3 redis的工具類
1、基本邏輯分析
1.1 文章投票功能模塊需求
- 用戶可以發(fā)表文章,發(fā)表時(shí)默認(rèn)給自己的文章投了一票
- 用戶在查看網(wǎng)站時(shí)可以按評分進(jìn)行排列查看 (想到排序就是zset)
- 用戶也可以按照文章發(fā)布時(shí)間進(jìn)行排序 (想到排序就是zset)
- 為節(jié)約內(nèi)存,,一篇文章發(fā)表后,,7天內(nèi)可以投票,7天過后就不能再投票了 (這里就是加上過期時(shí)間)
- 為防止同一用戶多次投票,用戶只能給一篇文章投一次票(重復(fù)就想到set)
1.2 數(shù)據(jù)庫設(shè)計(jì)
文章基本信息表 article
文章票數(shù)與分值表 vote_data
文章投票詳表 vote_details
1.3 使用到的數(shù)據(jù)類型和相關(guān)指令
指令如下:
- HASH:hset hincrBy hgetAll expire
- SET:sadd smembers
- ZSET:zadd zscore zincrby zrevrange
1.4 key設(shè)計(jì)思路
key的設(shè)計(jì)一般使用數(shù)據(jù)庫名+主鍵,這樣就能夠保證key的唯一性
比如:set user:1 123
1.5,、記錄已投票用戶,,防重投
- set:無序不重復(fù)
- zset:有序不重復(fù)
防止重復(fù)首先就想到set,zset類型數(shù)據(jù),,由于沒有排行等需求,,使用更加簡單的set就能滿足需求
1.6 記錄文章分?jǐn)?shù)
由于涉及到分?jǐn)?shù)的排行和文章發(fā)布時(shí)間的排行,,首先想到的就是使用zset保存數(shù)據(jù)(默認(rèn)分?jǐn)?shù)從小到大升序排序)
1.7、Redis緩存設(shè)計(jì)實(shí)戰(zhàn)
1.8,、緩存數(shù)據(jù)變化
2,、代碼實(shí)現(xiàn)
2.1文章處理相關(guān)接口
public interface RedisArticleService { * 文章發(fā)布,這里只是做個(gè)演示,一般存在數(shù)據(jù)庫中 String postArticle(String title, String content, String link, String userId); Map<String, String> hgetAll(String key); void articleVote(String userId, String articleId); String hget(String key, String votes); List<Map<String, String>> getArticles(int page, String order);
2.2 具體接口實(shí)現(xiàn)
代碼key說明:
文章1的投票人,,不能重復(fù)投,,使用set:jedis.sadd("voted:1" , userId);
文章對象信息,使用hash保存:jedis.hmset(article, articleData);
分?jǐn)?shù)排行,,看著排行,,就想到zset:jedis.zadd("score:info", now + Constants.VOTE_SCORE, article);
時(shí)間排行,看著排行,,就想到zset:jedis.zadd("time:", now, article);
public class RedisArticleServiceImpl implements RedisArticleService { private JedisUtils jedis; public String postArticle(String title, String content, String link, String userId) { //article:001// articleId=1 String articleId = String.valueOf(jedis.incr("article:")); String voted = "voted:" + articleId; jedis.sadd(voted, userId); jedis.expire(voted, Constants.ONE_WEEK_IN_SECONDS); long now = System.currentTimeMillis() / 1000; String article = "article:" + articleId; HashMap<String, String> articleData = new HashMap<String, String>(5); articleData.put("title", title); articleData.put("link", link); articleData.put("user", userId); articleData.put("now", String.valueOf(now)); articleData.put("votes", "1"); jedis.hmset(article, articleData); //增加分?jǐn)?shù)排行,,隨機(jī)初始化了一個(gè)分?jǐn)?shù)now + Constants.VOTE_SCORE jedis.zadd("score:info", now + Constants.VOTE_SCORE, article); jedis.zadd("time:", now, article); * @param article 文章ID(article:001) public void articleVote(String userId, String article) { long cutoff = (System.currentTimeMillis() / 1000) - Constants.ONE_WEEK_IN_SECONDS; //檢查是否還可以對文章進(jìn)行投票,如果該文章的發(fā)布時(shí)間比截止時(shí)間小,則已過期,,不能進(jìn)行投票 if (jedis.zscore("time:", article) < cutoff) { String articleId = article.substring(article.indexOf(':') + 1); //添加投票人,,投票人添加成功,才增加分?jǐn)?shù)和投票數(shù) if (jedis.sadd("voted:" + articleId, userId) == 1) { jedis.zincrby("score:info", Constants.VOTE_SCORE, article); jedis.hincrBy(article, "votes", 1L); public List<Map<String, String>> getArticles(int page, String key) { int start = (page - 1) * Constants.ARTICLES_PER_PAGE; int end = start + Constants.ARTICLES_PER_PAGE - 1; //倒序查詢出投票數(shù)最高的文章,,zset有序集合,,分值遞減 Set<String> ids = jedis.zrevrange(key, start, end); List<Map<String, String>> articles = new ArrayList<>(); Map<String, String> articleData = jedis.hgetAll(id); articleData.put("id", id); articles.add(articleData); public String hget(String key, String feild) { return jedis.hget(key, feild); public Map<String, String> hgetAll(String key) { return jedis.hgetAll(key);
2.3 redis的工具類
package com.james.cache.utils; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.Pipeline; import redis.clients.jedis.BinaryClient.LIST_POSITION; public class JedisUtils { private JedisPool pool = null; private String ip = "192.168.42.111"; private String auth = "12345678"; * 傳入ip和端口號構(gòu)建redis 連接 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxWaitMillis(100); config.setTestOnBorrow(true); pool = new JedisPool(config, this.ip, this.port, 100000, this.auth); * 通過key獲取儲存在redis中的value 并釋放連 * @return 成功返回value 失敗返回null public String get(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 向redis存入key和value,并釋放連接資 如果key已經(jīng)存在 則覆 public String set(String key, String value) { jedis = pool.getResource();//每次操作時(shí)向pool借用個(gè)jedis對象,用完即還? return jedis.set(key, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 向redis存入序列化的key和value,并釋放連接資 如果key已經(jīng)存在 則覆 public String setSerializer(byte[] keyBytes, byte[] valueBytes) { jedis = pool.getResource(); return jedis.set(keyBytes, valueBytes); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過序列化key獲取儲存在redis中的序列化value 并釋放連 * @return 成功返回value 失敗返回null public byte[] getSerializer(byte[] key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 刪除指定的key,也可以傳入一個(gè)包含key的數(shù) * 個(gè)key 也可以使 string 數(shù)組 public Long del(String... keys) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * @return 成功返回 添加后value的長 失敗 返回 添加 value 的長 異常返回0L public Long append(String key, String str) { jedis = pool.getResource(); res = jedis.append(key, str); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Boolean exists(String key) { jedis = pool.getResource(); return jedis.exists(key); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 設(shè)置key value,如果key已經(jīng)存在則返0,nx==> not exist * @return 成功返回1 如果存在 發(fā)生異常 返回 0 public Long setnx(String key, String value) { jedis = pool.getResource(); return jedis.setnx(key, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 設(shè)置key value并制定這個(gè)鍵值的有效 * @return 成功返回OK 失敗和異常返回null public String setex(String key, String value, int seconds) { jedis = pool.getResource(); res = jedis.setex(key, seconds, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key 和offset 從指定的位置始將原先value替換 下標(biāo)0,offset表示從offset下標(biāo)始替 public Long setrange(String key, String str, int offset) { jedis = pool.getResource(); return jedis.setrange(key, offset, str); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * string數(shù)組 也可以是個(gè)key * @return 成功返回value的集, 失敗返回null的集 ,異常返回 public List<String> mget(String... keys) { List<String> values = null; jedis = pool.getResource(); values = jedis.mget(keys); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 批量的設(shè)置key:value,可以 example: obj.mset(new * String[]{"key2","value1","key2","value2"}) * @return 成功返回OK 失敗 異常 返回 null public String mset(String... keysvalues) { jedis = pool.getResource(); res = jedis.mset(keysvalues); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * @return 成功返回value 失敗返回null public boolean mdel(List<String> keys) { jedis = pool.getResource();//從連接?用Jedis對象 Pipeline pipe = jedis.pipelined();//獲取jedis對象的pipeline對象 pipe.del(key); //將多個(gè)key放入pipe刪除指令 pipe.sync(); //執(zhí)行命令,,完全此時(shí)pipeline對象的遠(yuǎn)程調(diào) pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 批量的設(shè)置key:value,可以,如果key已經(jīng)存在則會失敗,操作會回 example: obj.msetnx(new * String[]{"key2","value1","key2","value2"}) public Long msetnx(String... keysvalues) { jedis = pool.getResource(); res = jedis.msetnx(keysvalues); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 設(shè)置key的?,并返回一個(gè)舊 * @return 舊? 如果key不存 則返回null public String getset(String key, String value) { jedis = pool.getResource(); res = jedis.getSet(key, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過下標(biāo) 和key 獲取指定下標(biāo)位置 value * 始位 0 負(fù)數(shù)表示從右邊開始截 public String getrange(String key, int startOffset, int endOffset) { jedis = pool.getResource(); res = jedis.getrange(key, startOffset, endOffset); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key 對value進(jìn)行加?+1操作,當(dāng)value不是int類型時(shí)會返回錯(cuò)誤,當(dāng)key不存在是則value1 public Long incr(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key給指定的value加?,如果key不存,則這是value為該 public Long incrBy(String key, Long integer) { jedis = pool.getResource(); res = jedis.incrBy(key, integer); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 對key的?做減減操作,如果key不存,則設(shè)置key-1 public Long decr(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Long decrBy(String key, Long integer) { jedis = pool.getResource(); res = jedis.decrBy(key, integer); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Long serlen(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key給field設(shè)置指定的?,如果key不存,則先創(chuàng)建 * @return 如果存在返回0 異常返回null public Long hset(String key, String field, String value) { jedis = pool.getResource(); res = jedis.hset(key, field, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key給field設(shè)置指定的?,如果key不存在則先創(chuàng),如果field已經(jīng)存在,返回0 public Long hsetnx(String key, String field, String value) { jedis = pool.getResource(); res = jedis.hsetnx(key, field, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key同時(shí)設(shè)置 hash的多個(gè)field public String hmset(String key, Map<String, String> hash) { jedis = pool.getResource(); res = jedis.hmset(key, hash); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public String hget(String key, String field) { jedis = pool.getResource(); res = jedis.hget(key, field); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key fields 獲取指定的value 如果沒有對應(yīng)的value則返回null * 可以 個(gè)String 也可以是 String數(shù)組 public List<String> hmget(String key, String... fields) { jedis = pool.getResource(); res = jedis.hmget(key, fields); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key給指定的field的value加上給定的? public Long hincrby(String key, String field, Long value) { jedis = pool.getResource(); res = jedis.hincrBy(key, field, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key和field判斷是否有指定的value存在 public Boolean hexists(String key, String field) { jedis = pool.getResource(); res = jedis.hexists(key, field); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Long hlen(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Map<String,String> hgetAll(String key) { Map<String,String> res = null; jedis = pool.getResource(); res = jedis.hgetAll(key); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以 field 也可以是 個(gè)數(shù) public Long hdel(String key, String... fields) { jedis = pool.getResource(); res = jedis.hdel(key, fields); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Set<String> zrange(String key, Long start, Long end) { jedis = pool.getResource(); res = jedis.zrange(key, start, end); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Set<String> hkeys(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key返回有和key有關(guān)的value public List<String> hvals(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Map<String, String> hgetall(String key) { Map<String, String> res = null; jedis = pool.getResource(); res = jedis.hgetAll(key); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以使一個(gè)string 也可以使string數(shù)組 * @return 返回list的value個(gè)數(shù) public Long lpush(String key, String... strs) { jedis = pool.getResource(); res = jedis.lpush(key, strs); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以使一個(gè)string 也可以使string數(shù)組 * @return 返回list的value個(gè)數(shù) public Long rpush(String key, String... strs) { jedis = pool.getResource(); res = jedis.rpush(key, strs); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key在list指定的位置之前或者之 添加字符串元 public Long linsert(String key, LIST_POSITION where, String pivot, String value) { jedis = pool.getResource(); res = jedis.linsert(key, where, pivot, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key設(shè)置list指定下標(biāo)位置的value * 如果下標(biāo)超過list里面value的個(gè)數(shù)則報(bào)錯(cuò) public String lset(String key, Long index, String value) { jedis = pool.getResource(); res = jedis.lset(key, index, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key從對應(yīng)的list中刪除指定的count value相同的元 * @return 返回被刪除的個(gè)數(shù) public Long lrem(String key, long count, String value) { jedis = pool.getResource(); res = jedis.lrem(key, count, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key保留list中從strat下標(biāo)始到end下標(biāo)結(jié)束的value public String ltrim(String key, long start, long end) { jedis = pool.getResource(); res = jedis.ltrim(key, start, end); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key從list的頭部刪除一個(gè)value,并返回該value public String lpop(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key從list尾部刪除個(gè)value,并返回該元素 public String rpop(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key從一個(gè)list的尾部刪除一個(gè)value并添加到另一個(gè)list的頭,并返回該value * 如果第一個(gè)list為空或?不存在則返回null public String rpoplpush(String srckey, String dstkey) { jedis = pool.getResource(); res = jedis.rpoplpush(srckey, dstkey); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key獲取list中指定下標(biāo)位置的value public String lindex(String key, long index) { jedis = pool.getResource(); res = jedis.lindex(key, index); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Long llen(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key獲取list指定下標(biāo)位置的value * 如果start 0 end -1 則返回全部的list中的value public List<String> lrange(String key, long start, long end) { jedis = pool.getResource(); res = jedis.lrange(key, start, end); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以是一個(gè)String 也可以是個(gè)String數(shù)組 public Long sadd(String key, String... members) { jedis = pool.getResource(); res = jedis.sadd(key, members); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public void expire(String key, int times) { jedis = pool.getResource(); jedis.expire(key, times); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key刪除set中對應(yīng)的value * 可以是一個(gè)String 也可以是個(gè)String數(shù)組 public Long srem(String key, String... members) { jedis = pool.getResource(); res = jedis.srem(key, members); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key隨機(jī)刪除個(gè)set中的value并返回該 public String spop(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以使一個(gè)string 則返回set中所有的value 也可以是string數(shù)組 public Set<String> sdiff(String... keys) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key獲取set中的差集并存入到另一個(gè)key * 可以使一個(gè)string 則返回set中所有的value 也可以是string數(shù)組 public Long sdiffstore(String dstkey, String... keys) { jedis = pool.getResource(); res = jedis.sdiffstore(dstkey, keys); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以使一個(gè)string 也可以是個(gè)string數(shù)組 public Set<String> sinter(String... keys) { jedis = pool.getResource(); res = jedis.sinter(keys); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key獲取指定set中的交集 并將結(jié)果存入新的set * 可以使一個(gè)string 也可以是個(gè)string數(shù)組 public Long sinterstore(String dstkey, String... keys) { jedis = pool.getResource(); res = jedis.sinterstore(dstkey, keys); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以使一個(gè)string 也可以是個(gè)string數(shù)組 public Set<String> sunion(String... keys) { jedis = pool.getResource(); res = jedis.sunion(keys); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key返回有set的并,并存入到新的set * 可以使一個(gè)string 也可以是個(gè)string數(shù)組 public Long sunionstore(String dstkey, String... keys) { jedis = pool.getResource(); res = jedis.sunionstore(dstkey, keys); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key將set中的value移除并添加到第二個(gè)set public Long smove(String srckey, String dstkey, String member) { jedis = pool.getResource(); res = jedis.smove(srckey, dstkey, member); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Long scard(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Boolean sismember(String key, String member) { jedis = pool.getResource(); res = jedis.sismember(key, member); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key獲取set中隨機(jī)的value,不刪除元 public String srandmember(String key) { jedis = pool.getResource(); res = jedis.srandmember(key); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public Set<String> smembers(String key) { jedis = pool.getResource(); res = jedis.smembers(key); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key向zset中添加value,score,其中score就是用來排序 如果該value已經(jīng)存在則根據(jù)score更新元素 public Long zadd(String key, Map<String,Double> scoreMembers) { jedis = pool.getResource(); res = jedis.zadd(key, scoreMembers); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key向zset中添加value,score,其中score就是用來排序 如果該value已經(jīng)存在則根據(jù)score更新元素 public Long zadd(String key, double score, String member) { jedis = pool.getResource(); res = jedis.zadd(key, score, member); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 可以使一個(gè)string 也可以是個(gè)string數(shù)組 public Long zrem(String key, String... members) { jedis = pool.getResource(); res = jedis.zrem(key, members); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key增加該zset中value的score的? public Double zincrby(String key, double score, String member) { jedis = pool.getResource(); res = jedis.zincrby(key, score, member); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public long hincrBy(String key, String field, long value) { jedis = pool.getResource(); res = jedis.hincrBy(key, field, value); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key返回zset中value的排 下標(biāo)從小到大排序 public Long zrank(String key, String member) { jedis = pool.getResource(); res = jedis.zrank(key, member); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key返回zset中value的排 下標(biāo)從大到小排序 public Long zrevrank(String key, String member) { jedis = pool.getResource(); res = jedis.zrevrank(key, member); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key將獲取score從start到end中zset的value socre從大到小排序 當(dāng)start0 end-1時(shí)返回全 public Set<String> zrevrange(String key, long start, long end) { jedis = pool.getResource(); res = jedis.zrevrange(key, start, end); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key返回指定score內(nèi)zset中的value public Set<String> zrangebyscore(String key, String max, String min) { jedis = pool.getResource(); res = jedis.zrevrangeByScore(key, max, min); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key返回指定score內(nèi)zset中的value public Set<String> zrangeByScore(String key, double max, double min) { jedis = pool.getResource(); res = jedis.zrevrangeByScore(key, max, min); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 返回指定區(qū)間內(nèi)zset中value的數(shù) public Long zcount(String key, String min, String max) { jedis = pool.getResource(); res = jedis.zcount(key, min, max); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key返回zset中的value個(gè)數(shù) public Long zcard(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key獲取zset中value的score public Double zscore(String key, String member) { jedis = pool.getResource(); res = jedis.zscore(key, member); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key刪除給定區(qū)間內(nèi)的元素 public Long zremrangeByRank(String key, long start, long end) { jedis = pool.getResource(); res = jedis.zremrangeByRank(key, start, end); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 通過key刪除指定score內(nèi)的元素 public Long zremrangeByScore(String key, double start, double end) { jedis = pool.getResource(); res = jedis.zremrangeByScore(key, start, end); pool.returnBrokenResource(jedis); returnResource(pool, jedis); * 返回滿足pattern表達(dá)式的有key keys(*) 返回有的key public Set<String> keys(String pattern) { jedis = pool.getResource(); res = jedis.keys(pattern); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public String type(String key) { jedis = pool.getResource(); pool.returnBrokenResource(jedis); returnResource(pool, jedis); public static void returnResource(JedisPool pool, Jedis jedis) { pool.returnResource(jedis);
|