springboot

springboor缓存

2019-03-19  本文已影响0人  rainbowz
1导入缓存依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2启动类加入@EnableCaching开启缓存
package com.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;

@MapperScan("com.demo.mapper")
@SpringBootApplication

@EnableAutoConfiguration
@EnableCaching
public class DemoApplication {

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

}

3缓存@Cacheable
@CacheConfig(cacheNames = "user")
@Service
public class userService {
 @Autowired
    UserMapper mapper;

    @Cacheable(/*cacheNames = {"user"}*//*,condition = "#a0>1"*/)
  //  @Cacheable(cacheNames = {"user","user2"},key = "id")
    public User getUser(Integer id){
        System.out.println("查询:"+id+"员工");
        User user =mapper.getUser(id);
        System.out.println(user);
        return user;
    }
}

此处的value是必需的,它指定了你的缓存存放在哪块命名空间。

此处的key是使用的spEL表达式,参考上章。这里有一个小坑,如果你把methodName换成method运行会报错,观察它们的返回类型,原因在于methodName是String而methoh是Method。

此处的User实体类一定要实现序列化public class User implements Serializable,否则会报java.io.NotSerializableException异常。

到这里,你已经可以运行程序检验缓存功能是否实现

4配置@CacheConfig

当我们需要缓存的地方越来越多,你可以使用@CacheConfig(cacheNames = {"myCache"})注解来统一指定value的值,这时可省略value,如果你在你的方法依旧写上了value,那么依然以方法的value值为准。

@CacheConfig(cacheNames = "user")
5Cacheput更新

@CachePut注解的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用 。简单来说就是用户更新缓存数据。但需要注意的是该注解的value 和 key 必须与要更新的缓存相同,也就是与@Cacheable 相同。示例

//先调用目标方法 然后将目标方法缓存起来
@CachePut(/*value = "user",*/key = "#result.id")
    public  User updateUser(User user){
    System.out.println("员工更新方法调用!");
         mapper.update(user);
        return user;
    }
6清除@CacheEvict

@CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空 。

//清除缓存数据

    @CacheEvict(/*value = "user",*/allEntries = true/*key = "#id"*/,beforeInvocation = true)
    public  void  delUser(Integer id){
        System.out.println("delete:"+id);
       // mapper.del(id);
        int a=10/0;
    }
图片.png
7.组合@Caching
 @Caching(
            cacheable = {
                    @Cacheable(/*value = "user",*/key = "#email")
            },
            put = {
                    @CachePut(/*value = "user",*/key = "#result.id"),
                   // @CachePut(value = "user",key="#result.last_name")
            }

    )
    public  User  getUserEmail(String email){
          return   mapper.getEmail(email);
    }

实际运行结果

图片.png

查询ID编号为1的字段,控制台打印结果,清除控制台再次查询


图片.png

发现控制台没有输出,说明查询结果从缓存中拿到
如果我们删除了缓存,则需要再次在数据库中查找,否则不会进行查询,allEntry是删除所有缓存内容。

首先查找ID为3的员工,然后使用 @CacheEvict清除缓存数据,del?id=3员工。再次查询发现此时缓存消失,需要再次在数据库中查找。
图片.png 图片.png 图片.png

参考:https://www.bilibili.com/video/av38657363/?p=80

Mybati开启驼峰命名
对缓存的真正CRUD操作 每个缓存有一个自己唯一的名字
condition指定条件
condition = "#a0>1" 条件大于1时才会缓存
unless否定缓存 当unles条件为true,方法的返回值就不会被缓存,可以获取到结果进行判断。


图片.png
上一篇 下一篇

猜你喜欢

热点阅读