首页 > 编程语言 > 详细

spring cache 注解

时间:2020-11-03 20:45:10      阅读:42      评论:0      收藏:0      [点我收藏+]
/**
* springboot启动类
*/
@SpringBootApplication
@EnableCaching // 开启缓存
public class SpringbootCacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootCacheApplication.class, args);
    }

}
/**
 * @CacheConfig : 这个注解用于指定这个service类中的缓存操作的公共属性,
 * 比如缓存的名字可以使用cacheNames指定,那么在下面的每一个注解中都可以不指定,默认使用的就是这个指定的
 */
@Service("userService")
//@CacheConfig(cacheNames = "user")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * @Cacheable : 这个注解是在方法运行之前检查缓存中是否存在指定的key的数据,如果存在,那么直接返回
     * 如果不存在,那么执行方法体,最后将方法体返回的结果添加到缓存中
     * 1、cacheNames/value : 指定cache的名字,指定将方法的返回值放在那个缓存中,是数组的方式
     * 2、key : 指定缓存的key,如果不指定的,那么默认使用方法参数的值,当然也是可以使用一些表达式指定这个key的值
     * 1、spEL表达式:
     * 1、当前被调用的方法名称 ---- #root.methodName  ---- #root.method.name
     * 2、当前目标对象  ---- #root.target
     * 3、当前目标对象的类  ----- #root.targetClass
     * 4、当前被调用对象的参数列表  ---- #root.args[index]
     * 5、方法参数的名字,直接使用`#名字`即可获取参数的值  ------ `#userId`
     * 6、方法的返回值    -----    #result
     * 3、keyGenerator : key的生成器,这个和key只能同时指定一个,当然也是可以自定义这个生成器
     * 4、condition :  指定缓存的条件,只有满足这个条件的时候才会使用缓存 --- condition = "#userId>2"
     * 5、unless :  当这个条件为true的时候就不缓存,这个和condition条件相反。
     * #result:使用返回的结果进行判断,比如当我们对返回结果为空的时候不使用缓存,那么可以写成unless = "#result==null"
     * 6、cacheManager : 指定缓存管理器
     * 7、sync : 是否使用异步模式
     * 8、注意:springBoot默认是将返回值为null的时候也会将其缓存起来,我们可以使用unless条件对结果进行判断是否缓存
     */
    @Override
    @Cacheable(value = "user", key = "#userid", condition = "#userid>2", unless = "#result==null")
    public User queryById(Integer userid) {
        System.out.println("根据id查询了");
        return this.userMapper.queryById(userid);
    }

    /**
     * @CachePut : 这个注解的作用是,在方法体执行完成之后,将返回的结果添加到缓存中,可以用于添加和修改操作
     * 其中可以设置的参数和@cacheable差不多
     * 注意:**只要是标注了这个注解之后,那么这个方法一定是要执行的,因为需要将方法执行的结果添加到缓存中**
     */

    @Override
    @CachePut(value = "user", key = "#user.userid")
    public User insert(User user) {
        System.out.println("添加了用户");
        this.userMapper.insert(user);
        System.out.println(user.getUserid());
        return user;
    }

    /**
     * @CacheEvict : 这个注解的作用是清除缓存,默认是在执行方法体之后清除缓存,如果执行的代码出现了异常,那么这个清除缓存将不会执行
     * 1、vaue : 指定缓存的名字
     * 2、key : 指定需要删除的key
     * 3、allEntries : 是否删除指定缓存中的全部缓存,默认为false,一旦指定为true,那么将会删除value指定的cache中的全部缓存
     * 4、beforeInvocation : 是否在执行方法体的代码之前执行清除缓存,默认为false,如果指定了为true,那么就会在方法执行之前清除缓存,
     * 此时如果方法体运行出错,那么缓存中的数据将不能回滚
     */

    @Override
    @CacheEvict(value = "user", key = "#userid")
    public boolean deleteById(Integer userid) {
        return this.userMapper.deleteById(userid) > 0;
    }

    /**
     * @Caching : 这个是一个组合注解,针对一个方法逻辑中多种的缓存操作
     * 1、cacheable : 一个数组,其中指定@Cacheable这个注解,可以指定多个,用于在执行方法之前先查询缓存,如果没有才会执行方法体,并且将结果缓存起来
     * 2、put: 一个数组,其中只能指定@CachePut这个注解,可以指定多个,用于在执行方法之后将返回的结果添加到缓存中
     * 3、evict : 一个数组,其中只能指定@CacheEvict这个注解,用于在方法执行完成之后清除缓存
     */

    @Override
    @Caching(
            cacheable = {
                    @Cacheable(value = "user", key = "#name")
            },
            put = {
                    @CachePut(value = "user", key = "#result.userpwd"),
                    @CachePut(value = "user", key = "#result.address")
            }
    )
    public User getByName(String name) {
        System.out.println("执行了getByName方法");
        return this.userMapper.getByName(name);
    }
}

spring cache 注解

原文:https://www.cnblogs.com/IgIsI/p/13922040.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!