【Redis】Redis相关

1、缓存穿透: 要重视,尤其是@Cacheable、返回值可能为null的场景下

import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.cache.interceptor.CacheOperationInvoker;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;

/**
 * Optional包装工具类,代替@Cacheable场景下的Optional来使用
 * <p>
 * {@link org.springframework.cache.interceptor.CacheAspectSupport#execute(CacheOperationInvoker, Method, CacheAspectSupport.CacheOperationContexts)}
 * <p>
 * 因为spring-cache不支持Optional.EMPTY, 在将Optional对象set入cache时, 会做拆包
 * cacheValue = this.unwrapReturnValue(returnValue)
 * 进而导致从cache中get时,无法区分以下两种情况(为null默认缓存未命中,框架的逻辑,无法绕过):
 * 1、key存在,value为null
 * 2、key不存在
 * 进而导致Optional.EMPTY也无法绕过,引发缓存穿透问题,故写此工具类,代替Optional
 * <p>
 * 其次,Optional没有implements Serializable,因此无法使用JDK序列化放入redis
 * <p>
 * {@link com.company.ec.cache.redis.RedisClient#put(String, Object, int)} byte[] values = SerializeUtils.serialize(value);
 * <p>
 */
public final class OptionalWrapUtil<T> implements Serializable 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值