SpringBoot之Caching Data with Spring

以从BookRepository获取Book为例,当之前已经获存在过了的book实例,则不再重新实例化,而是使用缓存的。

1 Book.java / BookRepository.java/ SimpleBookRepository.java

package caching.data.with.spring.hello;

public class Book {

    private String isbn;
    private String title;

    public Book(String isbn, String title) {
        super();
        this.isbn = isbn;
        this.title = title;
    }

    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "Book [isbn=" + isbn + ", title=" + title + "]";
    }
}
package caching.data.with.spring.hello;

public interface BookRepository {

    Book getByIsbn(String isbn);
}
package caching.data.with.spring.hello;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class SimpleBookRepository implements BookRepository {

    @Cacheable("books")
    public Book getByIsbn(String isbn) {
        simulateSlowService();
        return new Book(isbn, "Some Book");
    }

    private void simulateSlowService(){
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }

}

说明

@Cacheable(“books”)

2 Application.java

package caching.data.with.spring.hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Component;

@SpringBootApplication
@EnableCaching
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    @Component
    static class Runner implements CommandLineRunner{
        @Autowired
        private BookRepository bookRepository;

        public void run(String... args) throws Exception {
            log.info(".....获取Books");
            log.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
            log.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567"));
            log.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
            log.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
            log.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567"));
        }
    }

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

}

说明:

  • 如果不采用cache,log记录的book实例化的时间应该是间隔5s的。

  • 例子中采用cache,已经存在的book实例(名称相同)则不会在进行实例化了。运行结果如下:

2016-06-14 17:07:20.997 INFO 13200 — [ main] c.data.with.spring.hello.Application : …..获取Books
2016-06-14 17:07:26.066 INFO 13200 — [ main] c.data.with.spring.hello.Application : isbn-1234 –>Book [isbn=isbn-1234, title=Some Book]
2016-06-14 17:07:31.071 INFO 13200 — [ main] c.data.with.spring.hello.Application : isbn-4567 –>Book [isbn=isbn-4567, title=Some Book]
2016-06-14 17:07:31.073 INFO 13200 — [ main] c.data.with.spring.hello.Application : isbn-1234 –>Book [isbn=isbn-1234, title=Some Book]
2016-06-14 17:07:31.073 INFO 13200 — [ main] c.data.with.spring.hello.Application : isbn-1234 –>Book [isbn=isbn-1234, title=Some Book]
2016-06-14 17:07:31.073 INFO 13200 — [ main] c.data.with.spring.hello.Application : isbn-4567 –>Book [isbn=isbn-4567, title=Some Book]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值