springboot学习笔记(九) spring cache

来源:互联网 发布:linux系统shell 编辑:程序博客网 时间:2024/06/01 09:06

springboot学习笔记(九)

开启缓存spring cache

  • 导入 pom 依赖

    <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.7.RELEASE</version></parent><dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-cache</artifactId>    </dependency></dependencies>
  • 构建一个程序入口

    @SpringBootApplicationpublic class CacheApp {    public static void main(String[] args) {        SpringApplication.run(CacheApp.class, args);    }}
  • application.yml 文件如下

    server:   port: 8089
  • 构建一个数据访问层

    @Repositorypublic class UserDaoImpl implements UserDao{    @Override    public User getById(Integer id) {        System.out.println("get user " + id + " in repo");        dosomething();        return new User(id);    }    private void dosomething(){        try {            Thread.sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}
  • 编写测试类

    @Componentpublic class AppRunner implements CommandLineRunner {    private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);    private final UserDao userDaoImpl;    public AppRunner(UserDao userDaoImpl) {        this.userDaoImpl = userDaoImpl;    }    @Override    public void run(String... args) throws Exception {        logger.info("Fetching users...");        logger.info("" + userDaoImpl.getById(1));        logger.info("" + userDaoImpl.getById(2));        logger.info("" + userDaoImpl.getById(1));        logger.info("" + userDaoImpl.getById(2));        logger.info("" + userDaoImpl.getById(1));        logger.info("" + userDaoImpl.getById(1));        System.out.println();    }}
  • 启动程序,控制台打印如下:

    可以看见每一次都执行了数据访问层的方法,此时并没有开启缓存技术

开启缓存技术

  • 在程序的入口中加入@ EnableCaching开启缓存技术:

    @SpringBootApplication@EnableCachingpublic class CacheApp {    public static void main(String[] args) {        SpringApplication.run(CacheApp.class, args);    }}
  • 在需要缓存的地方加入@Cacheable注解,比如在 getById() 方法上加入@Cacheable(“users”),这个方法就开启了缓存策略,当缓存有这个数据的时候,会直接返回数据,不会等待去查询数据库。

    @Repositorypublic class UserDaoImpl implements UserDao{    @Override    @Cacheable("users")    public User getById(Integer id) {        System.out.println("get user " + id + " in repo");        dosomething();        return new User(id);    }    private void dosomething(){        try {            Thread.sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}
  • 此时启动程序,控制台打印如下:

    只有前面两个数据被打印了,说明此时缓存起作用了

    个人学习springboot博客地址:http://blog.csdn.net/forezp/article/details/70341818http://blog.didispace.com/Spring-Boot%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/
阅读全文
0 0
原创粉丝点击