com.onestop.common.redis.util.OsRedisUtils Maven / Gradle / Ivy
/*
*
* * Copyright (c) 2021 os-parent Authors. All Rights Reserved.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package com.onestop.common.redis.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redis工具类
*
* @author Clark
* @version 2021-05-10
*/
public class OsRedisUtils {
/**
* 注入redisTemplate bean
*/
@Autowired
private RedisTemplate redisTemplate;
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return boolean
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key获取过期时间
* 如果该值没有设置过期时间,就返回-1;
* 如果没有该值,就返回-2;
*
* @param key 键 不能为null
* @return 时间(秒)
*/
public long getExpire(String key) {
return this.redisTemplate.getExpire(key);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return this.redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
this.redisTemplate.delete(key[0]);
} else {
this.redisTemplate.delete((Collection) CollectionUtils.arrayToList(key));
}
}
}
/**
* 删除指定前缀的缓存:参数不可为空
* @param prefix
*/
public void delByPrefix(String prefix) {
if (StrUtil.isBlank(prefix)) {
return;
}
Set keys = this.redisTemplate.keys(prefix + "*");
if (CollUtil.isNotEmpty(keys)) {
this.redisTemplate.delete(keys);
}
}
/**
* 删除多层Key:模糊匹配
* @param prefix 参数可为空
* @param key 参数不可为空
*/
public void delByPrefixAndKey(String prefix, String key) {
if (StrUtil.isBlank(key)) {
return;
}
String queryKey = "*" + key;
if (StrUtil.isNotBlank(prefix)) {
queryKey = prefix + queryKey;
}
Set keys = this.redisTemplate.keys(queryKey);
if (CollUtil.isNotEmpty(keys)) {
this.redisTemplate.delete(keys);
}
}
/**
* 清空缓存
*/
public void clear() {
Set keys = this.redisTemplate.keys("*");
this.redisTemplate.delete(keys);
}
// ============================String(字符串)=============================
/**
* 普通缓存获取
*
* @param key 键
* @return String值(推荐JSON格式存储时使用)
*/
public String getStr(String key) {
return key == null ? null : this.redisTemplate.opsForValue().get(key).toString();
}
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : this.redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
this.redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增: +1
*
* @param key 键
* @return long
*/
public long incr(String key) {
return this.redisTemplate.opsForValue().increment(key);
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return long
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return this.redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减: -1
*
* @param key 键
* @return long
*/
public long decr(String key) {
return this.redisTemplate.opsForValue().decrement(key);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return long
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return this.redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Hash(哈希)=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
return this.redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy