当前位置:网站首页>Tool classes encapsulated based on redistemplate
Tool classes encapsulated based on redistemplate
2022-07-22 09:17:00 【Flowers bloom without knowing you】
be based on RedisTemplate Encapsulated tool class
Expected simplification redisTemplate The operation of
package com.huakai.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/** * @author huakai */
@Component
public class RedisUtil {
@Autowired
@Qualifier("myRedisTemplate")
private RedisTemplate<String, Object> redisTemplate;
// =============================common============================
/** * Specify cache expiration time * * @param key key * @param time Time ( second ) */
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 Optional.ofNullable(redisTemplate.getExpire(key, TimeUnit.SECONDS)).orElse(0L);
}
/** * Judge key Whether there is * * @param key key * @return true There is false non-existent */
public boolean hasKey(String key) {
try {
return Optional.ofNullable(redisTemplate.hasKey(key)).orElse(false);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Delete cache * * @param keys You can pass a value Or more */
public boolean del(List<String> keys) {
if (keys.size() > 0) {
final Long aLong = Optional.ofNullable(redisTemplate.delete(keys)).orElse(0L);
return aLong > 0;
}
return false;
}
// ============================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) */
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException(" The increment factor must be greater than 0");
}
return Optional.ofNullable(redisTemplate.opsForValue().increment(key, delta)).orElse(0L);
}
/** * Decline * * @param key key * @param delta To cut it down a few ( Less than 0) */
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException(" The decline factor must be greater than 0");
}
return Optional.ofNullable(redisTemplate.opsForValue().increment(key, -delta)).orElse(0L);
}
public long decr(String key) {
return Optional.ofNullable(redisTemplate.opsForValue().increment(key)).orElse(0L);
}
// ================================hash=================================
/** * 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> hKeys(String key) {
return redisTemplate.opsForHash().entries(key);
}
/** * obtain hashKey The length of * * @param key key * @return Hasan Key Size */
public Long hlen(String key) {
Long size = 0L;
try {
size = Optional.of(redisTemplate.opsForHash()).map(o -> o.size(key)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
return size;
}
/** * 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 multiple pieces of data in the table , If it doesn't exist, it will create * @param key key * @param map Corresponding to multiple key values * @return true success false Failure */
public boolean hset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
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) */
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) */
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ===============================list=================================
/** * 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 */
public Object lGet(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * obtain list Specify the content of the range * * @param key key * @param start Start * @param end end 0 To -1 For all values */
public List<Object> lRange(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 */
public long lLen(String key) {
try {
return Optional.ofNullable(redisTemplate.opsForList().size(key)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * take list Put into cache * * @param key key * @param value value */
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 ) */
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 */
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 ) */
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 */
public boolean lUpdate(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 {
return Optional.ofNullable(redisTemplate.opsForList().remove(key, count, value)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ============================set=============================
/** * according to key obtain Set All the values in * * @param key key */
public Set<Object> sMember(String key) {
return redisTemplate.opsForSet().members(key);
}
/** * 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 sIsMember(String key, Object value) {
return Optional.ofNullable(redisTemplate.opsForSet().isMember(key, value)).orElse(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 sAdd(String key, Object... values) {
return Optional.ofNullable(redisTemplate.opsForSet().add(key, values)).orElse(0L);
}
/** * 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 sSetWithTime(String key, long time, Object... values) {
try {
if (time > 0) {
expire(key, time);
}
return Optional.ofNullable(redisTemplate.opsForSet().add(key, values)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * Randomly remove and return an element * * @param key key * @return Removed elements */
public Object sPop(String key) {
return redisTemplate.opsForSet().pop(key);
}
/** * Randomly return the specified length count Elements * * @param key key * @param count Returns the number of elements * @return Returned element */
public List<Object> sRandMember(String key, long count) {
return redisTemplate.opsForSet().randomMembers(key, count);
}
/** * Two set Intersection of sets * * @param key1 One of them set Of key * @param key2 the other one set Of key * @return Two set The difference between the set */
public Set<Object> sDiff(String key1, String key2) {
return redisTemplate.opsForSet().difference(key1, key2);
}
/** * Two set Intersection of sets * * @param key1 One of them set Of key * @param key2 the other one set Of key * @return Two set Intersection */
public Set<Object> Sinter(String key1, String key2) {
return redisTemplate.opsForSet().intersect(key1, key2);
}
/** * Two set Set and union * * @param key1 One of them set Of key * @param key2 the other one set Of key * @return Two set Union */
public Set<Object> sUnion(String key1, String key2) {
return redisTemplate.opsForSet().union(key1, key2);
}
/** * obtain set The length of the cache * * @param key key */
public long sGetSetSize(String key) {
return Optional.ofNullable(redisTemplate.opsForSet().size(key)).orElse(0L);
}
/** * 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) {
return Optional.ofNullable(redisTemplate.opsForSet().remove(key, values)).orElse(0L);
}
// ============================sorted set=============================
/** * Add elements to zSet Get together * @param key key * @param value value * @param score fraction * @return Whether to add successfully */
public boolean zAdd(String key, Object value, double score) {
return Optional.ofNullable(redisTemplate.opsForZSet().add(key, value, score)).orElse(false);
}
/** * Return the data of the specified score range * @param key key * @param start The starting position * @param end End position * @return Specify zSet */
public Set<Object> zRange(String key,long start,long end){
return redisTemplate.opsForZSet().range(key,start,end);
}
}
边栏推荐
猜你喜欢
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
真人踩过的坑,告诉你避免自动化测试新手常犯的10个错误
2021-07-09
多线程常用类
Introduction to class loader
OpenGL drawing coordinate axis indicator
Leetcode703: the k-th element in data flow
flex布局
leetcode_ Add two numbers_ Personal solution
"Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
随机推荐
Implementation of recommendation system collaborative filtering in spark
Vscode configures markdown and the basic syntax of markdown
模拟实现库函数memcpy--复制内存块。详细理解内存重叠及精准复制问题
创建线程池的七种方式
A custom control for picture selection
腾讯云免费升级。
Analysis of encryption methods
Redis学习笔记
LeetCode·1720:解码异或后的数组
分库分表的数据存储方案思路
浏览器类型判断
[dry goods] obstacles and solutions to knowledge sharing
EasyCVR平台安全扫描提示Go pprof调试信息泄露的解决办法
浙大概率第四版,证明正态分布随机变量不相关等价于独立
获取 (对象数组 / 数组) 的(最小 / 最大值)
华泰app开户安全吗,佣金很高吗
MySQL的一些笔记
QT配置OpenCV(二):成功
详解flex布局
开户华泰证券安全吗,在哪开靠谱一些