1 Star 0 Fork 0

cps007/spring-boot-model

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
RedisUtils.java 45.66 KB
一键复制 编辑 原始数据 按行查看 历史
jf 提交于 2022-09-22 15:40 . 更新springboot版本
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
package cn.springboot.model.service.utils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Redis工具类:<br/>
* 常用类型:Value,Hash,List,Set,ZSet. <br/>
* 其他类型:Cluster,Geo,HyperLogLog.<br/>
* StringRedisTemplate:可操作 Value,Hash.<br/>
* RedisTemplate:可操作 Value,Hash,List,Set,ZSet.<br/>
* 示例:<br/><code>@Autowired<br/>private RedisUtils redisUtils;<br/><br/>
* public void setData(){<br/>
* redisUtils.value.set(k, v);<br/>
* }<br/>
* <code/>
*
* @author jf
* @version 1.0
*/
@Component("redisUtils")
@SuppressWarnings({"unchecked", "all"})
public class RedisUtils {
public final Common common = new Common();
public final ValuePlus value = new ValuePlus();
public final HashPlus hash = new HashPlus();
public final ListPlus list = new ListPlus();
public final SetPlus set = new SetPlus();
public final ZSetPlus zSet = new ZSetPlus();
protected final long NOT_EXPIRE = -1;
@Resource
@Qualifier("redisTemplate")
protected RedisTemplate redisTemplate;
public RedisUtils() {
}
public <T> ValueOperations<String, T> getValueOperations() {
ValueOperations<String, T> valueOperations = redisTemplate.opsForValue();
return valueOperations;
}
public <T> HashOperations<String, String, T> getHashOperations() {
HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
return hashOperations;
}
public <T> ListOperations<String, T> getListOperations() {
ListOperations<String, T> listOperations = redisTemplate.opsForList();
return listOperations;
}
public <T> SetOperations<String, T> getSetOperations() {
SetOperations<String, T> setOperations = redisTemplate.opsForSet();
return setOperations;
}
public <T> ZSetOperations<String, T> getZSetOperations() {
ZSetOperations<String, T> zSetOperations = redisTemplate.opsForZSet();
return zSetOperations;
}
/**
* 随机数 50-70 秒
* <p>
* autoSeconds()*50~=50分钟,*60~=1小时,*60*2~=2小时,*60*24~=1天,*60*24*5~=5天
*
* @return long
*/
public long autoSeconds() {
return ((int) (Math.random() * 20) + 50);
}
/**
* key相关操作 检测、删除、更新
* delete, dump, hasKey, expire, type, rename
*
* @author jf
* @version 1.0
*/
public class Common {
/**
* 删除
*
* @param key
*/
public void delete(final String key) {
redisTemplate.delete(key);
}
/**
* 批量删除key
*
* @param keys
*/
public void delete(final Collection<String> keys) {
redisTemplate.delete(keys);
}
/**
* 删除集合对象
*
* @param collection 多个对象
* @return
*/
public long deleteObject(final Collection collection) {
return redisTemplate.delete(collection);
}
/**
* 序列化key
*
* @param key
* @return byte[]
*/
public byte[] dump(final String key) {
return redisTemplate.dump(key);
}
/**
* 是否存在key
*
* @param key
* @return
*/
public Boolean hasKey(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 设置给定key生存时间..
*
* @param key
* @param seconds 过期时间
* @return
*/
public Boolean expire(final String key, final long seconds) {
return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
}
/**
* 设置给定key生存时间..
*
* @param key
* @param timeout
* @param timeUnit TimeUnit.type [SECONDS,MINUTES,HOURS,DAYS]
* @return
*/
public Boolean expire(final String key, final long timeout, TimeUnit timeUnit) {
return redisTemplate.expire(key, timeout, timeUnit);
}
/**
* 将给定key的到期时间设置为日期时间戳。
*
* @param key
* @param date
* @return
*/
public Boolean expireAt(final String key, final Date date) {
return redisTemplate.expireAt(key, date);
}
/**
* 查找与给定pattern匹配的所有键。
*
* @param pattern 例如:sys_dict:*
* @return
*/
public Set<String> keys(final String pattern) {
return (Set<String>) redisTemplate.keys(pattern);
}
/**
* 将给定的key移动到带有index数据库。
*
* @param key
* @param dbIndex
* @return
*/
public Boolean move(final String key, final int dbIndex) {
return redisTemplate.move(key, dbIndex);
}
/**
* 从给定的key删除过期时间,key 将持久保持
*
* @param key
* @return
*/
public Boolean persist(final String key) {
return redisTemplate.persist(key);
}
/**
* 获取输入key生存时间并将其转换为给定的TimeUnit
*
* @param key 指定返回key
* @param timeUnit 时间返回类型
* @return
*/
public Long getExpire(final String key, TimeUnit timeUnit) {
return redisTemplate.getExpire(key, timeUnit);
}
/**
* 返回 key 的剩余的过期时间
*
* @param key
* @return
*/
public Long getExpire(final String key) {
return redisTemplate.getExpire(key);
}
/**
* 从当前数据库中随机返回一个 key
*
* @return
*/
public String randomKey() {
return (String) redisTemplate.randomKey();
}
/**
* 修改 key 的名称
*
* @param oldKey
* @param newKey
*/
public void rename(final String oldKey, final String newKey) {
redisTemplate.rename(oldKey, newKey);
}
/**
* 仅当 newkey 不存在时,将 oldKey 改名为 newkey
*
* @param oldKey
* @param newKey
* @return
*/
public Boolean renameIfAbsent(final String oldKey, final String newKey) {
return redisTemplate.renameIfAbsent(oldKey, newKey);
}
/**
* 返回 key 所储存的值的类型
*
* @param key
* @return
*/
public DataType type(final String key) {
return redisTemplate.type(key);
}
}
/**
* @author jf
* @version 1.0
*/
public class ValuePlus {
/**
* 设置数据, 默认过期时间2小时
*
* @param key 键
* @param value 值
*/
public <T> void set(final String key, final T value) {
set(key, value, (autoSeconds() * 60 * 2));
}
/**
* 设置数据 & 过期时间
*
* @param key 键
* @param value 值,自动JSON.toJSONString(value)
* @param timeout 过期时间:默认时间单位为秒
*/
public <T> void set(final String key, T value, long timeout) {
set(key, value, timeout, TimeUnit.SECONDS);
}
/**
* 设置数据 & 过期时间 & 指定时间单位
*
* @param key 键
* @param value 值,自动JSON.toJSONString(value)
* @param timeout 过期时间
* @param timeUnit 过期时间单位
*/
public <T> void set(final String key, T value, long timeout, TimeUnit timeUnit) {
getValueOperations().set(key, value);
if (timeout != NOT_EXPIRE) {
redisTemplate.expire(key, timeout, timeUnit);
}
}
/**
* 添加持久化数据
*
* @param key 键
* @param value 设置指定 key 的值,无过期时间
*/
public <T> void setNotExpire(final String key, final T value) {
set(key, value, NOT_EXPIRE);
}
/**
* 设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
* 设置存储在key中的 value 中offset处的位。
*
* @param key 键
* @param offset 位置
* @param value 值
* @return
*/
public Boolean setBit(final String key, final long offset, final boolean value) {
return getValueOperations().setBit(key, offset, value);
}
/**
* 如果key不存在,则设置key以保存字符串value 。
*
* @param key
* @param value
* @return
*/
public <T> Boolean setIfAbsent(final String key, final T value) {
return setIfAbsent(key, value, NOT_EXPIRE, null);
}
/**
* 如果key不存在,则设置key以保存字符串value和过期timeout 。
*
* @param key
* @param value
* @param seconds 秒
* @return
*/
public <T> Boolean setIfAbsent(final String key, final T value, final long seconds) {
return setIfAbsent(key, value, seconds, TimeUnit.SECONDS);
}
public <T> Boolean setIfAbsent(final String key, final T value, final long seconds, TimeUnit unit) {
if (seconds != NOT_EXPIRE && seconds > 0) {
return getValueOperations().setIfAbsent(key, value, seconds, unit);
}
return getValueOperations().setIfAbsent(key, value);
}
/**
* 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
*
* @param key
* @param value
* @param offset
*/
public <T> void setRange(final String key, final T value, final long offset) {
getValueOperations().set(key, value, offset);
}
/**
* 批量添加
*
* @param maps
*/
public void setMulti(final Map<String, String> maps) {
getValueOperations().multiSet(maps);
}
/**
* 仅当提供的key不存在时,才同时设置一个或多个 key-value
*
* @param maps
* @return
*/
public Boolean setMultiIfAbsent(final Map<String, String> maps) {
return getValueOperations().multiSetIfAbsent(maps);
}
/**
* 获取指定 key 的值
*
* @param key
* @return
*/
public <T> T get(final String key) {
return get(key, NOT_EXPIRE);
}
/**
* 获取数据 & 更新过期时间
*
* @param key
* @param expire
* @return
*/
public <T> T get(final String key, final long expire) {
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return (T) getValueOperations().get(key);
}
/**
* 获取对应class数据
*
* @param key
* @param clazz
* @param <T>
* @return
*/
public <T> T get(final String key, final Class<T> clazz) {
return get(key, clazz, NOT_EXPIRE);
}
/**
* 获取对应class数据 & 更新key的过期时间
*
* @param key
* @param clazz
* @param expire
* @param <T>
* @return
*/
public <T> T get(final String key, final Class<T> clazz, final long expire) {
String value = JSON.toJSONString(getValueOperations().get(key));
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : JSON.parseObject(value, new TypeReference<T>() {
});
}
/**
* 返回 key 中字符串值的子字符
*
* @param key
* @param start
* @param end
* @return
*/
public String getRange(final String key, final long start, final long end) {
return getValueOperations().get(key, start, end);
}
/**
* 设置键的值并返回其旧值
*
* @param key
* @param value
* @return
*/
public <T> T getAndSet(final String key, final T value) {
return (T) getValueOperations().getAndSet(key, value);
}
/**
* 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
*
* @param key
* @param offset
* @return
*/
public Boolean getBit(final String key, final long offset) {
return getValueOperations().getBit(key, offset);
}
/**
* 批量获取
*
* @param keys
* @return
*/
public List<String> multiGet(Collection<String> keys) {
return JSON.parseObject(JSON.toJSONString(getValueOperations().multiGet(keys)),
new TypeReference<List<String>>() {
});
}
/**
* 获取字符串的长度
*
* @param key
* @return
*/
public Long size(String key) {
return getValueOperations().size(key);
}
/**
* 增加(自增长), 负数则为自减
* 将 key 下存储为字符串值的整数值增加 increment。
*
* @param key
* @param increment
* @return
*/
public Long incrBy(String key, long increment) {
return getValueOperations().increment(key, increment);
}
/**
* 增加(自增长), 负数则为自减 浮点数
* 通过 {@code increment} 将存储为 {@code key} 下的字符串值的浮点数值递增。
*
* @param key
* @param increment
* @return
*/
public Double incrByFloat(String key, double increment) {
return getValueOperations().increment(key, increment);
}
/**
* 追加到末尾
*
* @param key
* @param value
* @return
*/
public <T> Integer append(String key, T value) {
return getValueOperations().append(key, JSON.toJSONString(value));
}
}
// *********************************************** hash相关操作
/**
* @author jf
* @version 1.0
*/
public class HashPlus {
/**
* 从 key 处的hash中获取给定 hashKey 的值。
*
* @param key
* @param hashKey
* @return
*/
public <T> T get(String key, String hashKey) {
return (T) getHashOperations().get(key, hashKey);
}
/**
* 获取存储在 key 的整个hash。
*
* @param key
* @return
*/
public <T> Map<String, T> getHAll(String key) {
return (Map<String, T>) getHashOperations().entries(key);
}
/**
* 从 key 处的 hash 中获取给定 HK 的值。
*
* @param key
* @param hashKeys
* @return
*/
public <T> List<T> getMulti(String key, Collection<String> hashKeys) {
return (List<T>) getHashOperations().multiGet(key, hashKeys);
}
/**
* 设置 hash 的 hashKey 的值<p>
* H key, HK hashKey, HV value
*
* @param key
* @param hashKey
* @param value
*/
public <T> void put(String key, String hashKey, T value) {
getHashOperations().put(key, hashKey, value);
}
/**
* 设置 hash 的一个或多个HK-HV值。
*
* @param key
* @param maps
*/
public <T> void putHAll(String key, Map<String, T> maps) {
getHashOperations().putAll(key, maps);
}
/**
* 仅当hashKey不存在时才设置
*
* @param key
* @param hashKey
* @param value
* @return
*/
public <T> Boolean putIfAbsent(String key, String hashKey, T value) {
return getHashOperations().putIfAbsent(key, hashKey, value);
}
/**
* 删除一个或多个哈希表字段
*
* @param key
* @param hashKeys
* @return
*/
public Long delete(String key, Object... hashKeys) {
return getHashOperations().delete(key, hashKeys);
}
/**
* 确定给定的 hash hashKey 是否存在
*
* @param key
* @param hashKey
* @return
*/
public Boolean exists(String key, Object hashKey) {
return getHashOperations().hasKey(key, hashKey);
}
/**
* 为哈希表 key 中的指定字段的整数值加上增量 increment<p>
* 通过给定的增量增加 hash hashKey 的值。
*
* @param key
* @param hashKey
* @param increment
* @return
*/
public Long increment(String key, String hashKey, long increment) {
return getHashOperations().increment(key, hashKey, increment);
}
/**
* 为哈希表 key 中的指定字段的整数值加上增量 increment<p>
* 通过给定的增量增加哈希 hashKey 的值。
*
* @param key
* @param hashKey
* @param delta
* @return
*/
public Double increment(String key, String hashKey, double delta) {
return getHashOperations().increment(key, hashKey, delta);
}
/**
* 在 key 处获取 hash 的键集(字段)。
*
* @param key
* @return
*/
public Set<String> keys(String key) {
return getHashOperations().keys(key);
}
/**
* 获取 key 的哈希大小。
*
* @param key
* @return
*/
public Long size(String key) {
return getHashOperations().size(key);
}
/**
* 获取哈希表中所有值<p>
* 获取hash key条目集
*
* @param key
* @return
*/
public List<Object> values(String key) {
return getHashOperations().values(key);
}
/**
* 迭代哈希表中的键值对<p>
* 使用 Cursor 迭代 key 处哈希中的条目。<p>
* 重要提示:完成后调用 Cursor.close() 以避免资源泄漏。<p>
*
* @param key
* @param options
* @return
* @throws IOException
*/
public Cursor<Map.Entry<String, Object>> scan(String key, ScanOptions options) throws IOException {
Cursor<Map.Entry<String, Object>> scan = getHashOperations().scan(key, options);
scan.close();
return scan;
}
}
/**
* @author jf
* @version 1.0
*/
public class ListPlus {
/**
* 通过索引获取列表中的元素
*
* @param key
* @param index 正数: 从0|头部位置检索,负数:从尾部位置检索,-index值越小【-1 -2 -3】,检索位置越考前<p>
* 【-index-size=index、【-3-4=1(index),-2-4=2(index)】<p>
* index 0 1 2 3<p>
* 添加 【000Vlues 001Vlues 002Vlues 003Vlues】<p>
* 检索+ 【000Vlues 001Vlues 002Vlues 003Vlues】<p>
* 检索- 【-4:000Vlues -1:003Vlues -2:002Vlues -3:001Vlues】<p>
* @return
*/
public String index(String key, long index) {
return JSON.toJSONString(getListOperations().index(key, index));
}
/**
* 获取列表指定范围内的元素
*
* @param key
* @param start 开始位置
* @param end 结束位置
* @return
*/
public <T> List<T> range(String key, long start, long end) {
return (List<T>) getListOperations().range(key, start, end);
}
/**
* 存储在list头部
*
* @param key
* @param value
* @return
*/
public <T> Long leftPush(String key, T value) {
return getListOperations().leftPush(key, value);
}
/**
* 存储在list头部
*
* @param key
* @param value
* @return
*/
public <T> Long leftPushAll(String key, T... value) {
return getListOperations().leftPushAll(key, value);
}
/**
* 存储在list头部
*
* @param key
* @param value
* @return
*/
public <T> Long leftPushAll(String key, Collection<T> value) {
return getListOperations().leftPushAll(key, value);
}
/**
* 当list存在的时候才加入
*
* @param key
* @param value
* @return
*/
public <T> Long leftPushIfPresent(String key, T value) {
return getListOperations().leftPushIfPresent(key, value);
}
/**
* 如果pivot存在,再pivot前面添加
*
* @param key
* @param pivot
* @param value
* @return
*/
public <T> Long leftPush(String key, Object pivot, T value) {
return getListOperations().leftPush(key, pivot, value);
}
/**
* 存储在list尾部
*
* @param key
* @param value
* @return
*/
public <T> Long rightPush(String key, T value) {
return rightPush(key, value, NOT_EXPIRE, TimeUnit.SECONDS);
}
/**
* 在pivot元素的右边添加值
*
* @param key
* @param pivot
* @param value
* @return
*/
public <T> Long rightPush(String key, Object pivot, T value) {
return getListOperations().rightPush(key, pivot, value);
}
/**
* 存储在list尾部+过期时间
*
* @param key
* @param value
* @param timeout 过期时间
* @param unit 时间单位
*/
public <T> Long rightPush(String key, T value, long timeout, TimeUnit unit) {
Long r = getListOperations().rightPush(key, value);
if (timeout > 0 && timeout != NOT_EXPIRE) {
redisTemplate.expire(key, timeout, unit);
}
return r;
}
/**
* 存储在list尾部
*
* @param key
* @param value
* @return
*/
public <T> Long rightPushAll(String key, T... value) {
return getListOperations().rightPushAll(key, value);
}
/**
* 存储在list尾部
*
* @param key
* @param value
* @return
*/
public <T> Long rightPushAll(String key, Collection<T> value) {
return getListOperations().rightPushAll(key, value);
}
/**
* 为已存在的列表添加值
*
* @param key
* @param value
* @return
*/
public <T> Long rightPushIfPresent(String key, T value) {
return getListOperations().rightPushIfPresent(key, value);
}
/**
* 通过索引设置列表元素的值
*
* @param key
* @param index
* @param value
*/
public <T> void set(String key, long index, T value) {
getListOperations().set(key, index, value);
}
/**
* 移出并获取列表的第一个元素
*
* @param key
* @return
*/
public String leftPop(String key) {
return JSON.toJSONString(getListOperations().leftPop(key));
}
/**
* 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
*
* @param key
* @param timeout
* @param timeUnit
* @return
*/
public String leftPop(String key, long timeout, TimeUnit timeUnit) {
return JSON.toJSONString(getListOperations().leftPop(key, timeout, timeUnit));
}
/**
* 移除并获取列表最后一个元素
*
* @param key
* @return
*/
public String rightPop(String key) {
return JSON.toJSONString(getListOperations().rightPop(key));
}
/**
* 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
*
* @param key
* @param timeout
* @param timeUnit
* @return
*/
public String rightPop(String key, long timeout, TimeUnit timeUnit) {
return JSON.toJSONString(getListOperations().rightPop(key, timeout, timeUnit));
}
/**
* 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
*
* @param sourceKey
* @param destinationKey
* @return
*/
public String rightPopAndLeftPush(String sourceKey, String destinationKey) {
return JSON.toJSONString(getListOperations().rightPopAndLeftPush(sourceKey, destinationKey));
}
/**
* 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
*
* @param sourceKey
* @param destinationKey
* @param timeout
* @param timeUnit
* @return
*/
public String rightPopAndLeftPush(String sourceKey, String destinationKey, long timeout, TimeUnit timeUnit) {
return JSON.toJSONString(getListOperations().rightPopAndLeftPush(sourceKey, destinationKey, timeout, timeUnit));
}
/**
* 删除集合中值等于value得元素
*
* @param key
* @param index
* @param value
* @return
*/
public <T> Long remove(String key, long index, T value) {
return getListOperations().remove(key, index, value);
}
/**
* 裁剪list
*
* @param key
* @param start
* @param end
*/
public void trim(String key, long start, long end) {
getListOperations().trim(key, start, end);
}
/**
* 获取列表长度
*
* @param key
* @return
*/
public Long size(String key) {
return getListOperations().size(key);
}
}
/**
* @author jf
* @version 1.0
*/
public class SetPlus {
/**
* set添加元素
*
* @param key
* @param values
* @return
*/
public <T> Long add(String key, T... values) {
return getSetOperations().add(key, values);
}
/**
* set移除元素
*
* @param key
* @param values
* @return
*/
public Long remove(String key, Object... values) {
return getSetOperations().remove(key, values);
}
/**
* 移除并返回集合的一个随机元素
*
* @param key
* @return
*/
public String pop(String key) {
return JSON.toJSONString(getSetOperations().pop(key));
}
/**
* 将元素value从一个集合移到另一个集合
*
* @param key
* @param value
* @param destKey
* @return
*/
public <T> Boolean move(String key, T value, String destKey) {
return getSetOperations().move(key, value, destKey);
}
/**
* 获取集合的大小
*
* @param key
* @return
*/
public Long size(String key) {
return getSetOperations().size(key);
}
/**
* 判断集合是否包含value
*
* @param key
* @param value
* @return
*/
public <T> Boolean isMember(String key, T value) {
return getSetOperations().isMember(key, value);
}
/**
* 获取两个集合的交集
*
* @param key
* @param otherKey
* @return
*/
public Set<String> intersect(String key, String otherKey) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().intersect(key, otherKey)), new TypeReference<Set<String>>() {
});
}
/**
* 获取key集合与多个集合的交集
*
* @param key
* @param otherKeys
* @return
*/
public Set<String> intersect(String key, Collection<String> otherKeys) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().intersect(key, otherKeys)), new TypeReference<Set<String>>() {
});
}
/**
* key集合与otherKey集合的交集存储到destKey集合中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long intersectAndStore(String key, String otherKey, String destKey) {
return getSetOperations().intersectAndStore(key, otherKey, destKey);
}
/**
* key集合与多个集合的交集存储到destKey集合中
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey) {
return getSetOperations().intersectAndStore(key, otherKeys, destKey);
}
/**
* 获取两个集合的并集
*
* @param key
* @param otherKeys
* @return
*/
public Set<String> union(String key, String otherKeys) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().union(key, otherKeys)), new TypeReference<Set<String>>() {
});
}
/**
* 获取key集合与多个集合的并集
*
* @param key
* @param otherKeys
* @return
*/
public Set<String> union(String key, Collection<String> otherKeys) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().union(key, otherKeys)), new TypeReference<Set<String>>() {
});
}
/**
* key集合与otherKey集合的并集存储到destKey中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long unionAndStore(String key, String otherKey, String destKey) {
return getSetOperations().unionAndStore(key, otherKey, destKey);
}
/**
* key集合与多个集合的并集存储到destKey中
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long unionAndStore(String key, Collection<String> otherKeys, String destKey) {
return getSetOperations().unionAndStore(key, otherKeys, destKey);
}
/**
* 获取两个集合的差集
*
* @param key
* @param otherKey
* @return
*/
public Set<String> difference(String key, String otherKey) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().difference(key, otherKey)), new TypeReference<Set<String>>() {
});
}
/**
* 获取key集合与多个集合的差集
*
* @param key
* @param otherKeys
* @return
*/
public Set<String> difference(String key, Collection<String> otherKeys) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().difference(key, otherKeys)), new TypeReference<Set<String>>() {
});
}
/**
* key集合与otherKey集合的差集存储到destKey中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long differenceAndStore(String key, String otherKey, String destKey) {
return getSetOperations().differenceAndStore(key, otherKey, destKey);
}
/**
* key集合与多个集合的差集存储到destKey中
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long differenceAndStore(String key, Collection<String> otherKeys, String destKey) {
return getSetOperations().differenceAndStore(key, otherKeys, destKey);
}
/**
* 获取集合所有元素
*
* @param key
* @return
*/
public Set<String> members(String key) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().members(key)), new TypeReference<Set<String>>() {
});
}
/**
* 随机获取集合中的一个元素
*
* @param key
* @return
*/
public String randomMember(String key) {
return JSON.toJSONString(getSetOperations().randomMember(key));
}
/**
* 随机获取集合中count个元素
*
* @param key
* @param count
* @return
*/
public List<String> randomMembers(String key, long count) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().randomMembers(key, count)), new TypeReference<List<String>>() {
});
}
/**
* 随机获取集合中count个元素并且去除重复的
*
* @param key
* @param count
* @return
*/
public Set<String> distinctRandomMembers(String key, long count) {
return JSON.parseObject(JSON.toJSONString(getSetOperations().distinctRandomMembers(key, count)), new TypeReference<Set<String>>() {
});
}
/**
* 使用迭代器获取元素
*
* @param key
* @param options
* @return
* @throws IOException
*/
public Cursor<String> scan(String key, ScanOptions options) throws IOException {
Cursor<Object> scan = getSetOperations().scan(key, options);
scan.close();
return JSON.parseObject(JSON.toJSONString(scan), new TypeReference<Cursor<String>>() {
});
}
}
/**
* @author jf
* @version 1.0
*/
public class ZSetPlus {
/**
* 添加元素,有序集合是按照元素的score值由小到大排列
*
* @param key
* @param value
* @param score
* @return
*/
public <T> Boolean add(String key, T value, double score) {
return getZSetOperations().add(key, value, score);
}
/**
* 添加元素,zSet按score由小到大排列
*
* @param key
* @param values
* @return
*/
public Long add(String key, Set<ZSetOperations.TypedTuple<Object>> values) {
return getZSetOperations().add(key, values);
}
/**
* 移除
*
* @param key
* @param values
* @return
*/
public Long remove(String key, Object... values) {
return getZSetOperations().remove(key, values);
}
/**
* 增加元素的score值,并返回增加后的值
*
* @param key
* @param value
* @param delta
* @return
*/
public <T> Double incrementScore(String key, T value, double delta) {
return getZSetOperations().incrementScore(key, value, delta);
}
/**
* 返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
*
* @param key
* @param value
* @return
*/
public <T> Long rank(String key, T value) {
return getZSetOperations().rank(key, value);
}
/**
* 返回元素在集合的排名,按元素的score值由大到小排列
*
* @param key
* @param value
* @return
*/
public <T> Long reverseRank(String key, T value) {
return getZSetOperations().reverseRank(key, value);
}
/**
* 获取集合的元素, 从小到大排序
*
* @param key
* @param start
* @param end
* @return
*/
public Set<Object> range(String key, long start, long end) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().range(key, start, end)), new TypeReference<Set<String>>() {
});
}
/**
* 获取集合元素, 并且把score值也获取
*
* @param key
* @param start
* @param end
* @return
*/
public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, long start, long end) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().rangeWithScores(key, start, end)), new TypeReference<Set<ZSetOperations.TypedTuple<String>>>() {
});
}
/**
* 根据Score值查询集合元素
*
* @param key
* @param min
* @param max
* @return
*/
public Set<String> rangeByScore(String key, double min, double max) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().rangeByScore(key, min, max)), new TypeReference<Set<String>>() {
});
}
/**
* 根据Score值查询集合元素, 从小到大排序
*
* @param key
* @param min
* @param max
* @return
*/
public Set<ZSetOperations.TypedTuple<String>> rangeByScoreWithScores(String key, double min, double max) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().rangeByScoreWithScores(key, min, max)), new TypeReference<Set<ZSetOperations.TypedTuple<String>>>() {
});
}
/**
* 根据score查询元素,s开始e结束位置
*
* @param key
* @param min
* @param max
* @param start
* @param end
* @return
*/
public Set<ZSetOperations.TypedTuple<String>> rangeByScoreWithScores(String key, double min, double max, long start, long end) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().rangeByScoreWithScores(key, min, max, start, end)), new TypeReference<Set<ZSetOperations.TypedTuple<String>>>() {
});
}
/**
* 获取集合的元素, 从大到小排序
*
* @param key
* @param start
* @param end
* @return
*/
public Set<String> reverseRange(String key, long start, long end) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().reverseRange(key, start, end)), new TypeReference<Set<String>>() {
});
}
/**
* 获取集合的元素, 从大到小排序, 并返回score值
*
* @param key
* @param start
* @param end
* @return
*/
Set<ZSetOperations.TypedTuple<String>> reverseRangeWithScores(String key, long start, long end) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().reverseRangeWithScores(key, start, end)), new TypeReference<Set<ZSetOperations.TypedTuple<String>>>() {
});
}
/**
* 根据Score值查询集合元素, 从大到小排序
*
* @param key
* @param min
* @param max
* @return
*/
public Set<String> reverseRangeByScore(String key, double min, double max) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().reverseRangeByScore(key, min, max)), new TypeReference<Set<String>>() {
});
}
/**
* 根据Score值查询集合元素, 从大到小排序
*
* @param key
* @param min
* @param max
* @return
*/
public Set<ZSetOperations.TypedTuple<String>> reverseRangeByScoreWithScores(String key, double min, double max) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().reverseRangeByScoreWithScores(key, min, max)), new TypeReference<Set<ZSetOperations.TypedTuple<String>>>() {
});
}
/**
* 根据score查询,大到小,s开始e结束
*
* @param key
* @param min
* @param max
* @param start
* @param end
* @return
*/
public Set<String> reverseRangeByScore(String key, double min, double max, long start, long end) {
return JSON.parseObject(JSON.toJSONString(getZSetOperations().reverseRangeByScore(key, min, max, start, end)), new TypeReference<Set<String>>() {
});
}
/**
* 根据score值获取集合元素数量
*
* @param key
* @param min
* @param max
* @return
*/
public Long count(String key, double min, double max) {
return getZSetOperations().count(key, min, max);
}
/**
* 返回使用给定key存储的有序集合的元素数。
*
* @param key
* @return
*/
public Long size(String key) {
return getZSetOperations().size(key);
}
/**
* 使用key获取排序集的大小。
*
* @param key
* @return
*/
public Long zCard(String key) {
return getZSetOperations().zCard(key);
}
/**
* 获取集合中value元素的score值
*
* @param key
* @param value
* @return
*/
public <T> Double score(String key, T value) {
return getZSetOperations().score(key, value);
}
/**
* 移除指定索引位置的成员
*
* @param key
* @param start
* @param end
* @return
*/
public Long removeRange(String key, long start, long end) {
return getZSetOperations().removeRange(key, start, end);
}
/**
* 根据指定的score值的范围来移除成员
*
* @param key
* @param min
* @param max
* @return
*/
public Long removeRangeByScore(String key, double min, double max) {
return getZSetOperations().removeRangeByScore(key, min, max);
}
/**
* 获取key和otherKey的并集并存储在destKey中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long unionAndStore(String key, String otherKey, String destKey) {
return getZSetOperations().unionAndStore(key, otherKey, destKey);
}
/**
* 获取key和多个集合并集并存在dKey中
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long unionAndStore(String key, Collection<String> otherKeys, String destKey) {
return getZSetOperations().unionAndStore(key, otherKeys, destKey);
}
/**
* 交集
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long intersectAndStore(String key, String otherKey, String destKey) {
return getZSetOperations().intersectAndStore(key, otherKey, destKey);
}
/**
* 交集
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey) {
return getZSetOperations().intersectAndStore(key, otherKeys, destKey);
}
/**
* 使用迭代器获取
*
* @param key
* @param options
* @return
* @throws IOException
*/
public Cursor<ZSetOperations.TypedTuple<String>> scan(String key, ScanOptions options) throws IOException {
Cursor<ZSetOperations.TypedTuple<Object>> scan = getZSetOperations().scan(key, options);
scan.close();
return JSON.parseObject(JSON.toJSONString(scan), new TypeReference<Cursor<ZSetOperations.TypedTuple<String>>>() {
});
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cps007/spring-boot-model.git
git@gitee.com:cps007/spring-boot-model.git
cps007
spring-boot-model
spring-boot-model
dev

搜索帮助