原文地址:http://blog.csdn.net/liuxiao723846/article/details/50401406
1、使用了jedis客戶端,對Redis進行了封裝,包括:
1)使用了redispool獲取連接;以及連接的回收;
2)常用五種數據結構的常用操作封裝;
- package redis.utils;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- //import org.apache.log4j.Logger;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- import redis.clients.jedis.SortingParams;
- import redis.clients.jedis.BinaryClient.LIST_POSITION;
- import redis.clients.util.SafeEncoder;
- /**
- * @author Mr.hu
- * @version crateTime:2013-10-30 下午5:41:30
- * Class Explain:JedisUtil
- */
- public class JedisUtil {
- //private Logger log = Logger.getLogger(this.getClass());
- /**緩存生存時間 */
- private final int expire = 60000;
- /** 操作Key的方法 */
- public Keys KEYS;
- /** 對存儲結構為String類型的操作 */
- public Strings STRINGS;
- /** 對存儲結構為List類型的操作 */
- public Lists LISTS;
- /** 對存儲結構為Set類型的操作 */
- public Sets SETS;
- /** 對存儲結構為HashMap類型的操作 */
- public Hash HASH;
- /** 對存儲結構為Set(排序的)類型的操作 */
- public SortSet SORTSET;
- private static JedisPool jedisPool = null;
- private JedisUtil() {
- }
- static {
- JedisPoolConfig config = new JedisPoolConfig();
- //控制一個pool可分配多少個jedis實例,通過pool.getResource()來獲取;
- //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。
- config.setMaxTotal(500);
- //控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例。
- config.setMaxIdle(5);
- //表示當borrow(引入)一個jedis實例時,最大的等待時間,如果超過等待時間,則直接拋出JedisConnectionException;
- config.setMaxWaitMillis(1000 * 100);
- //在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的;
- config.setTestOnBorrow(true);
- //redis如果設置了密碼:
- /*jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP,
- JRedisPoolConfig.REDIS_PORT,
- 10000,JRedisPoolConfig.REDIS_PASSWORD); */
- //redis未設置了密碼:
- jedisPool = new JedisPool(config, "172.30.37.73",6379);
- }
- public JedisPool getPool() {
- return jedisPool;
- }
- /**
- * 從jedis連接池中獲取獲取jedis對象
- * @return
- */
- public Jedis getJedis() {
- return jedisPool.getResource();
- }
- private static final JedisUtil jedisUtil = new JedisUtil();
- /**
- * 獲取JedisUtil實例
- * @return
- */
- public static JedisUtil getInstance() {
- return jedisUtil;
- }
- /**
- * 回收jedis(放到finally中)
- * @param jedis
- */
- public void returnJedis(Jedis jedis) {
- if (null != jedis && null != jedisPool) {
- jedisPool.returnResource(jedis);
- }
- }
- /**
- * 銷毀連接(放到catch中)
- * @param pool
- * @param jedis
- */
- public static void returnBrokenResource(Jedis jedis) {
- if (null != jedis && null != jedisPool) {
- jedisPool.returnResource(jedis);
- }
- }
- /**
- * 設置過期時間
- * @author ruan 2013-4-11
- * @param key
- * @param seconds
- */
- public void expire(String key, int seconds) {
- if (seconds <= 0) {
- return;
- }
- Jedis jedis = getJedis();
- jedis.expire(key, seconds);
- returnJedis(jedis);
- }
- /**
- * 設置默認過期時間
- * @author ruan 2013-4-11
- * @param key
- */
- public void expire(String key) {
- expire(key, expire);
- }
- //*******************************************Keys*******************************************//
- public class Keys {
- /**
- * 清空所有key
- */
- public String flushAll() {
- Jedis jedis = getJedis();
- String stata = jedis.flushAll();
- returnJedis(jedis);
- return stata;
- }
- /**
- * 更改key
- * @param String oldkey
- * @param String newkey
- * @return 狀態碼
- * */
- public String rename(String oldkey, String newkey) {
- return rename(SafeEncoder.encode(oldkey),
- SafeEncoder.encode(newkey));
- }
- /**
- * 更改key,僅當新key不存在時才執行
- * @param String oldkey
- * @param String newkey
- * @return 狀態碼
- * */
- public long renamenx(String oldkey, String newkey) {
- Jedis jedis = getJedis();
- long status = jedis.renamenx(oldkey, newkey);
- returnJedis(jedis);
- return status;
- }
- /**
- * 更改key
- * @param String oldkey
- * @param String newkey
- * @return 狀態碼
- * */
- public String rename(byte[] oldkey, byte[] newkey) {
- Jedis jedis = getJedis();
- String status = jedis.rename(oldkey, newkey);
- returnJedis(jedis);
- return status;
- }
- /**
- * 設置key的過期時間,以秒為單位
- * @param String key
- * @param 時間,已秒為單位
- * @return 影響的記錄數
- * */
- public long expired(String key, int seconds) {
- Jedis jedis = getJedis();
- long count = jedis.expire(key, seconds);
- returnJedis(jedis);
- return count;
- }
- /**
- * 設置key的過期時間,它是距歷元(即格林威治標准時間 1970 年 1 月 1 日的 00:00:00,格里高利歷)的偏移量。
- * @param String key
- * @param 時間,已秒為單位
- * @return 影響的記錄數
- * */
- public long expireAt(String key, long timestamp) {
- Jedis jedis = getJedis();
- long count = jedis.expireAt(key, timestamp);
- returnJedis(jedis);
- return count;
- }
- /**
- * 查詢key的過期時間
- * @param String key
- * @return 以秒為單位的時間表示
- * */
- public long ttl(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis=getJedis();
- long len = sjedis.ttl(key);
- returnJedis(sjedis);
- return len;
- }
- /**
- * 取消對key過期時間的設置
- * @param key
- * @return 影響的記錄數
- * */
- public long persist(String key) {
- Jedis jedis = getJedis();
- long count = jedis.persist(key);
- returnJedis(jedis);
- return count;
- }
- /**
- * 刪除keys對應的記錄,可以是多個key
- * @param String ... keys
- * @return 刪除的記錄數
- * */
- public long del(String... keys) {
- Jedis jedis = getJedis();
- long count = jedis.del(keys);
- returnJedis(jedis);
- return count;
- }
- /**
- * 刪除keys對應的記錄,可以是多個key
- * @param String .. keys
- * @return 刪除的記錄數
- * */
- public long del(byte[]... keys) {
- Jedis jedis = getJedis();
- long count = jedis.del(keys);
- returnJedis(jedis);
- return count;
- }
- /**
- * 判斷key是否存在
- * @param String key
- * @return boolean
- * */
- public boolean exists(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis=getJedis();
- boolean exis = sjedis.exists(key);
- returnJedis(sjedis);
- return exis;
- }
- /**
- * 對List,Set,SortSet進行排序,如果集合數據較大應避免使用這個方法
- * @param String key
- * @return List<String> 集合的全部記錄
- * **/
- public List<String> sort(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis=getJedis();
- List<String> list = sjedis.sort(key);
- returnJedis(sjedis);
- return list;
- }
- /**
- * 對List,Set,SortSet進行排序或limit
- * @param String key
- * @param SortingParams parame 定義排序類型或limit的起止位置.
- * @return List<String> 全部或部分記錄
- * **/
- public List<String> sort(String key, SortingParams parame) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis=getJedis();
- List<String> list = sjedis.sort(key, parame);
- returnJedis(sjedis);
- return list;
- }
- /**
- * 返回指定key存儲的類型
- * @param String key
- * @return String string|list|set|zset|hash
- * **/
- public String type(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis=getJedis();
- String type = sjedis.type(key);
- returnJedis(sjedis);
- return type;
- }
- /**
- * 查找所有匹配給定的模式的鍵
- * @param String key的表達式,*表示多個,?表示一個
- * */
- public Set<String> keys(String pattern) {
- Jedis jedis = getJedis();
- Set<String> set = jedis.keys(pattern);
- returnJedis(jedis);
- return set;
- }
- }
- //*******************************************Sets*******************************************//
- public class Sets {
- /**
- * 向Set添加一條記錄,如果member已存在返回0,否則返回1
- * @param String key
- * @param String member
- * @return 操作碼,0或1
- * */
- public long sadd(String key, String member) {
- Jedis jedis = getJedis();
- long s = jedis.sadd(key, member);
- returnJedis(jedis);
- return s;
- }
- public long sadd(byte[] key, byte[] member) {
- Jedis jedis = getJedis();
- long s = jedis.sadd(key, member);
- returnJedis(jedis);
- return s;
- }
- /**
- * 獲取給定key中元素個數
- * @param String key
- * @return 元素個數
- * */
- public long scard(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- long len = sjedis.scard(key);
- returnJedis(sjedis);
- return len;
- }
- /**
- * 返回從第一組和所有的給定集合之間的差異的成員
- * @param String ... keys
- * @return 差異的成員集合
- * */
- public Set<String> sdiff(String... keys) {
- Jedis jedis = getJedis();
- Set<String> set = jedis.sdiff(keys);
- returnJedis(jedis);
- return set;
- }
- /**
- * 這個命令等於sdiff,但返回的不是結果集,而是將結果集存儲在新的集合中,如果目標已存在,則覆蓋。
- * @param String newkey 新結果集的key
- * @param String ... keys 比較的集合
- * @return 新集合中的記錄數
- * **/
- public long sdiffstore(String newkey, String... keys) {
- Jedis jedis = getJedis();
- long s = jedis.sdiffstore(newkey, keys);
- returnJedis(jedis);
- return s;
- }
- /**
- * 返回給定集合交集的成員,如果其中一個集合為不存在或為空,則返回空Set
- * @param String ... keys
- * @return 交集成員的集合
- * **/
- public Set<String> sinter(String... keys) {
- Jedis jedis = getJedis();
- Set<String> set = jedis.sinter(keys);
- returnJedis(jedis);
- return set;
- }
- /**
- * 這個命令等於sinter,但返回的不是結果集,而是將結果集存儲在新的集合中,如果目標已存在,則覆蓋。
- * @param String newkey 新結果集的key
- * @param String ... keys 比較的集合
- * @return 新集合中的記錄數
- * **/
- public long sinterstore(String newkey, String... keys) {
- Jedis jedis = getJedis();
- long s = jedis.sinterstore(newkey, keys);
- returnJedis(jedis);
- return s;
- }
- /**
- * 確定一個給定的值是否存在
- * @param String key
- * @param String member 要判斷的值
- * @return 存在返回1,不存在返回0
- * **/
- public boolean sismember(String key, String member) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- boolean s = sjedis.sismember(key, member);
- returnJedis(sjedis);
- return s;
- }
- /**
- * 返回集合中的所有成員
- * @param String key
- * @return 成員集合
- * */
- public Set<String> smembers(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Set<String> set = sjedis.smembers(key);
- returnJedis(sjedis);
- return set;
- }
- public Set<byte[]> smembers(byte[] key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Set<byte[]> set = sjedis.smembers(key);
- returnJedis(sjedis);
- return set;
- }
- /**
- * 將成員從源集合移出放入目標集合 <br/>
- * 如果源集合不存在或不包哈指定成員,不進行任何操作,返回0<br/>
- * 否則該成員從源集合上刪除,並添加到目標集合,如果目標集合中成員已存在,則只在源集合進行刪除
- * @param String srckey 源集合
- * @param String dstkey 目標集合
- * @param String member 源集合中的成員
- * @return 狀態碼,1成功,0失敗
- * */
- public long smove(String srckey, String dstkey, String member) {
- Jedis jedis = getJedis();
- long s = jedis.smove(srckey, dstkey, member);
- returnJedis(jedis);
- return s;
- }
- /**
- * 從集合中刪除成員
- * @param String key
- * @return 被刪除的成員
- * */
- public String spop(String key) {
- Jedis jedis = getJedis();
- String s = jedis.spop(key);
- returnJedis(jedis);
- return s;
- }
- /**
- * 從集合中刪除指定成員
- * @param String key
- * @param String member 要刪除的成員
- * @return 狀態碼,成功返回1,成員不存在返回0
- * */
- public long srem(String key, String member) {
- Jedis jedis = getJedis();
- long s = jedis.srem(key, member);
- returnJedis(jedis);
- return s;
- }
- /**
- * 合並多個集合並返回合並后的結果,合並后的結果集合並不保存<br/>
- * @param String ... keys
- * @return 合並后的結果集合
- * @see sunionstore
- * */
- public Set<String> sunion(String... keys) {
- Jedis jedis = getJedis();
- Set<String> set = jedis.sunion(keys);
- returnJedis(jedis);
- return set;
- }
- /**
- * 合並多個集合並將合並后的結果集保存在指定的新集合中,如果新集合已經存在則覆蓋
- * @param String newkey 新集合的key
- * @param String ... keys 要合並的集合
- * **/
- public long sunionstore(String newkey, String... keys) {
- Jedis jedis = getJedis();
- long s = jedis.sunionstore(newkey, keys);
- returnJedis(jedis);
- return s;
- }
- }
- //*******************************************SortSet*******************************************//
- public class SortSet {
- /**
- * 向集合中增加一條記錄,如果這個值已存在,這個值對應的權重將被置為新的權重
- * @param String key
- * @param double score 權重
- * @param String member 要加入的值,
- * @return 狀態碼 1成功,0已存在member的值
- * */
- public long zadd(String key, double score, String member) {
- Jedis jedis = getJedis();
- long s = jedis.zadd(key, score, member);
- returnJedis(jedis);
- return s;
- }
- /*public long zadd(String key, Map<Double, String> scoreMembers) {
- Jedis jedis = getJedis();
- long s = jedis.zadd(key, scoreMembers);
- returnJedis(jedis);
- return s;
- }*/
- /**
- * 獲取集合中元素的數量
- * @param String key
- * @return 如果返回0則集合不存在
- * */
- public long zcard(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- long len = sjedis.zcard(key);
- returnJedis(sjedis);
- return len;
- }
- /**
- * 獲取指定權重區間內集合的數量
- * @param String key
- * @param double min 最小排序位置
- * @param double max 最大排序位置
- * */
- public long zcount(String key, double min, double max) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- long len = sjedis.zcount(key, min, max);
- returnJedis(sjedis);
- return len;
- }
- /**
- * 獲得set的長度
- *
- * @param key
- * @return
- */
- public long zlength(String key) {
- long len = 0;
- Set<String> set = zrange(key, 0, -1);
- len = set.size();
- return len;
- }
- /**
- * 權重增加給定值,如果給定的member已存在
- * @param String key
- * @param double score 要增的權重
- * @param String member 要插入的值
- * @return 增后的權重
- * */
- public double zincrby(String key, double score, String member) {
- Jedis jedis = getJedis();
- double s = jedis.zincrby(key, score, member);
- returnJedis(jedis);
- return s;
- }
- /**
- * 返回指定位置的集合元素,0為第一個元素,-1為最后一個元素
- * @param String key
- * @param int start 開始位置(包含)
- * @param int end 結束位置(包含)
- * @return Set<String>
- * */
- public Set<String> zrange(String key, int start, int end) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Set<String> set = sjedis.zrange(key, start, end);
- returnJedis(sjedis);
- return set;
- }
- /**
- * 返回指定權重區間的元素集合
- * @param String key
- * @param double min 上限權重
- * @param double max 下限權重
- * @return Set<String>
- * */
- public Set<String> zrangeByScore(String key, double min, double max) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Set<String> set = sjedis.zrangeByScore(key, min, max);
- returnJedis(sjedis);
- return set;
- }
- /**
- * 獲取指定值在集合中的位置,集合排序從低到高
- * @see zrevrank
- * @param String key
- * @param String member
- * @return long 位置
- * */
- public long zrank(String key, String member) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- long index = sjedis.zrank(key, member);
- returnJedis(sjedis);
- return index;
- }
- /**
- * 獲取指定值在集合中的位置,集合排序從高到低
- * @see zrank
- * @param String key
- * @param String member
- * @return long 位置
- * */
- public long zrevrank(String key, String member) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- long index = sjedis.zrevrank(key, member);
- returnJedis(sjedis);
- return index;
- }
- /**
- * 從集合中刪除成員
- * @param String key
- * @param String member
- * @return 返回1成功
- * */
- public long zrem(String key, String member) {
- Jedis jedis = getJedis();
- long s = jedis.zrem(key, member);
- returnJedis(jedis);
- return s;
- }
- /**
- * 刪除
- * @param key
- * @return
- */
- public long zrem(String key) {
- Jedis jedis = getJedis();
- long s = jedis.del(key);
- returnJedis(jedis);
- return s;
- }
- /**
- * 刪除給定位置區間的元素
- * @param String key
- * @param int start 開始區間,從0開始(包含)
- * @param int end 結束區間,-1為最后一個元素(包含)
- * @return 刪除的數量
- * */
- public long zremrangeByRank(String key, int start, int end) {
- Jedis jedis = getJedis();
- long s = jedis.zremrangeByRank(key, start, end);
- returnJedis(jedis);
- return s;
- }
- /**
- * 刪除給定權重區間的元素
- * @param String key
- * @param double min 下限權重(包含)
- * @param double max 上限權重(包含)
- * @return 刪除的數量
- * */
- public long zremrangeByScore(String key, double min, double max) {
- Jedis jedis = getJedis();
- long s = jedis.zremrangeByScore(key, min, max);
- returnJedis(jedis);
- return s;
- }
- /**
- * 獲取給定區間的元素,原始按照權重由高到低排序
- * @param String key
- * @param int start
- * @param int end
- * @return Set<String>
- * */
- public Set<String> zrevrange(String key, int start, int end) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Set<String> set = sjedis.zrevrange(key, start, end);
- returnJedis(sjedis);
- return set;
- }
- /**
- * 獲取給定值在集合中的權重
- * @param String key
- * @param memeber
- * @return double 權重
- * */
- public double zscore(String key, String memebr) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Double score = sjedis.zscore(key, memebr);
- returnJedis(sjedis);
- if (score != null)
- return score;
- return 0;
- }
- }
- //*******************************************Hash*******************************************//
- public class Hash {
- /**
- * 從hash中刪除指定的存儲
- * @param String key
- * @param String fieid 存儲的名字
- * @return 狀態碼,1成功,0失敗
- * */
- public long hdel(String key, String fieid) {
- Jedis jedis = getJedis();
- long s = jedis.hdel(key, fieid);
- returnJedis(jedis);
- return s;
- }
- public long hdel(String key) {
- Jedis jedis = getJedis();
- long s = jedis.del(key);
- returnJedis(jedis);
- return s;
- }
- /**
- * 測試hash中指定的存儲是否存在
- * @param String key
- * @param String fieid 存儲的名字
- * @return 1存在,0不存在
- * */
- public boolean hexists(String key, String fieid) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- boolean s = sjedis.hexists(key, fieid);
- returnJedis(sjedis);
- return s;
- }
- /**
- * 返回hash中指定存儲位置的值
- *
- * @param String key
- * @param String fieid 存儲的名字
- * @return 存儲對應的值
- * */
- public String hget(String key, String fieid) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- String s = sjedis.hget(key, fieid);
- returnJedis(sjedis);
- return s;
- }
- public byte[] hget(byte[] key, byte[] fieid) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- byte[] s = sjedis.hget(key, fieid);
- returnJedis(sjedis);
- return s;
- }
- /**
- * 以Map的形式返回hash中的存儲和值
- * @param String key
- * @return Map<Strinig,String>
- * */
- public Map<String, String> hgetAll(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Map<String, String> map = sjedis.hgetAll(key);
- returnJedis(sjedis);
- return map;
- }
- /**
- * 添加一個對應關系
- * @param String key
- * @param String fieid
- * @param String value
- * @return 狀態碼 1成功,0失敗,fieid已存在將更新,也返回0
- * **/
- public long hset(String key, String fieid, String value) {
- Jedis jedis = getJedis();
- long s = jedis.hset(key, fieid, value);
- returnJedis(jedis);
- return s;
- }
- public long hset(String key, String fieid, byte[] value) {
- Jedis jedis = getJedis();
- long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
- returnJedis(jedis);
- return s;
- }
- /**
- * 添加對應關系,只有在fieid不存在時才執行
- * @param String key
- * @param String fieid
- * @param String value
- * @return 狀態碼 1成功,0失敗fieid已存
- * **/
- public long hsetnx(String key, String fieid, String value) {
- Jedis jedis = getJedis();
- long s = jedis.hsetnx(key, fieid, value);
- returnJedis(jedis);
- return s;
- }
- /**
- * 獲取hash中value的集合
- *
- * @param String
- * key
- * @return List<String>
- * */
- public List<String> hvals(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- List<String> list = sjedis.hvals(key);
- returnJedis(sjedis);
- return list;
- }
- /**
- * 在指定的存儲位置加上指定的數字,存儲位置的值必須可轉為數字類型
- * @param String key
- * @param String fieid 存儲位置
- * @param String long value 要增加的值,可以是負數
- * @return 增加指定數字后,存儲位置的值
- * */
- public long hincrby(String key, String fieid, long value) {
- Jedis jedis = getJedis();
- long s = jedis.hincrBy(key, fieid, value);
- returnJedis(jedis);
- return s;
- }
- /**
- * 返回指定hash中的所有存儲名字,類似Map中的keySet方法
- * @param String key
- * @return Set<String> 存儲名稱的集合
- * */
- public Set<String> hkeys(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- Set<String> set = sjedis.hkeys(key);
- returnJedis(sjedis);
- return set;
- }
- /**
- * 獲取hash中存儲的個數,類似Map中size方法
- * @param String key
- * @return long 存儲的個數
- * */
- public long hlen(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- long len = sjedis.hlen(key);
- returnJedis(sjedis);
- return len;
- }
- /**
- * 根據多個key,獲取對應的value,返回List,如果指定的key不存在,List對應位置為null
- * @param String key
- * @param String ... fieids 存儲位置
- * @return List<String>
- * */
- public List<String> hmget(String key, String... fieids) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- List<String> list = sjedis.hmget(key, fieids);
- returnJedis(sjedis);
- return list;
- }
- public List<byte[]> hmget(byte[] key, byte[]... fieids) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- List<byte[]> list = sjedis.hmget(key, fieids);
- returnJedis(sjedis);
- return list;
- }
- /**
- * 添加對應關系,如果對應關系已存在,則覆蓋
- * @param Strin key
- * @param Map <String,String> 對應關系
- * @return 狀態,成功返回OK
- * */
- public String hmset(String key, Map<String, String> map) {
- Jedis jedis = getJedis();
- String s = jedis.hmset(key, map);
- returnJedis(jedis);
- return s;
- }
- /**
- * 添加對應關系,如果對應關系已存在,則覆蓋
- * @param Strin key
- * @param Map <String,String> 對應關系
- * @return 狀態,成功返回OK
- * */
- public String hmset(byte[] key, Map<byte[], byte[]> map) {
- Jedis jedis = getJedis();
- String s = jedis.hmset(key, map);
- returnJedis(jedis);
- return s;
- }
- }
- //*******************************************Strings*******************************************//
- public class Strings {
- /**
- * 根據key獲取記錄
- * @param String key
- * @return 值
- * */
- public String get(String key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- String value = sjedis.get(key);
- returnJedis(sjedis);
- return value;
- }
- /**
- * 根據key獲取記錄
- * @param byte[] key
- * @return 值
- * */
- public byte[] get(byte[] key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- byte[] value = sjedis.get(key);
- returnJedis(sjedis);
- return value;
- }
- /**
- * 添加有過期時間的記錄
- *
- * @param String key
- * @param int seconds 過期時間,以秒為單位
- * @param String value
- * @return String 操作狀態
- * */
- public String setEx(String key, int seconds, String value) {
- Jedis jedis = getJedis();
- String str = jedis.setex(key, seconds, value);
- returnJedis(jedis);
- return str;
- }
- /**
- * 添加有過期時間的記錄
- *
- * @param String key
- * @param int seconds 過期時間,以秒為單位
- * @param String value
- * @return String 操作狀態
- * */
- public String setEx(byte[] key, int seconds, byte[] value) {
- Jedis jedis = getJedis();
- String str = jedis.setex(key, seconds, value);
- returnJedis(jedis);
- return str;
- }
- /**
- * 添加一條記錄,僅當給定的key不存在時才插入
- * @param String key
- * @param String value
- * @return long 狀態碼,1插入成功且key不存在,0未插入,key存在
- * */
- public long setnx(String key, String value) {
- Jedis jedis = getJedis();
- long str = jedis.setnx(key, value);
- returnJedis(jedis);
- return str;
- }
- /**
- * 添加記錄,如果記錄已存在將覆蓋原有的value
- * @param String key
- * @param String value
- * @return 狀態碼
- * */
- public String set(String key, String value) {
- return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
- }
- /**
- * 添加記錄,如果記錄已存在將覆蓋原有的value
- * @param String key
- * @param String value
- * @return 狀態碼
- * */
- public String set(String key, byte[] value) {
- return set(SafeEncoder.encode(key), value);
- }
- /**
- * 添加記錄,如果記錄已存在將覆蓋原有的value
- * @param byte[] key
- * @param byte[] value
- * @return 狀態碼
- * */
- public String set(byte[] key, byte[] value) {
- Jedis jedis = getJedis();
- String status = jedis.set(key, value);
- returnJedis(jedis);
- return status;
- }
- /**
- * 從指定位置開始插入數據,插入的數據會覆蓋指定位置以后的數據<br/>
- * 例:String str1="123456789";<br/>
- * 對str1操作后setRange(key,4,0000),str1="123400009";
- * @param String key
- * @param long offset
- * @param String value
- * @return long value的長度
- * */
- public long setRange(String key, long offset, String value) {
- Jedis jedis = getJedis();
- long len = jedis.setrange(key, offset, value);
- returnJedis(jedis);
- return len;
- }
- /**
- * 在指定的key中追加value
- * @param String key
- * @param String value
- * @return long 追加后value的長度
- * **/
- public long append(String key, String value) {
- Jedis jedis = getJedis();
- long len = jedis.append(key, value);
- returnJedis(jedis);
- return len;
- }
- /**
- * 將key對應的value減去指定的值,只有value可以轉為數字時該方法才可用
- * @param String key
- * @param long number 要減去的值
- * @return long 減指定值后的值
- * */
- public long decrBy(String key, long number) {
- Jedis jedis = getJedis();
- long len = jedis.decrBy(key, number);
- returnJedis(jedis);
- return len;
- }
- /**
- * <b>可以作為獲取唯一id的方法</b><br/>
- * 將key對應的value加上指定的值,只有value可以轉為數字時該方法才可用
- * @param String key
- * @param long number 要減去的值
- * @return long 相加后的值
- * */
- public long incrBy(String key, long number) {
- Jedis jedis = getJedis();
- long len = jedis.incrBy(key, number);
- returnJedis(jedis);
- return len;
- }
- /**
- * 對指定key對應的value進行截取
- * @param String key
- * @param long startOffset 開始位置(包含)
- * @param long endOffset 結束位置(包含)
- * @return String 截取的值
- * */
- public String getrange(String key, long startOffset, long endOffset) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- String value = sjedis.getrange(key, startOffset, endOffset);
- returnJedis(sjedis);
- return value;
- }
- /**
- * 獲取並設置指定key對應的value<br/>
- * 如果key存在返回之前的value,否則返回null
- * @param String key
- * @param String value
- * @return String 原始value或null
- * */
- public String getSet(String key, String value) {
- Jedis jedis = getJedis();
- String str = jedis.getSet(key, value);
- returnJedis(jedis);
- return str;
- }
- /**
- * 批量獲取記錄,如果指定的key不存在返回List的對應位置將是null
- * @param String keys
- * @return List<String> 值得集合
- * */
- public List<String> mget(String... keys) {
- Jedis jedis = getJedis();
- List<String> str = jedis.mget(keys);
- returnJedis(jedis);
- return str;
- }
- /**
- * 批量存儲記錄
- * @param String keysvalues 例:keysvalues="key1","value1","key2","value2";
- * @return String 狀態碼
- * */
- public String mset(String... keysvalues) {
- Jedis jedis = getJedis();
- String str = jedis.mset(keysvalues);
- returnJedis(jedis);
- return str;
- }
- /**
- * 獲取key對應的值的長度
- * @param String key
- * @return value值得長度
- * */
- public long strlen(String key) {
- Jedis jedis = getJedis();
- long len = jedis.strlen(key);
- returnJedis(jedis);
- return len;
- }
- }
- //*******************************************Lists*******************************************//
- public class Lists {
- /**
- * List長度
- * @param String key
- * @return 長度
- * */
- public long llen(String key) {
- return llen(SafeEncoder.encode(key));
- }
- /**
- * List長度
- * @param byte[] key
- * @return 長度
- * */
- public long llen(byte[] key) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- long count = sjedis.llen(key);
- returnJedis(sjedis);
- return count;
- }
- /**
- * 覆蓋操作,將覆蓋List中指定位置的值
- * @param byte[] key
- * @param int index 位置
- * @param byte[] value 值
- * @return 狀態碼
- * */
- public String lset(byte[] key, int index, byte[] value) {
- Jedis jedis = getJedis();
- String status = jedis.lset(key, index, value);
- returnJedis(jedis);
- return status;
- }
- /**
- * 覆蓋操作,將覆蓋List中指定位置的值
- * @param key
- * @param int index 位置
- * @param String value 值
- * @return 狀態碼
- * */
- public String lset(String key, int index, String value) {
- return lset(SafeEncoder.encode(key), index,
- SafeEncoder.encode(value));
- }
- /**
- * 在value的相對位置插入記錄
- * @param key
- * @param LIST_POSITION 前面插入或后面插入
- * @param String pivot 相對位置的內容
- * @param String value 插入的內容
- * @return 記錄總數
- * */
- public long linsert(String key, LIST_POSITION where, String pivot,
- String value) {
- return linsert(SafeEncoder.encode(key), where,
- SafeEncoder.encode(pivot), SafeEncoder.encode(value));
- }
- /**
- * 在指定位置插入記錄
- * @param String key
- * @param LIST_POSITION 前面插入或后面插入
- * @param byte[] pivot 相對位置的內容
- * @param byte[] value 插入的內容
- * @return 記錄總數
- * */
- public long linsert(byte[] key, LIST_POSITION where, byte[] pivot,
- byte[] value) {
- Jedis jedis = getJedis();
- long count = jedis.linsert(key, where, pivot, value);
- returnJedis(jedis);
- return count;
- }
- /**
- * 獲取List中指定位置的值
- * @param String key
- * @param int index 位置
- * @return 值
- * **/
- public String lindex(String key, int index) {
- return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
- }
- /**
- * 獲取List中指定位置的值
- * @param byte[] key
- * @param int index 位置
- * @return 值
- * **/
- public byte[] lindex(byte[] key, int index) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- byte[] value = sjedis.lindex(key, index);
- returnJedis(sjedis);
- return value;
- }
- /**
- * 將List中的第一條記錄移出List
- * @param String key
- * @return 移出的記錄
- * */
- public String lpop(String key) {
- return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
- }
- /**
- * 將List中的第一條記錄移出List
- * @param byte[] key
- * @return 移出的記錄
- * */
- public byte[] lpop(byte[] key) {
- Jedis jedis = getJedis();
- byte[] value = jedis.lpop(key);
- returnJedis(jedis);
- return value;
- }
- /**
- * 將List中最后第一條記錄移出List
- *
- * @param byte[] key
- * @return 移出的記錄
- * */
- public String rpop(String key) {
- Jedis jedis = getJedis();
- String value = jedis.rpop(key);
- returnJedis(jedis);
- return value;
- }
- /**
- * 向List尾部追加記錄
- * @param String key
- * @param String value
- * @return 記錄總數
- * */
- public long lpush(String key, String value) {
- return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
- }
- /**
- * 向List頭部追加記錄
- * @param String key
- * @param String value
- * @return 記錄總數
- * */
- public long rpush(String key, String value) {
- Jedis jedis = getJedis();
- long count = jedis.rpush(key, value);
- returnJedis(jedis);
- return count;
- }
- /**
- * 向List頭部追加記錄
- * @param String key
- * @param String value
- * @return 記錄總數
- * */
- public long rpush(byte[] key, byte[] value) {
- Jedis jedis = getJedis();
- long count = jedis.rpush(key, value);
- returnJedis(jedis);
- return count;
- }
- /**
- * 向List中追加記錄
- * @param byte[] key
- * @param byte[] value
- * @return 記錄總數
- * */
- public long lpush(byte[] key, byte[] value) {
- Jedis jedis = getJedis();
- long count = jedis.lpush(key, value);
- returnJedis(jedis);
- return count;
- }
- /**
- * 獲取指定范圍的記錄,可以做為分頁使用
- * @param String key
- * @param long start
- * @param long end
- * @return List
- * */
- public List<String> lrange(String key, long start, long end) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- List<String> list = sjedis.lrange(key, start, end);
- returnJedis(sjedis);
- return list;
- }
- /**
- * 獲取指定范圍的記錄,可以做為分頁使用
- * @param byte[] key
- * @param int start
- * @param int end 如果為負數,則尾部開始計算
- * @return List
- * */
- public List<byte[]> lrange(byte[] key, int start, int end) {
- //ShardedJedis sjedis = getShardedJedis();
- Jedis sjedis = getJedis();
- List<byte[]> list = sjedis.lrange(key, start, end);
- returnJedis(sjedis);
- return list;
- }
- /**
- * 刪除List中c條記錄,被刪除的記錄值為value
- * @param byte[] key
- * @param int c 要刪除的數量,如果為負數則從List的尾部檢查並刪除符合的記錄
- * @param byte[] value 要匹配的值
- * @return 刪除后的List中的記錄數
- * */
- public long lrem(byte[] key, int c, byte[] value) {
- Jedis jedis = getJedis();
- long count = jedis.lrem(key, c, value);
- returnJedis(jedis);
- return count;
- }
- /**
- * 刪除List中c條記錄,被刪除的記錄值為value
- * @param String key
- * @param int c 要刪除的數量,如果為負數則從List的尾部檢查並刪除符合的記錄
- * @param String value 要匹配的值
- * @return 刪除后的List中的記錄數
- * */
- public long lrem(String key, int c, String value) {
- return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
- }
- /**
- * 算是刪除吧,只保留start與end之間的記錄
- * @param byte[] key
- * @param int start 記錄的開始位置(0表示第一條記錄)
- * @param int end 記錄的結束位置(如果為-1則表示最后一個,-2,-3以此類推)
- * @return 執行狀態碼
- * */
- public String ltrim(byte[] key, int start, int end) {
- Jedis jedis = getJedis();
- String str = jedis.ltrim(key, start, end);
- returnJedis(jedis);
- return str;
- }
- /**
- * 算是刪除吧,只保留start與end之間的記錄
- * @param String key
- * @param int start 記錄的開始位置(0表示第一條記錄)
- * @param int end 記錄的結束位置(如果為-1則表示最后一個,-2,-3以此類推)
- * @return 執行狀態碼
- * */
- public String ltrim(String key, int start, int end) {
- return ltrim(SafeEncoder.encode(key), start, end);
- }
- }
- public static void main(String[] args) {
- JedisUtil jedisUtil= JedisUtil.getInstance();
- JedisUtil.Strings strings=jedisUtil.new Strings();
- strings.set("nnn", "nnnn");
- System.out.println("-----"+strings.get("nnn"));
- Jedis jedis=JedisUtil.getInstance().getJedis();
- for (int i = 0; i < 10; i++) {
- jedis.set("test", "test");
- System.out.println(i+"=="+jedis.get("test"));
- }
- JedisUtil.getInstance().returnJedis(jedis);
- }
- }
2、序列化、反序列化:
redis服務器本身支持二進制安全的類型,所以可以把一個Java對象序列化后存儲到redis中。下面封裝了一個序列化、反序列化的工具類:
- package redis.utils;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- public class SerializeUtil {
- /**
- * 序列化
- *
- * @param object
- * @return
- */
- public static byte[] serialize(Object object) {
- ObjectOutputStream oos = null;
- ByteArrayOutputStream baos = null;
- try {
- // 序列化
- baos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(baos);
- oos.writeObject(object);
- byte[] bytes = baos.toByteArray();
- return bytes;
- } catch (Exception e) {
- }
- return null;
- }
- /**
- * 反序列化
- *
- * @param bytes
- * @return
- */
- public static Object unserialize(byte[] bytes) {
- ByteArrayInputStream bais = null;
- try {
- // 反序列化
- bais = new ByteArrayInputStream(bytes);
- ObjectInputStream ois = new ObjectInputStream(bais);
- return ois.readObject();
- } catch (Exception e) {
- }
- return null;
- }
- }
3、測試:
1)直接使用RedisUtils實例進行五大數據類型的操作:(這樣,使用完后會自動歸還到池子中)
- JedisUtil jedisUtil= JedisUtil.getInstance();
- JedisUtil.Strings strings=jedisUtil.new Strings();
- strings.set("nnn", "nnnn");
- System.out.println("-----"+strings.get("nnn"));
2)通過RedisUtil實例獲取Jedis連接對象;這樣就可以用原生的方式使用;最后使用完后需要手動將其歸還到池子中:
- Jedis jedis=JedisUtil.getInstance().getJedis();
- for (int i = 0; i < 10; i++) {
- jedis.set("test", "test");
- System.out.println(i+"=="+jedis.get("test"));
- }
- JedisUtil.getInstance().returnJedis(jedis);
3)將java對象存到redis中:
- Person p = new Person();
- p.setId(3);
- p.setName("測試");
- JedisUtil.Strings strings=jedisUtil.new Strings();
- strings.set("object3", SerializeUtil.serialize(p));
- //jedis.set(SafeEncoder.encode("object1"),SerializeUtil.serialize(p));
- byte[] personBy = jedis.get(SafeEncoder.encode("object3"));
- Person p1 = (Person) SerializeUtil.unserialize(personBy);
- System.out.println(p1.getName());
