Redis封裝工具類
阿新 • • 發佈:2019-01-26
引用到的jedis的封裝類,然後又加了一層封裝
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.JedisCommands;
import com.pccc.touda.cache.api.IRedisCache;
import com.pccc.touda.cache.impl.RedisClusterImpl;
public class TSPRedisClusterUtil {
private static IRedisCache cache = new RedisClusterImpl();
/**TSP系統 存放基礎引數資訊*/
public static final String TSP_CACHE = "jedisCluster";
/**
*
* @return 返回Jredis操作客戶端
*/
public static JedisCommands getResource(String redisName){
return cache.getResource(redisName);
}
/**
* 儲存資料到快取中,若key已存在則覆蓋 value的長度不能超過1073741824 bytes (1 GB)
*
* @param key
* @param value
* @return
*/
public static String set(String redisName, String key, String value){
return cache.set(redisName, key, value);
}
/**
* 為指定的 key 設定值及其過期時間。如果 key 已經存在,將會替換舊的值
*
* @param key
* @param value
* @param millSeconds 超時時間,單位毫秒
* @return
*/
public static String setex(String redisName, String key, String value, long millSeconds){
return cache.getResource(redisName).psetex(key, millSeconds, value);
}
/**
* 儲存資料到快取中,若key不存在才設定 value的長度不能超過1073741824 bytes (1 GB)
*
* @param key
* @param value
* @param seconds 超時時間,單位毫秒
* @return
*/
public static String set(String redisName, String key, String value, long millSeconds){
return cache.set(redisName, key, value, millSeconds);
}
/**
* 儲存資料到快取中,若key存在才設定 value的長度不能超過1073741824 bytes (1 GB)
*
* @param key
* @param value
* @param seconds 超時時間,單位毫秒
* @return
*/
public static String setIfExist(String redisName, String key, String value, long millSeconds){
return cache.setIfExist(redisName, key, value, millSeconds);
}
/**
* 放入hash結構
* @param key
* @param map
* @return
*/
public static String setHash(String redisName, String key,Map<String,String> map)
{
return cache.getResource(redisName).hmset(key, map);
}
/**
* 取出redis鍵對應的hash下的鍵的值得集合
* @param key
* @param ids map的鍵
* @return
*/
public static List<String> getHash(String redisName, String key,String[] ids)
{
return cache.getResource(redisName).hmget(key, ids);
}
/**
* 根據key獲取map
* @param key
* @return
*/
public static Map<String,String> getHashAll(String redisName, String key)
{
return cache.getResource(redisName).hgetAll(key);
}
/**
* list中從左側放入值
* @param key
* @param value
* @return
*/
public static Long setLList(String redisName, String key,String... value)
{
return cache.getResource(redisName).lpush(key, value);
}
/**
* 從右側插入值到list
* @param key
* @param value
* @return
*/
public static Long setRList(String redisName, String key,String... value)
{
return cache.getResource(redisName).rpush(key, value);
}
/**
* 按索引取出list的值
* @param key
* @param startIndex
* @param endIndex -1表示所有
* @return
*/
public static List<String> rengeList(String redisName, String key,long startIndex,long endIndex)
{
return cache.getResource(redisName).lrange(key, startIndex, endIndex);
}
/**
* 刪除list頭部元素
* @param key
* @return
*/
public static String delrList(String redisName, String key)
{
return cache.getResource(redisName).rpop(key);
}
/**
* 刪除list尾部元素
* @param key
* @return
*/
public static String dellList(String redisName, String key)
{
return cache.getResource(redisName).lpop(key);
}
/**
* 從快取中根據key取得其String型別的值,如果key不存在則返回null,如果key存在但value不是string型別的,
* 則返回一個error。這個方法只能從快取中取得value為string型別的值。
*
* @param key
* @return
*/
public static String get(String redisName, String key){
return cache.get(redisName, key);
}
/**
* 檢查某個key是否在快取中存在,如果存在返回true,否則返回false;需要注意的是,即使該key所對應的value是一個空字串,
* 也依然會返回true。
*
* @param key
* @return
*/
public static Boolean exists(String redisName, String key){
return cache.exists(redisName, key);
}
/**
* 刪除一個Key,如果刪除的key不存在,則直接忽略。
*
* @param key
* @return 被刪除的keys的數量
*/
public static Long del(String redisName, String key){
return cache.del(redisName, key);
}
/**
* 為key設定一個特定的過期時間,單位為秒。過期時間一到,redis將會從快取中刪除掉該key。
* 即使是有過期時間的key,redis也會在持久化時將其寫到硬碟中,並把相對過期時間改為絕對的Unix過期時間。
* 在一個有設定過期時間的key上重複設定過期時間將會覆蓋原先設定的過期時間。
*
* @param key
* @param seconds
* @return 返回1表示成功設定過期時間,返回0表示key不存在。
*/
public static Long expire(String redisName, String key, int seconds){
return cache.expire(redisName, key, seconds);
}
/**
* 返回一個key還能活多久,單位為秒
*
* @param key
* @return 如果該key本來並沒有設定過期時間,則返回-1,如果該key不存在,則返回-2
*/
public static Long ttl(String redisName, String key){
return cache.ttl(redisName, key);
}
/**
* 設定hash表裡field欄位的值為value。如果key不存在,則建立一個新的hash表
*
* @param key
* @param field
* @param value
* @return 如果該欄位已經存在,那麼將會更新該欄位的值,返回0.如果欄位不存在,則新建立一個並且返回1.
*/
public static Long hset(String redisName, String key, String field, String value){
return cache.hset(redisName, key, field, value);
}
/**
* 刪除hash中指定欄位
*
* @param key
* @param field
* @return 刪除成功返回1, 刪除不成功返回0
*/
public static Long hdel(String redisName, String key, String field){
return hdel(redisName, key, field);
}
/**
* 刪除hash中指定欄位
*
* @param key
* @param field
* @return 刪除成功返回1, 刪除不成功返回0
*/
public static Long hdel(String redisName, String key, String... field){
return cache.getResource(redisName).hdel(key, field);
}
/**
* 如果該key對應的值是一個Hash表,則返回對應欄位的值。 如果不存在該欄位,或者key不存在,則返回一個"nil"值。
*
* @param key
* @param field
* @return
*/
public static String hget(String redisName, String key, String field){
return cache.hget(redisName, key, field);
}
/**
* @param redisName
* @param key
* @return
*/
public static Map<String, String> hgetAll(String redisName, String key){
return cache.getResource(redisName).hgetAll(key);
}
/**
* 向Set結構中新增 members
* @param redisName
* @param key
* @param members
* @return
*/
public static Long sadd(String redisName, String key,String...members){
JedisCommands commands = getResource(redisName);
return commands.sadd(key, members);
}
/**
* 從set結構中移除 members
* @param redisName
* @param key
* @param members
* @return
*/
public static Long srem(String redisName, String key,String...members){
JedisCommands commands = getResource(redisName);
return commands.srem(key, members);
}
/**
* 返回Set的所有成員
* @param redisName
* @param key
* @return
*/
public static Set<String> smembers(String redisName, String key ){
JedisCommands commands = getResource(redisName);
return commands.smembers(key);
}
/**
* 獲取集合的成員數
* @param redisName
* @param key
* @return
*/
public static Long scard(String redisName, String key ){
JedisCommands commands = getResource(redisName);
return commands.scard(key);
}
/**
* 返回某個key所儲存的資料型別,返回的資料型別有可能是"none", "string", "list", "set", "zset",
* "hash". "none"代表key不存在。
*
* @param key
* @return
*/
public static String type(String redisName, String key){
return cache.type(redisName, key);
}
/**
* 將指令碼 script 新增到指令碼快取中,但並不立即執行這個指令碼
* @param redisName
* @param script
* @param key
* @return
*/
public static String scriptLoad (String redisName, String script, String key) {
return cache.getResource(redisName).scriptLoad(script, key);
}
/**
* 將指令碼新增到指令碼快取中,並立即對輸入的指令碼進行求值
* @param redisName
* @param sha
* @param count
* @param params
* @return
*/
public static Object evalsha (String redisName, String sha, int count, String... params) {
return cache.getResource(redisName).evalsha(sha, count, params);
}
public static Object eval(String redisName, String script,int count, String[] value)
{
return cache.getResource(redisName).eval(script, count, value);
}
/**
* 在redis中累加,每次加1
* @param redisName 快取名稱
* @param key 需要累加的KEY
* @return
*/
public static Object incr(String redisName, String key)
{
return cache.getResource(redisName).incr(key);
}
/**
* 在redis中累加,每次加1
* @param redisName 快取名稱
* @param key 需要累加的KEY
* @param value 需要累加的value
* @return
*/
public static Object incr(String redisName, String key, Double value)
{
return cache.getResource(redisName).incrByFloat(key, value);
}
/**
* 將訊息傳送到指定的頻道上
* @param redisName 快取名稱
* @param channel 頻道名稱
* @param value 訊息
* @return
*/
public static Object publish(String redisName, String channel, String value)
{
return cache.getResource(redisName).publish(channel, value);
}
/**
* 如果key存在,設定失敗,返回0;如果不存在,則設定成功,返回1
* @param redisName 快取名稱
* @param key 快取key
* @param value 快取值
* @return
*/
public static Long setnx(String redisName, String key, String value)
{
return cache.getResource(redisName).setnx(key, value);
}
}