Spring Boot文档阅读笔记=Caching Data with Spring

此篇博文展示了使用Spring去管理Bean开启缓存。

 

maven如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.it1995</groupId>
    <artifactId>CacheDataDemo</artifactId>
    <version>1.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>1.18.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.2.RELEASE</version>
            </plugin>
        </plugins>
    </build>

</project>

创建Book的模型,用于查询时返回对象:

Book.java

package cn.it1995.object;

import lombok.Data;

@Data
public class Book {

    private String isbn;
    private String title;

    public Book(String isbn, String title) {

        this.isbn = isbn;
        this.title = title;
    }
}

创建Book仓库接口:

package cn.it1995.repository;

import cn.it1995.object.Book;

public interface BookRepository {

    Book getByIsbn(String isbn);
}

接口的实现:

package cn.it1995.repository.impl;

import cn.it1995.object.Book;
import cn.it1995.repository.BookRepository;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class SimpleBookRepository implements BookRepository {


    @Override
//    @Cacheable("books")
    public Book getByIsbn(String isbn) {

        simulateSlowService();
        return new Book(isbn, "Some book");
    }

    private void simulateSlowService(){

        try{

            long time = 3000L;
            Thread.sleep(time);
        }
        catch(InterruptedException e){

            e.printStackTrace();
        }
    }
}

入口函数:

package cn.it1995;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
//@EnableCaching
public class CacheDataDemoMain {

    public static void main(String[] args){

        SpringApplication.run(CacheDataDemoMain.class);
    }
}

调用如下:

package cn.it1995;

import cn.it1995.repository.BookRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);

    private final BookRepository bookRepository;

    public AppRunner(BookRepository bookRepository){

        this.bookRepository = bookRepository;
    }

    @Override
    public void run(String... args){


        logger.info("......Fetching books......");
        logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
        logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567"));
        logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
        logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567"));
        logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
        logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
    }
}

开启缓存只要将上面注释放开就可以了。

 

未开启缓存时,程序运行如下:

开启缓存后,程序运行如下:

从中可以看到,如果查找相同的数据,开启缓存后,基本上都是秒回。

 

源码打包下载地址:

https://github.com/fengfanchen/Java/tree/master/CacheDataDemo

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页