(一) 实际应用
(1) 插入缓存
String redisKey = String.format("sys:cache:dictTable::SimpleKey [%s,%s]", dictCode, dict.getValue());
try {
RedisUtil.set(redisKey, dict.getCode(),60*60*24*30);
} catch (Exception e) {
log.warn(e.getMessage(), e);
}
或
加时间
RedisUtil.set(ConfigCacheNames.SYS_CONFIG+CacheNames.UNION_KEY+"cookie",sb.toString(),7200);
(2) 查询
Object cacheVerifyCode = RedisUtil.get(OauthCacheNames.VERIFY_CODE_CACHE_PREFIX + authenticationDTO.getVerifyCodeKey());
或
String codeNum = RedisUtil.get(UserCacheNames.USER_CAPTCHA + phoneNum);
(3) 加锁
boolean isLocked = redissonLockClient.fairLock(UserCacheNames.USER_BIND + userVo.getId(), TimeUnit.SECONDS, ConstantNumeral.THREE.value());
boolean isLocked = redissonLockClient.fairLock(UserCacheNames.USER_AUTH + userId, TimeUnit.SECONDS, ConstantNumeral.THREE.value());
(4)//redisson防重复调用
if(redissonLockClient.tryLock(SmsCacheNames.PHONE_CODE_REDISSON+phoneNum, -1, 60)){
smsService.sendMsg(phoneNum, type, codeNum);
}else{
throw new WangyaoBusinessException("验证码仍然有效");
}
(5)
(二) RedisUtil 工具类
public class RedisUtil {
private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
@SuppressWarnings("unchecked")
private static final RedisTemplate<String, Object> REDIS_TEMPLATE = SpringContextUtils.getBean("redisTemplate",
RedisTemplate.class);
public static final StringRedisTemplate STRING_REDIS_TEMPLATE = SpringContextUtils.getBean("stringRedisTemplate",
StringRedisTemplate.class);
// =============================common============================
/**
* 指定缓存失效时间
* @param key 键
* @param time 时间(秒)
* @return 是否成功
*/
public static Boolean expire(String key, long time) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
try {
if (time > 0) {
REDIS_TEMPLATE.expire(key, time, TimeUnit.SECONDS);
}
return Boolean.TRUE;
}
catch (Exception e) {
logger.error("Set expire error: {}", e.getMessage());
return Boolean.FALSE;
}
}
/**
* 根据key 获取过期时间
* @param key 键 不能为null
* @return 时间(秒) 返回-1代表为永久有效 失效时间为0,说明该主键未设置失效时间(失效时间默认为-1)
*/
public static Long getExpire(String key) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
return REDIS_TEMPLATE.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
* @param key 键
* @return true 存在 false 不存在
*/
public static Boolean hasKey(String key) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
try {
return REDIS_TEMPLATE.hasKey(key);
}
catch (Exception e) {
logger.error("Error getting hasKey: {}", e.getMessage());
return Boolean.FALSE;
}
}
/**
* 删除缓存
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public static void del(String... key) {
if (key != null && key.length > 0) {
for (String s : key) {
if (s.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
}
if (key.length == 1) {
REDIS_TEMPLATE.delete(key[0]);
}
else {
REDIS_TEMPLATE.delete(Arrays.asList(key));
}
}
}
// ============================String=============================
/**
* 普通缓存获取
* @param key 键
* @return 值
*/
@SuppressWarnings("unchecked")
public static <T> T get(String key) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
return (T) REDIS_TEMPLATE.opsForValue().get(key);
}
/**
* 普通缓存放入并设置时间
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public static boolean set(String key, Object value, long time) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
try {
if (time > 0) {
REDIS_TEMPLATE.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}
else {
REDIS_TEMPLATE.opsForValue().set(key, value);
}
return true;
}
catch (Exception e) {
logger.error("Redis opsForValue error: {}", e.getMessage());
return false;
}
}
/**
* 递增 此时value值必须为int类型 否则报错
* @param key 键
* @param delta 要增加几(大于0)
* @return 自增后的值
*/
public static Long incr(String key, long delta) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return STRING_REDIS_TEMPLATE.opsForValue().increment(key, delta);
}
/**
* 递减
* @param key 键
* @param delta 要减少几(小于0)
* @return 自减后的值
*/
public static Long decr(String key, long delta) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
if (delta < 0) {
throw new RuntimeException("递减因子必须小于0");
}
return STRING_REDIS_TEMPLATE.opsForValue().increment(key, -delta);
}
public static boolean setLongValue(String key, Long value, long time) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
try {
if (time > 0) {
STRING_REDIS_TEMPLATE.opsForValue().set(key, String.valueOf(value), time, TimeUnit.SECONDS);
}
else {
STRING_REDIS_TEMPLATE.opsForValue().set(key, String.valueOf(value));
}
return true;
}
catch (Exception e) {
logger.error("setLongValue() error: {}", e.getMessage());
return false;
}
}
/**
* 普通缓存获取
* @param key 键
* @return 值
*/
public static Long getLongValue(String key) {
if (key == null) {
return null;
}
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
String result = STRING_REDIS_TEMPLATE.opsForValue().get(key);
if (result == null) {
return null;
}
return Long.valueOf(result);
}
/**
* 批量删除缓存
* @param keys
*/
public static void deleteBatch(List<String> keys) {
if (CollUtil.isEmpty(keys)) {
return;
}
for (String key : keys) {
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
}
REDIS_TEMPLATE.delete(keys);
}
/**
* 批量删除缓存
* @param cacheName 缓存名
* @param cacheKeys 缓存key
*/
public static void deleteBatch(String cacheName, List<?> cacheKeys) {
if (StrUtil.isBlank(cacheName) || CollUtil.isEmpty(cacheKeys)) {
return;
}
List<String> strCacheKeys = cacheKeys.stream().map(String::valueOf).toList();
List<String> keys = new ArrayList<>();
for (String cacheKey : strCacheKeys) {
String key = cacheName + CacheNames.UNION + cacheKey;
keys.add(key);
if (key.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
}
REDIS_TEMPLATE.delete(keys);
}
/**
* 比较和删除标记,原子性
* @return 是否成功
*/
public static boolean cad(String key, String value) {
if (key.contains(StrUtil.SPACE) || value.contains(StrUtil.SPACE)) {
throw new WangyaoException(ResponseEnum.EXCEPTION);
}
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
//通过lure脚本原子验证令牌和删除令牌
Long result = STRING_REDIS_TEMPLATE.execute(new DefaultRedisScript<Long>(script, Long.class),
Collections.singletonList(key),
value);
return !Objects.equals(result, 0L);
}
}
(三) 分布式锁实现基于Redisson
@Slf4j
@Component
public class RedissonLockClient {
@Autowired
private RedissonClient redissonClient;
/**
* 获取锁
*/
public RLock getLock(String lockKey) {
return redissonClient.getLock(lockKey);
}
/**
* 加锁操作
*
* @return boolean
*/
public boolean tryLock(String lockName, long expireSeconds) {
return tryLock(lockName, 0, expireSeconds);
}
/**
* 加锁操作
*
* @return boolean
*/
public boolean tryLock(String lockName, long waitTime, long expireSeconds) {
RLock rLock = getLock(lockName);
boolean getLock = false;
try {
getLock = rLock.tryLock(waitTime, expireSeconds, TimeUnit.SECONDS);
if (getLock) {
log.info("获取锁成功,lockName={}", lockName);
} else {
log.info("获取锁失败,lockName={}", lockName);
}
} catch (InterruptedException e) {
log.error("获取式锁异常,lockName=" + lockName, e);
getLock = false;
}
return getLock;
}
public boolean fairLock(String lockKey, TimeUnit unit, int leaseTime) {
RLock fairLock = redissonClient.getFairLock(lockKey);
try {
boolean existKey = existKey(lockKey);
// 已经存在了,就直接返回
if (existKey) {
return false;
}
return fairLock.tryLock(3, leaseTime, unit);
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
public boolean existKey(String key) {
return redissonClient.getKeys().countExists(key)>0;
}
/**
* 锁lockKey
*
* @param lockKey
* @return
*/
public RLock lock(String lockKey) {
RLock lock = getLock(lockKey);
lock.lock();
return lock;
}
/**
* 锁lockKey
*
* @param lockKey
* @param leaseTime
* @return
*/
public RLock lock(String lockKey, long leaseTime) {
RLock lock = getLock(lockKey);
lock.lock(leaseTime, TimeUnit.SECONDS);
return lock;
}
/**
* 解锁
*
* @param lockName 锁名称
*/
public void unlock(String lockName) {
try {
redissonClient.getLock(lockName).unlock();
} catch (Exception e) {
log.error("解锁异常,lockName=" + lockName, e);
}
}
}
(四)Redisson配置管理器,用于初始化的redisson实例
@Slf4j
public class RedissonManager {
private Config config = new Config();
private Redisson redisson = null;
public RedissonManager() {
}
public RedissonManager(RedissonProperties redissonProperties) {
//装配开关
Boolean enabled = redissonProperties.getEnabled();
if (enabled) {
try {
config = RedissonConfigFactory.getInstance().createConfig(redissonProperties);
redisson = (Redisson) Redisson.create(config);
} catch (Exception e) {
log.error("Redisson初始化错误", e);
}
}
}
public Redisson getRedisson() {
return redisson;
}
/**
* Redisson连接方式配置工厂
* 双重检查锁
*/
static class RedissonConfigFactory {
private RedissonConfigFactory() {
}
private static volatile RedissonConfigFactory factory = null;
public static RedissonConfigFactory getInstance() {
if (factory == null) {
synchronized (Object.class) {
if (factory == null) {
factory = new RedissonConfigFactory();
}
}
}
return factory;
}
/**
* 根据连接类型創建连接方式的配置
*
* @param redissonProperties
* @return Config
*/
Config createConfig(RedissonProperties redissonProperties) {
Preconditions.checkNotNull(redissonProperties);
Preconditions.checkNotNull(redissonProperties.getAddress(), "redis地址未配置");
RedisConnectionType connectionType = redissonProperties.getType();
// 声明连接方式
RedissonConfigStrategy redissonConfigStrategy;
if (connectionType.equals(RedisConnectionType.SENTINEL)) {
redissonConfigStrategy = new SentinelRedissonConfigStrategyImpl();
} else if (connectionType.equals(RedisConnectionType.CLUSTER)) {
redissonConfigStrategy = new ClusterRedissonConfigStrategyImpl();
} else if (connectionType.equals(RedisConnectionType.MASTERSLAVE)) {
redissonConfigStrategy = new MasterslaveRedissonConfigStrategyImpl();
} else {
redissonConfigStrategy = new StandaloneRedissonConfigStrategyImpl();
}
Preconditions.checkNotNull(redissonConfigStrategy, "连接方式创建异常");
return redissonConfigStrategy.createRedissonConfig(redissonProperties);
}
}
}
(五)集群方式Redisson配置
/**
* 集群方式Redisson配置
* cluster方式至少6个节点(3主3从)
* 配置方式:127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
*
*/
@Slf4j
public class ClusterRedissonConfigStrategyImpl implements RedissonConfigStrategy {
@Override
public Config createRedissonConfig(RedissonProperties redissonProperties) {
Config config = new Config();
try {
String address = redissonProperties.getAddress();
String password = redissonProperties.getPassword();
String[] addrTokens = address.split(",");
// 设置集群(cluster)节点的服务IP和端口
for (int i = 0; i < addrTokens.length; i++) {
config.useClusterServers().addNodeAddress(GlobalConstant.REDIS_CONNECTION_PREFIX + addrTokens[i]);
if (StrUtil.isNotBlank(password)) {
config.useClusterServers().setPassword(password);
}
}
log.info("初始化集群方式Config,连接地址:" + address);
} catch (Exception e) {
log.error("集群Redisson初始化错误", e);
e.printStackTrace();
}
return config;
}
}
(六)防止重复提交分布式锁拦截器
@Aspect
@Component
public class RepeatSubmitAspect extends BaseAspect {
@Resource
private RedissonLockClient redissonLockClient;
/***
* 定义controller切入点拦截规则,拦截JRepeat注解的业务方法
*/
@Pointcut("@annotation(jRepeat)")
public void pointCut(JRepeat jRepeat) {
}
/**
* AOP分布式锁拦截
*
* @param joinPoint
* @return
* @throws Exception
*/
@Around(value = "pointCut(jRepeat)", argNames = "joinPoint,jRepeat")
public Object repeatSubmit(ProceedingJoinPoint joinPoint, JRepeat jRepeat) throws Throwable {
String[] parameterNames = new String[]{}; //todo 需要重写;
// new LocalVariableTableParameterNameDiscoverer().getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
if (Objects.nonNull(jRepeat)) {
// 获取参数
Object[] args = joinPoint.getArgs();
// 进行一些参数的处理,比如获取订单号,操作人id等
StringBuffer lockKeyBuffer = new StringBuffer();
String key = getValueBySpEL(jRepeat.lockKey(), parameterNames, args, "RepeatSubmit").get(0);
// 公平加锁,lockTime后锁自动释放
boolean isLocked = false;
try {
isLocked = redissonLockClient.fairLock(key, jRepeat.timeUnit(), jRepeat.lockTime());
// 如果成功获取到锁就继续执行
if (isLocked) {
// 执行进程
return joinPoint.proceed();
} else {
// 未获取到锁
throw new Exception(jRepeat.info());
}
} finally {
if (jRepeat.delKey()) {
// 如果锁还存在,在方法执行完成后,释放锁
if (isLocked) {
redissonLockClient.unlock(key);
}
}
}
}
return joinPoint.proceed();
}
}
(七) 防止重复提交的注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface JRepeat {
/**
* redis 锁key的
*
* @return redis 锁key
*/
String lockKey() default "";
/**
* 有效期 默认:1 有效期要大于程序执行时间,否则请求还是可能会进来
* @return expireTime
*/
int lockTime() default 1;
/**
* 时间单位 默认:s
* @return TimeUnit
*/
TimeUnit timeUnit() default TimeUnit.SECONDS;
/**
* 提示信息,可自定义
* @return String
*/
String info() default "重复请求,请稍后重试";
/**
* 是否在业务完成后删除key true:删除 false:不删除
* @return boolean
*/
boolean delKey() default false;
}