当前位置:网站首页>Redis caching tool class, worth owning~
Redis caching tool class, worth owning~
2022-07-07 23:48:00 【Xiaoqu classmate】
Still for redis Are you worried about the cached tool class , Here is a careful summary for you redis Various tool classes of cache , Very comprehensive , Come and see ~
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/** * Specify cache expiration time * * @param key key * @param time Time ( second ) * @return */
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * according to key Get expiration time * * @param key key Not for null * @return Time ( second ) return 0 Stands for permanent validity */
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/** * Judge key Whether there is * * @param key key * @return true There is false non-existent */
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Delete cache * * @param key You can pass a value Or more */
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
//============================String=============================
/** * Normal cache fetch * * @param key key * @return value */
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/** * Normal cache put in * * @param key key * @param value value * @return true success false Failure */
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * The normal cache is put in and set the time * * @param key key * @param value value * @param time Time ( second ) time Be greater than 0 If time Less than or equal to 0 Will be set indefinitely * @return true success false Failure */
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Increasing * * @param key key * @param delta How much more should I add ( Greater than 0) * @return */
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException(" The increment factor must be greater than 0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/** * Decline * * @param key key * @param delta To cut it down a few ( Less than 0) * @return */
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException(" The decline factor must be greater than 0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
//================================Map=================================
/** * HashGet * * @param key key Not for null * @param item term Not for null * @return value */
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/** * obtain hashKey All the corresponding key values * * @param key key * @return Corresponding multiple key values */
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/** * HashSet * * @param key key * @param map Corresponding to multiple key values * @return true success false Failure */
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * HashSet And set the time * * @param key key * @param map Corresponding to multiple key values * @param time Time ( second ) * @return true success false Failure */
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * To a piece of hash Put data in the table , If it doesn't exist, it will create * * @param key key * @param item term * @param value value * @return true success false Failure */
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * To a piece of hash Put data in the table , If it doesn't exist, it will create * * @param key key * @param item term * @param value value * @param time Time ( second ) Be careful : If there is already hash Watch has time , This will replace the original time * @return true success false Failure */
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Delete hash The values in the table * * @param key key Not for null * @param item term Can make multiple Not for null */
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/** * Judge hash Whether there is a value of this item in the table * * @param key key Not for null * @param item term Not for null * @return true There is false non-existent */
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/** * hash Increasing If it doesn't exist , It creates a And return the added value to * * @param key key * @param item term * @param by How much more should I add ( Greater than 0) * @return */
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/** * hash Decline * * @param key key * @param item term * @param by Remember less ( Less than 0) * @return */
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
//============================set=============================
/** * according to key obtain Set All the values in * * @param key key * @return */
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * according to value From a set Query in , Whether there is * * @param key key * @param value value * @return true There is false non-existent */
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Put data into set cache * * @param key key * @param values value It can be more than one * @return The number of successes */
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * take set Data is put into the cache * * @param key key * @param time Time ( second ) * @param values value It can be more than one * @return The number of successes */
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * obtain set The length of the cache * * @param key key * @return */
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * The removal value is value Of * * @param key key * @param values value It can be more than one * @return Number of removed */
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//===============================list=================================
/** * obtain list The contents of the cache * * @param key key * @param start Start * @param end end 0 To -1 For all values * @return */
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * obtain list The length of the cache * * @param key key * @return */
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * Through the index obtain list The value in * * @param key key * @param index Indexes index>=0 when , 0 Header ,1 The second element , By analogy ;index<0 when ,-1, Tail ,-2 The next to last element , By analogy * @return */
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * take list Put into cache * * @param key key * @param value value * @return */
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * take list Put into cache * * @param key key * @param value value * @param time Time ( second ) * @return */
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * take list Put into cache * * @param key key * @param value value * @return */
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * take list Put into cache * * @param key key * @param value value * @param time Time ( second ) * @return */
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Modify according to the index list A piece of data in * * @param key key * @param index Indexes * @param value value * @return */
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * remove N The values are value * * @param key key * @param count How many removed * @param value value * @return Number of removed */
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//================ Ordered set sort set===================
/** * Orderly set Additive elements * * @param key * @param value * @param score * @return */
public boolean zSet(String key, Object value, double score) {
return redisTemplate.opsForZSet().add(key, value, score);
}
public long batchZSet(String key, Set<ZSetOperations.TypedTuple> typles) {
return redisTemplate.opsForZSet().add(key, typles);
}
public void zIncrementScore(String key, Object value, long delta) {
redisTemplate.opsForZSet().incrementScore(key, value, delta);
}
public void zUnionAndStore(String key, Collection otherKeys, String destKey) {
redisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey);
}
/** * obtain zset Number * @param key * @param value * @return */
public long getZsetScore(String key, Object value) {
Double score = redisTemplate.opsForZSet().score(key, value);
if(score==null){
return 0;
}else{
return score.longValue();
}
}
/** * Get ordered set key Member of the member Ranking . * The members of the ordered set press score The value is decreasing ( From big to small ) Sort . * @param key * @param start * @param end * @return */
public Set<ZSetOperations.TypedTuple> getZSetRank(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
}
}
边栏推荐
- Anxin vb01 offline voice module access intelligent curtain guidance
- 数据分析系列 之3σ规则/依据拉依达准则来剔除异常值
- postgres timestamp转人眼时间字符串或者毫秒值
- go time包常用函数
- May day C - most
- C - Fibonacci sequence again
- BSS 7230 flame retardant performance test of aviation interior materials
- Svn relocation
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- DataGuard active / standby cleanup archive settings
猜你喜欢
Installing gradle
Navicat connects Oracle
一鍵免費翻譯300多頁的pdf文檔
光流传感器初步测试:GL9306
SAP HR reward and punishment information export
Open source hardware small project: anxinco esp-c3f control ws2812
Laser slam learning (2d/3d, partial practice)
Chisel tutorial - 05 Sequential logic in chisel (including explicit multi clock, explicit synchronous reset and explicit asynchronous reset)
Chisel tutorial - 03 Combinatorial logic in chisel (chisel3 cheat sheet is attached at the end)
HB 5469 combustion test method for non-metallic materials in civil aircraft cabin
随机推荐
SAP HR reward and punishment information export
[summary] some panels and videos seen
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
JNI uses asan to check memory leaks
gorm 关联关系小结
蓝桥ROS中使用fishros一键安装
SAP HR labor contract information 0016
Archery installation test
UIC564-2 附录4 –阻燃防火测试:火焰的扩散
【实验分享】通过Console口登录到Cisco设备
What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method
Reverse output three digit and arithmetic sequence
机器人(自动化)等专业课程创新的结果
AITM3.0005 烟雾毒性测试
[stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
webflux - webclient Connect reset by peer Error
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
The for loop realizes 1-100 addition and eliminates the 4-digit tail number
数据库面试题+解析
SQL uses the in keyword to query multiple fields