@Cacheing实现控制是否更新缓存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33222871/article/details/83893642
  • 项目使用了springCache,早先使用的是@Cacheable和@CachePut两个注解;这样要实现从缓存读数据和更新缓存,就需要两个方法申明,mybatis对应配置sql语句也需要两份;
  • 想实现的效果是一个方法既能做到拿数据,也能更新缓存,具体什么操作由一个参数指定;

因此使用了**@Caching**注解
其声明如下

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Caching {
    Cacheable[] cacheable() default {};

    CachePut[] put() default {};

    CacheEvict[] evict() default {};
}

因此方法申明如下

@Caching(
            cacheable = {
                    @Cacheable(key = "#root.args[0]+'" + Asset.KEY_CACHE_SCHOOL_SCENES + "'", value = Asset.VALUE_CACHE_SCHOOL_SCENES, unless = "#result == null")
            },
            put = {
                    @CachePut(key = "#root.args[0]+'" + Asset.KEY_CACHE_SCHOOL_SCENES + "'", value = Asset.VALUE_CACHE_SCHOOL_SCENES, condition = " #root.args[1]")

            }
    )
    List<Activity> selectScenes(@Param("id") int schoolId, boolean update) throws Exception;
   

当put的conditiontrue时,即方法最后一个参数为true时,将会执行对应的sql语句,更新缓存

猜你喜欢

转载自blog.csdn.net/qq_33222871/article/details/83893642