package com.qiwu.inference.util; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import java.util.Collection; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; /** * 基于Redisson实现Redis数据的操作 */ public class RedissonUtil { private static RedissonClient client; static { Config config = new Config(); config.setThreads(1000); config.useSingleServer().setAddress("redis://127.0.0.1:6379"); // 单机 client = Redisson.create(config); } // 新增 public static void setStr(String key,String val){ client.getBucket(key).set(val); } public static void setStr(String key,String val,long seconds){ client.getBucket(key).set(val,seconds, TimeUnit.SECONDS); } public static void setSet(String key,String val){ client.getSet(key).add(val); } public static void setSet(String key, Set vals){ client.getSet(key).addAll(vals); } public static void setSet(String key,double score,String val){ client.getScoredSortedSet(key).add(score,val); } public static void setSet(String key, Map vals){ client.getScoredSortedSet(key).addAll(vals); } public static void setHash(String key,String val,Object file){ client.getMap(key).put(val,file); } public static void serHashAll(String key,Map map){ client.getMap(key).putAll(map); } // 查询 public static String getStr(String key){ return client.getBucket(key).get().toString(); } public static Set getSet(String key){ return client.getSet(key).readAll(); } public static Collection getzSet(String key){ return client.getScoredSortedSet(key).readAll(); } public static boolean checkSet(String key,Object val){ return client.getSet(key).contains(val); } public static boolean checkzSet(String key,Object val){ return client.getScoredSortedSet(key).contains(val); } public static boolean checkKey(String key){ return client.getKeys().countExists(key) > 0; } public static boolean checkField(String key,String file){ return client.getMap(key).containsKey(file); } public static Object getHash(String key,String file){ return client.getMap(key).get(file); } // 设置有效期 public static void setTime(String key,long time,TimeUnit timeUnit){ client.getKeys().expire(key, time, timeUnit); } // 移除元素 public static boolean delZet(String key,Object val){ return client.getScoredSortedSet(key).remove(val); } // 删除key public static void delKey(String... key){ client.getKeys().delete(key); } public static void delHash(String key,Object val){ client.getMap(key).remove(val); } // 分布式锁 Redis = (Setnx + Java中的LOCK) // 加锁 public static void lock(String key){ client.getLock(key).lock(); } // 加锁设置有效期 超出有效期自动释放 public static void lock(String key,long seconds){ client.getLock(key).lock(seconds,TimeUnit.SECONDS); } // 释放锁 public static void unlock(String key){ client.getLock(key).unlock(); } public static Iterable getKeys(String key){ return client.getKeys().getKeysByPattern(key); } public static void main(String[] args) { System.out.println(RedissonUtil.getSet("2ba767cf-62e8-41a2-9f9f-d0581a0c9fa5")); } }