| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622 |
- package com.ydtech.utils;
- import com.alibaba.fastjson.JSON;
- import com.ydtech.components.MyJedisPoolConfig;
- import redis.clients.jedis.*;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- /**
- * 基于jedis的redis操作工具类
- *
- * @author: lig
- * @date: 2023年03月13日 0013
- */
- public class RedisUtils {
- /*
- 除了该工具类提供的方法外,还可以在外面调用getJedis()方法,获取到jedis实例后,调用它原生的api来操作
- */
- /**
- * 获取jedis对象,并选择redis库。jedis默认是0号库,可传入1-16之间的数选择库存放数据
- * 原则上使用一个redis库存放数据,通过特定的key的命令规则来区分不同的数据就行了。
- *
- * @param index redis库号。使用可变参数的目的就是该参数可传可不传。
- * @return 返回jedis对象
- */
- public static Jedis getJedis(int... index) {
- Jedis jedis = MyJedisPoolConfig.getJedisPool().getResource();
- if (index != null && index.length > 0) {
- if (index[0] > 0 && index[0] <= 16) {
- jedis.select(index[0]);
- }
- }
- return jedis;
- }
- /*######################## key的操作 ################################*/
- /**
- * 根据pattern返回当前库中的key
- *
- * @param pattern
- * @return
- */
- public static Set<String> keys(String pattern) {
- Jedis jedis = null;
- Set<String> keys = null;
- try {
- jedis = getJedis();
- keys = jedis.keys(pattern);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return keys;
- }
- /**
- * 删除一个或多个key
- *
- * @param key 一个或多个key
- */
- public static Long del(String... key) {
- Jedis jedis = null;
- Long delNum = 0L;
- try {
- jedis = getJedis();
- delNum = jedis.del(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return delNum;
- }
- /**
- * 批量删除
- *
- * @param keyList 要删除的key的集合
- */
- public static void mdel(List<String> keyList) {
- Jedis jedis = getJedis();
- //获取pipeline
- Pipeline pipeline = jedis.pipelined();
- for (String key : keyList) {
- pipeline.del(key);
- }
- //执行结果同步,这样才能保证结果的正确性。实际上不执行该方法也执行了上面的命令,但是结果确不一定完全正确。
- //注意
- pipeline.sync();
- //关闭连接
- jedis.close();
- }
- /**
- * 判断某个key是否还存在
- *
- * @param key key
- * @return
- */
- public static Boolean exists(String key) {
- Jedis jedis = null;
- Boolean flag = false;
- try {
- jedis = getJedis();
- flag = jedis.exists(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return flag;
- }
- /**
- * 设置某个key的过期时间,单位秒
- *
- * @param key key
- * @param seconds 过期时间秒
- */
- public static void expire(String key, int seconds) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.expire(key, seconds);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 查看某个key还有几秒过期,-1表示永不过期 ,-2表示已过期
- *
- * @param key key
- * @return
- */
- public static Long timeToLive(String key) {
- Jedis jedis = null;
- Long ttl;
- try {
- jedis = getJedis();
- ttl = jedis.ttl(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return ttl;
- }
- /**
- * 查看某个key对应的value的类型
- *
- * @param key
- * @return
- */
- public static String type(String key) {
- Jedis jedis = null;
- String type = null;
- try {
- jedis = getJedis();
- type = jedis.type(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return type;
- }
- /*######################## string(字符串)的操作 ####################*/
- /**
- * 获取某个key的value,类型要对,只能value是string的才能获取
- *
- * @param key
- * @return
- */
- public static String get(String key) {
- Jedis jedis = null;
- String value = null;
- try {
- jedis = getJedis();
- value = jedis.get(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return value;
- }
- /**
- * 获取某个key的value 对象
- *
- * @param key
- * @return
- */
- public static Object getObject(String key) {
- Jedis jedis = null;
- String value = null;
- try {
- jedis = getJedis();
- value = jedis.get(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- Object obj = JSON.parse(value);
- return obj;
- }
- /**
- * 设置某个key的value
- *
- * @param key
- * @param value
- */
- public static void set(String key, String value) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.set(key, value);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 设置某个key的value
- *
- * @param key
- * @param value
- */
- public static void setObject(String key, Object obj) {
- String value = JSON.toJSONString(obj);
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.set(key, value);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 设置某个key的value 包含过期时间
- *
- * @param key
- * @param value
- * @param seconds 过期时间
- */
- public static void set(String key, String value ,int seconds) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.set(key, value);
- jedis.expire(key, seconds);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 字符串后追加内容
- *
- * @param key key
- * @param appendContent 要追加的内容
- */
- public static void append(String key, String appendContent) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.append(key, appendContent);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 返回key的value的长度
- *
- * @param key
- * @return
- */
- public static Long strlen(String key) {
- Jedis jedis = null;
- Long strLen = 0L;
- try {
- jedis = getJedis();
- strLen = jedis.strlen(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return strLen;
- }
- /**
- * value 加1 必
- * 须是字符型数字
- *
- * @param key
- * @return 增加后的值
- */
- public static Long incr(String key) {
- Jedis jedis = null;
- Long incrResult = 0L;
- try {
- jedis = getJedis();
- incrResult = jedis.incr(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return incrResult;
- }
- /**
- * value 减1 必须是字符型数字
- *
- * @param key
- * @return
- */
- public static Long decr(String key) {
- Jedis jedis = null;
- Long decrResult = 0L;
- try {
- jedis = getJedis();
- decrResult = jedis.decr(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return decrResult;
- }
- /**
- * value 加increment
- *
- * @param key key
- * @param increment 加几
- * @return
- */
- public static Long incrby(String key, int increment) {
- Jedis jedis = null;
- Long incrByResult = 0L;
- try {
- jedis = getJedis();
- incrByResult = jedis.incrBy(key, increment);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return incrByResult;
- }
- /**
- * value 减increment
- *
- * @param key
- * @param increment
- * @return
- */
- public static Long decrby(String key, int increment) {
- Jedis jedis = null;
- Long decrByResult = 0L;
- try {
- jedis = getJedis();
- decrByResult = jedis.decrBy(key, increment);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return decrByResult;
- }
- /**
- * 给某个key设置过期时间和value,成功返回OK
- *
- * @param key key
- * @param seconds 过期时间秒
- * @param value 设置的值
- * @return
- */
- public static String setex(String key, int seconds, String value) {
- Jedis jedis = null;
- String result = null;
- try {
- jedis = getJedis();
- result = jedis.setex(key, seconds, value);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return result;
- }
- /*######################## list(列表)的操作 #######################*/
- //lpush rpush lpop rpop lrange lindex llen lset
- /**
- * 从左边向列表中添加值
- *
- * @param key key
- * @param str 要添加的值
- */
- public static void lpush(String key, String str) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.lpush(key, str);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 从右边向列表中添加值
- *
- * @param key key
- * @param str 要添加的值
- */
- public static void rpush(String key, String str) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.rpush(key, str);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 从左边取出一个列表中的值
- *
- * @param key
- * @return
- */
- public static String lpop(String key) {
- Jedis jedis = null;
- String lpop = null;
- try {
- jedis = getJedis();
- lpop = jedis.lpop(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return lpop;
- }
- /**
- * 从右边取出一个列表中的值
- *
- * @param key
- * @return
- */
- public static String rpop(String key) {
- Jedis jedis = null;
- String rpop = null;
- try {
- jedis = getJedis();
- rpop = jedis.rpop(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return rpop;
- }
- /**
- * 取出列表中指定范围内的值,0 到 -1 表示全部
- *
- * @param key
- * @param startIndex
- * @param endIndex
- * @return
- */
- public static List<String> lrange(String key, int startIndex, int endIndex) {
- Jedis jedis = null;
- List<String> result = null;
- try {
- jedis = getJedis();
- result = jedis.lrange(key, startIndex, endIndex);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return result;
- }
- /**
- * 返回某列表指定索引位置的值
- *
- * @param key 列表key
- * @param index 索引位置
- * @return
- */
- public static String lindex(String key, int index) {
- Jedis jedis = null;
- String lindex = null;
- try {
- jedis = getJedis();
- lindex = jedis.lindex(key, index);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return lindex;
- }
- /**
- * 返回某列表的长度
- *
- * @param key
- * @return
- */
- public static Long llen(String key) {
- Jedis jedis = null;
- Long llen = 0L;
- try {
- jedis = getJedis();
- llen = jedis.llen(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return llen;
- }
- /**
- * 给某列表指定位置设置为指定的值
- *
- * @param key
- * @param index
- * @param str
- */
- public static void lset(String key, Long index, String str) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.lset(key, index, str);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 对列表进行剪裁,保留指定闭区间的元素(索引位置也会重排)
- *
- * @param key 列表key
- * @param startIndex 开始索引位置
- * @param endIndex 结束索引位置
- */
- public static void ltrim(String key, Integer startIndex, Integer endIndex) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.ltrim(key, startIndex, endIndex);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 从列表的左边阻塞弹出一个元素
- *
- * @param key 列表的key
- * @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞
- * @return
- */
- public static List<String> blpop(String key, Integer timeout) {
- Jedis jedis = null;
- List<String> valueList = null;
- try {
- jedis = getJedis();
- valueList = jedis.blpop(timeout, key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return valueList;
- }
- /**
- * 从列表的右边阻塞弹出一个元素
- *
- * @param key 列表的key
- * @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞
- * @return
- */
- public static List<String> brpop(String key, Integer timeout) {
- Jedis jedis = null;
- List<String> valueList = null;
- try {
- jedis = getJedis();
- valueList = jedis.brpop(timeout, key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return valueList;
- }
- /*######################## hash(哈希表)的操作 #######################*/
- //hset hget hmset hmget hgetall hdel hkeys hvals hexists hincrby
- /**
- * 给某个hash表设置一个键值对
- *
- * @param key
- * @param field
- * @param value
- */
- public static void hset(String key, String field, String value) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.hset(key, field, value);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 取出某个hash表中某个field对应的value
- *
- * @param key key
- * @param field field
- * @return
- */
- public static String hget(String key, String field) {
- Jedis jedis = null;
- String hget = null;
- try {
- jedis = getJedis();
- hget = jedis.hget(key, field);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return hget;
- }
- /**
- * 某个hash表设置一个或多个键值对
- *
- * @param key
- * @param kvMap
- */
- public static void hmset(String key, Map<String, String> kvMap) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.hmset(key, kvMap);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 取出某个hash表中任意多个key对应的value的集合
- *
- * @param key
- * @param fields
- * @return
- */
- public static List<String> hmget(String key, String... fields) {
- Jedis jedis = null;
- List<String> hmget = null;
- try {
- jedis = getJedis();
- hmget = jedis.hmget(key, fields);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return hmget;
- }
- /**
- * 取出某个hash表中所有的键值对
- *
- * @param key
- * @return
- */
- public static Map<String, String> hgetall(String key) {
- Jedis jedis = null;
- Map<String, String> kvMap = null;
- try {
- jedis = getJedis();
- kvMap = jedis.hgetAll(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return kvMap;
- }
- /**
- * 判断某个hash表中的某个key是否存在
- *
- * @param key
- * @param field
- * @return
- */
- public static Boolean hexists(String key, String field) {
- Jedis jedis = null;
- Boolean exists = null;
- try {
- jedis = getJedis();
- exists = jedis.hexists(key, field);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return exists;
- }
- /**
- * 返回某个hash表中所有的key
- *
- * @param key
- * @return
- */
- public static Set<String> hkeys(String key) {
- Jedis jedis = null;
- Set<String> keys = null;
- try {
- jedis = getJedis();
- keys = jedis.hkeys(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return keys;
- }
- /**
- * 返回某个hash表中所有的value
- *
- * @param key
- * @return
- */
- public static List<String> hvals(String key) {
- Jedis jedis = null;
- List<String> hvals = null;
- try {
- jedis = getJedis();
- hvals = jedis.hvals(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return hvals;
- }
- /**
- * 删除某个hash表中的一个或多个键值对
- *
- * @param key
- * @param fields
- */
- public static void hdel(String key, String... fields) {
- Jedis jedis = null;
- try {
- jedis = getJedis();
- jedis.hdel(key, fields);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- /**
- * 给某个hash表中的某个field的value增加多少
- *
- * @param key hash表的key
- * @param field 表中的某个field
- * @param increment 增加多少
- * @return
- */
- public static Long hincrby(String key, String field, Long increment) {
- Jedis jedis = null;
- Long result = null;
- try {
- jedis = getJedis();
- result = jedis.hincrBy(key, field, increment);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return result;
- }
- /*######################## set(集合)的操作 ###########################*/
- /**
- * 往set集合中添加一个或多个元素
- *
- * @param key key
- * @param members 要添加的元素
- * @return 添加成功的元素个数
- */
- public static Long sadd(String key, String... members) {
- Jedis jedis = null;
- Long num = 0L;
- try {
- jedis = getJedis();
- num = jedis.sadd(key, members);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 返回set集合中的所有元素,顺序与加入时的顺序一致
- *
- * @param key key
- * @return
- */
- public static Set<String> smembers(String key) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.smembers(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 判断集合中是否存在某个元素
- *
- * @param key key
- * @param member 某个元素
- * @return true存在,false不存在
- */
- public static Boolean sismember(String key, String member) {
- Jedis jedis = null;
- Boolean isMember = false;
- try {
- jedis = getJedis();
- isMember = jedis.sismember(key, member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return isMember;
- }
- /**
- * 返回set集合的长度
- *
- * @param key key
- * @return
- */
- public static Long scard(String key) {
- Jedis jedis = null;
- Long len = 0L;
- try {
- jedis = getJedis();
- len = jedis.scard(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return len;
- }
- /**
- * 删除set集合中指定的一个或多个元素
- *
- * @param key
- * @param members 要删除的元素
- * @return 删除成功的元素个数
- */
- public static Long srem(String key, String... members) {
- Jedis jedis = null;
- Long num = 0L;
- try {
- jedis = getJedis();
- num = jedis.srem(key, members);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 将key1中的元素key1Member移动到key2中
- *
- * @param key1 来源集合key
- * @param key2 目的地集合key
- * @param key1Member key1中的元素
- * @return 1成功,0失败
- */
- public static Long smove(String key1, String key2, String key1Member) {
- Jedis jedis = null;
- Long num = 0L;
- try {
- jedis = getJedis();
- num = jedis.smove(key1, key2, key1Member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 随机查询返回集合中的指定个数的元素(若count为负数,返回的元素可能会重复)
- *
- * @param key key
- * @param count 要查询返回的元素个数
- * @return 元素list集合
- */
- public static List<String> srandmember(String key, int count) {
- Jedis jedis = null;
- List<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.srandmember(key, count);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 从set集合中随机弹出指定个数个元素
- *
- * @param key key
- * @param count 要弹出的个数
- * @return 随机弹出的元素
- */
- public static Set<String> spop(String key, int count) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.spop(key, count);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 求交集,返回多个set集合相交的部分
- *
- * @param setKeys 多个set集合的key
- * @return 相交的元素集合
- */
- public static Set<String> sinter(String... setKeys) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.sinter(setKeys);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 求并集,求几个set集合的并集(因为set中不会有重复的元素,合并后的集合也不会有重复的元素)
- *
- * @param setKeys 多个set的key
- * @return 合并后的集合
- */
- public static Set<String> sunion(String... setKeys) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.sunion(setKeys);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 求差集,求几个集合之间的差集
- *
- * @param setKeys 多个set的key
- * @return 差集
- */
- public static Set<String> sdiff(String... setKeys) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.sdiff(setKeys);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /*######################## zset(有序集合)的操作 #######################*/
- /**
- * 添加一个元素到zset
- *
- * @param key key
- * @param score 元素的分数
- * @param member 元素
- * @return 成功添加的元素个数
- */
- public static Long zadd(String key, double score, String member) {
- Jedis jedis = null;
- Long num = null;
- try {
- jedis = getJedis();
- num = jedis.zadd(key, score, member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 添加一个或多个元素到zset
- *
- * @param key key
- * @param memberScores 元素和分数的map集合,map的k是元素,v是分数
- * @return 成功添加的元素个数
- */
- public static Long zadd(String key, Map<String, Double> memberScores) {
- Jedis jedis = null;
- Long num = null;
- try {
- jedis = getJedis();
- num = jedis.zadd(key, memberScores);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 查询指定闭区间的元素(根据分数升序)
- * (0,-1表示全部)
- *
- * @param key zset的key
- * @param start 开始区间
- * @param end 结束区间
- * @return 元素集合
- */
- public static Set<String> zrange(String key, long start, long end) {
- Jedis jedis = null;
- Set<String> ascResult = null;
- try {
- jedis = getJedis();
- ascResult = jedis.zrange(key, start, end);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return ascResult;
- }
- /**
- * 查询指定闭区间的元素,带着分数
- * (0,-1表示全部)
- *
- * @param key zset的key
- * @param start 开始区间
- * @param end 结束区间
- * @return 元素集合 格式为:
- * [{"element":"a","score":10.0,"binaryElement":"YQ=="},{"element":"b","score":20.0,"binaryElement":"Yg=="}]
- */
- public static Set<Tuple> zrangeWithScores(String key, long start, long end) {
- Jedis jedis = null;
- Set<Tuple> ascResult = null;
- try {
- jedis = getJedis();
- ascResult = jedis.zrangeWithScores(key, start, end);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return ascResult;
- }
- /**
- * 查询指定索引闭区间的元素(根据分数降序)
- * (0,-1表示全部)
- *
- * @param key zset的key
- * @param start 开始区间
- * @param end 结束区间
- * @return 元素集合
- */
- public static Set<String> zrevrange(String key, long start, long end) {
- Jedis jedis = null;
- Set<String> descResult = null;
- try {
- jedis = getJedis();
- descResult = jedis.zrevrange(key, start, end);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return descResult;
- }
- /**
- * 查询指定索引闭区间的元素,带着分数(根据分数降序)
- * (0,-1表示全部)
- *
- * @param key zset的key
- * @param start 开始区间
- * @param end 结束区间
- * @return 元素集合 格式为:
- * [{"element":"b","score":20.0,"binaryElement":"Yg=="},{"element":"a","score":10.0,"binaryElement":"YQ=="}]
- */
- public static Set<Tuple> zrevrangeWithScores(String key, long start, long end) {
- Jedis jedis = null;
- Set<Tuple> descResult = null;
- try {
- jedis = getJedis();
- descResult = jedis.zrevrangeWithScores(key, start, end);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return descResult;
- }
- /**
- * 返回有序集合(zset)中的元素个数
- *
- * @param key key
- * @return 个数
- */
- public static Long zcard(String key) {
- Jedis jedis = null;
- Long count = null;
- try {
- jedis = getJedis();
- count = jedis.zcard(key);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return count;
- }
- /**
- * 返回指定分数区间的元素个数(闭区间)
- *
- * @param key key
- * @param startScore 开始分数
- * @param endScore 结束分数
- * @return 个数
- */
- public static Long zcount(String key, Long startScore, Long endScore) {
- Jedis jedis = null;
- Long count = null;
- try {
- jedis = getJedis();
- count = jedis.zcount(key, startScore, endScore);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return count;
- }
- /**
- * 返回某元素在集合中的排名(根据分数降序排列时)
- *
- * @param key key
- * @param member 集合中的元素
- * @return 排名,从0开始
- */
- public static Long zrevrank(String key, String member) {
- Jedis jedis = null;
- Long count = null;
- try {
- jedis = getJedis();
- count = jedis.zrevrank(key, member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return count;
- }
- /**
- * 返回某元素在集合中的排名(根据分数升序排列时)
- *
- * @param key key
- * @param member 集合中的元素
- * @return 排名,从0开始
- */
- public static Long zrank(String key, String member) {
- Jedis jedis = null;
- Long count = null;
- try {
- jedis = getJedis();
- count = jedis.zrank(key, member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return count;
- }
- /**
- * 升序查询指定分数闭区间的元素
- *
- * @param key key
- * @param min 最小分数
- * @param max 最大分数
- * @return 元素集合
- */
- public static Set<String> zrangeByScore(String key, double min, double max) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrangeByScore(key, min, max);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 升序查询指定分数闭区间的元素,并指定偏移量
- *
- * @param key key
- * @param min 最小分数
- * @param max 最大分数
- * @param offset 偏移量
- * @param size 数量
- * @return 元素集合
- */
- public static Set<String> zrangeByScore(String key, double min, double max, int offset, int size) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrangeByScore(key, min, max, offset, size);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 升序查询指定分数闭区间的元素,带着分数
- *
- * @param key key
- * @param min 最小分数
- * @param max 最大分数
- * @return 元素集合
- */
- public static Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
- Jedis jedis = null;
- Set<Tuple> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrangeByScoreWithScores(key, min, max);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 升序查询指定分数闭区间的元素,并指定偏移量,带着分数
- *
- * @param key key
- * @param min 最小分数
- * @param max 最大分数
- * @param offset 偏移量
- * @param size 数量
- * @return 元素集合
- */
- public static Set<Tuple> zrangeByScoreWithScores(String key, double min, double max, int offset, int size) {
- Jedis jedis = null;
- Set<Tuple> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrangeByScoreWithScores(key, min, max, offset, size);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 降序查询指定分数闭区间的元素
- *
- * @param key key
- * @param max 最大分数
- * @param min 最小分数
- * @return 元素集合
- */
- public static Set<String> zrevrangebyscore(String key, double max, double min) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrevrangeByScore(key, max, min);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 降序查询指定分数闭区间的元素,并指定偏移量
- *
- * @param key key
- * @param max 最大分数
- * @param min 最小分数
- * @return 元素集合
- */
- public static Set<String> zrevrangebyscore(String key, double max, double min, int offset, int size) {
- Jedis jedis = null;
- Set<String> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrevrangeByScore(key, max, min, offset, size);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 降序查询指定分数闭区间的元素,并带着分数
- *
- * @param key key
- * @param min 最小分数
- * @param max 最大分数
- * @return 元素集合
- */
- public static Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min) {
- Jedis jedis = null;
- Set<Tuple> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrevrangeByScoreWithScores(key, max, min);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 降序查询指定分数闭区间的元素,并指定偏移量,带着分数
- *
- * @param key key
- * @param min 最小分数
- * @param max 最大分数
- * @param offset 偏移量
- * @param size 数量
- * @return 元素集合
- */
- public static Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min, int offset, int size) {
- Jedis jedis = null;
- Set<Tuple> members = null;
- try {
- jedis = getJedis();
- members = jedis.zrevrangeByScoreWithScores(key, min, max, offset, size);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return members;
- }
- /**
- * 有序集合中指定删除一或多个元素
- *
- * @param key
- * @param member 元素
- * @return 删除成功的个数
- */
- public static Long zrem(String key, String... member) {
- Jedis jedis = null;
- Long num = null;
- try {
- jedis = getJedis();
- num = jedis.zrem(key, member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 分数升序排名时,删除指定索引区间的元素
- *
- * @param key key
- * @param start 开始索引
- * @param end 结束索引
- * @return 删除成功的个数
- */
- public static Long zremrangebyrank(String key, long start, long end) {
- Jedis jedis = null;
- Long num = null;
- try {
- jedis = getJedis();
- num = jedis.zremrangeByRank(key, start, end);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 分数升序排名时,删除指定分数区间的元素
- *
- * @param key key
- * @param min 最小分数
- * @param max 最大分数
- * @return 删除成功的个数
- */
- public static Long zremrangeByScore(String key, long min, long max) {
- Jedis jedis = null;
- Long num = null;
- try {
- jedis = getJedis();
- num = jedis.zremrangeByScore(key, min, max);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return num;
- }
- /**
- * 查询有序集合中某元素的分数
- *
- * @param key key
- * @param member 元素
- * @return 删除成功的个数
- */
- public static Double zscore(String key, String member) {
- Jedis jedis = null;
- Double score = null;
- try {
- jedis = getJedis();
- score = jedis.zscore(key, member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return score;
- }
- /**
- * 给有序集合中某元素的分数增加(正数)或减少(负数)
- *
- * @param key key
- * @param score 分数
- * @param member 元素
- * @return 新的分数
- */
- public static Double zincrby(String key, double score, String member) {
- Jedis jedis = null;
- Double newScore = null;
- try {
- jedis = getJedis();
- newScore = jedis.zincrby(key, score, member);
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- return newScore;
- }
- /**
- * 私有化构造器,不让实例化对象
- */
- private RedisUtils() {
- }
- public static void main(String[] args) {
- // JedisPool jedisPool = MyJedisPoolConfig.getJedisPool();
- //// JedisPool jedisPool1 = MyJedisPoolConfig.getJedisPool();
- //// System.out.println(jedisPool == jedisPool1);//true
- //
- // Jedis j = new Jedis("127.0.0.1",6379);
- //
- // System.out.println(j.ping());
- JedisPoolConfig poolConfig = new JedisPoolConfig();
- //========= jedisPool的一些配置=============================
- poolConfig.setMaxTotal(10000);//最大连接数
- poolConfig.setMaxIdle(50);//最多空闲数
- poolConfig.setMaxWaitMillis(5 * 1000);//当池中没有连接时,最多等待5秒
- JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379, 3000, null);
- Jedis j = jedisPool.getResource();
- System.out.println(j.ping());
- }
- }
|