packagecom.**.**.common.utils;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.HashOperations;importorg.springframework.data.redis.core.RedisCallback;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.core.ValueOperations;importorg.springframework.data.redis.serializer.StringRedisSerializer;importorg.springframework.stereotype.Component;importjava.io.Serializable;importjava.util.Set;importjava.util.concurrent.TimeUnit;
@Slf4j
@Componentpublic classRedisUtil {
@AutowiredprivateRedisTemplate redisTemplate;public voidremove(String... keys) {for(String key : keys) {
remove(key);
}
}public voidremovePattern(String pattern) {
Set keys = this.redisTemplate.keys(pattern);if (keys.size() > 0) {this.redisTemplate.delete(keys);
}
}public voidremove(String key) {if(exists(key)) {this.redisTemplate.delete(key);
}
}public booleanexists(String key) {return this.redisTemplate.hasKey(key).booleanValue();
}public T get(String key) {
T result= null;
ValueOperations operations = this.redisTemplate.opsForValue();
result=operations.get(key);returnresult;
}public booleanset(String key, T value) {boolean result = false;
ValueOperations operations = this.redisTemplate.opsForValue();
operations.set(key, value);
result= true;returnresult;
}/*** 只适用于key对应的值不再更新的问题,如果遇到包含生命周期的值需要更新的情景就不适用了,因为set 方法会丢失该key的生存时间,变成永久有效的
*@paramkey
*@paramvalue
*@paramexpireTime 单位秒
*@param
*@return
*/
public booleanset(String key, T value, Long expireTime) {boolean result = false;
ValueOperations operations = this.redisTemplate.opsForValue();
operations.set(key, value);this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result= true;returnresult;
}/***
* 只适用于key对应的值不再更新的问题,如果遇到包含生命周期的值需要更新的情景就不适用了,因为set 方法会丢失该key的生存时间,变成永久有效的
*@paramkey
*@paramvalue
*@paramexpireTime
*@paramunit
*@param
*@return
*/
public boolean set(String key, T value, Long expireTime, finalTimeUnit unit) {boolean result = false;
ValueOperations operations = this.redisTemplate.opsForValue();
operations.set(key, value);this.redisTemplate.expire(key, expireTime, unit);
result= true;returnresult;
}public T getHash(String hashName, String key) {
T result= null;
HashOperations operations = this.redisTemplate.opsForHash();
result=operations.get(hashName, key);returnresult;
}public voidsetHash(String hashName, String key, T value) {
redisTemplate.opsForHash().put(hashName, key, value);
}/*** 只适用于key对应的值不再更新的问题,如果遇到包含生命周期的值需要更新的情景就不适用了,因为set 方法会丢失该key的生存时间,变成永久有效的
*@paramkey
*@paramexpireTime*/
publicBoolean expire(String key, Long expireTime) {return this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}/*** 设置分布式锁
*@paramkey
*@paramvalue
*@return
*/
public synchronized Boolean setNX(final String key, final String value) throwsException{
Object obj= null;try{
obj= redisTemplate.execute((RedisCallback) connection ->{
StringRedisSerializer serializer= newStringRedisSerializer();
Boolean success=connection.setNX(serializer.serialize(key), serializer.serialize(value));
connection.close();returnsuccess;
});
}catch(Exception e) {
log.error("setNX com.strawhat.redis error, key : {} - {}", key,e);throwe;
}return obj != null ? (Boolean) obj : false;
}/*** 设置分布式锁,超时间单位秒
*@paramkey
*@paramvalue
*@return
*/
public synchronized Boolean setNX(final String key, final String value,long timeOut) throwsException {boolean b = this.setNX(key,value);
redisTemplate.expire(key,timeOut,TimeUnit.SECONDS);returnb;
}/*** 删除锁
*@paramkey
*@return
*/
public void unlock(finalString key) {
redisTemplate.delete(key);
}
}