ehcache redis 双缓存(aop + 自定义注解)

该博客介绍了如何使用AOP和自定义注解实现Ehcache与Redis的双缓存机制。在查询数据时,首先查询Ehcache,未命中再查询Redis,最后若仍未找到则查询数据库。插入、更新和删除操作会同步更新Ehcache和Redis缓存。示例中包含了自定义的查询、添加和删除注解,以及对应的AOP切面处理。
摘要由CSDN通过智能技术生成

ehcache redis 双缓存(aop + 自定义注解)

 

查询数据:先查询ehcache缓存,如果没有数据,再查询redis缓存;如果查询不到,查询后端数据库

插入数据:后端数据库插入数据后,同时插入ehcache、redis缓存

更新数据:后端数据库更新数据后,同时更新ehcache、redis缓存

删除数据:后端数据库删除数据时,同时删除ehcache、redis缓存

 

 

****************************

示例

 

**********************

pojo 层

 

Person

@Data
public class Person implements Serializable {

    private Integer id;

    private String name;
    private Integer age;
}

 

**********************

myannotation 层

 

CustomCacheCacheable:自定义查询注解

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomCacheable {          //缓存查询注解

    String value() default "";

    /*
    key为方法参数名、参数名的属性
    Person  fun(String name,Person person){} //key可用格式:"#name"、“#person.id”
     */
    String key() default "";
}

 

CustomCachePut:自定义缓存添加注解

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomCachePut {         //缓存添加注解

    String value() default "";

    /*
    key为方法参数名、参数名的属性、返回值、返回值的属性
    Person  fun(String name,Person person){} //key可用格式:"#name"、“#person.id”、“#result”、"#result.id"
     */
    String key() default "";
}

 

CustomCacheEvict:自定义缓存删除注解

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomCacheEvict {       //缓存删除注解

    String value() default "";

    /*
    key为方法参数名、参数名的属性
    Person  fun(String name,Person person){} //key可用格式:"#name"、“#person.id”、“#result”、“#result.id”
     */
    String key() default "";
}

 

**********************

aop 层

 

CustomAspect

@Aspect
@Component
public class CustomAspect {

    private final Logger logger= LoggerFactory.getLogger(this.getClass().getName());

    @Resource(name = "ehcacheCacheManager")
    private CacheManager cacheManager;

    @Resource(name = "redisCacheManager")
    private RedisCacheManager redisCacheManager;

    @Pointcut("@annotation(com.example.demo.myannotation.CustomCacheable)")
    public void fun(){

    }

    @Pointcut("@annotation(com.example.demo.myannotation.CustomCachePut)" +
            "||@annotation(com.example.demo.myannotation.CustomCacheEvict)")
    public void fun2(){

    }

    @Around("fun()")
    public Person CustomCacheable(ProceedingJoinPoint joinPoint){
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值