Cacheable CacheEvict CachePut

2023-02-20 11:23:23 浏览数 (1)

昨天写了spring caching简单入门

今天把省下俩注解也说了

一共是

@Cacheable加缓存(缓存获取不到就调用方法获取结果再放入缓存)

@CachePut更新缓存

@CacheEvict删缓存

我们在Repository实现类加上这几个缓存注解

代码语言:javascript复制
package com.ruben.simplecache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class SimpleBookRepository implements BookRepository {

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

    @Override
    @Cacheable("books")
    public Book getByIsbn(String isbn) {
        simulateSlowService();
        log.warn("getByIsbn called {}", isbn);
        return new Book(isbn, "Some book");
    }

    @Override
    @CacheEvict("books")
    public Book removeByIsbn(String isbn) {
        log.warn("removeByIsbn called {}", isbn);
        simulateSlowService();
        return new Book(isbn, "Some book");
    }

    @Override
    @CachePut("books")
    public Book updateByIsbn(Book book) {
        log.warn("updateByIsbn called {}", book);
        simulateSlowService();
        return book;
    }

    // Don't do this at home
    private void simulateSlowService() {
        try {
            long time = 3000L;
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }

}

AppRunner调用试试

代码语言:javascript复制
package com.ruben.simplecache;

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) throws Exception {
        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"));

        logger.info(".... Updating books");
        logger.info("isbn-1234 -->"   bookRepository.updateByIsbn(new Book("isbn-1234", "Some book")));
        logger.info("isbn-4567 -->"   bookRepository.updateByIsbn(new Book("isbn-4567", "Some book")));
        logger.info(".... Fetching books");
        logger.info("isbn-1234 -->"   bookRepository.getByIsbn("isbn-1234"));
        logger.info("isbn-4567 -->"   bookRepository.getByIsbn("isbn-4567"));

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

}

可以看到执行结果:

代码语言:javascript复制
2023-01-25T15:38:29.004 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : .... Fetching books
2023-01-25T15:38:32.008 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : getByIsbn called isbn-1234
2023-01-25T15:38:32.009 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:35.015 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : getByIsbn called isbn-4567
2023-01-25T15:38:35.015 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}
2023-01-25T15:38:35.016 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:35.016 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}
2023-01-25T15:38:35.016 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:35.016 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:35.016 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : .... Updating books
2023-01-25T15:38:35.016 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : updateByIsbn called Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:38.026 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:38.026 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : updateByIsbn called Book{isbn='isbn-4567', title='Some book'}
2023-01-25T15:38:41.028 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}
2023-01-25T15:38:41.028 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : .... Fetching books
2023-01-25T15:38:41.028 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:41.028 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}
2023-01-25T15:38:41.028 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : ... Removing Books
2023-01-25T15:38:41.028 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : removeByIsbn called isbn-1234
2023-01-25T15:38:44.029 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:44.030 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : removeByIsbn called isbn-4567
2023-01-25T15:38:47.030 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}
2023-01-25T15:38:47.030 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : .... Fetching books
2023-01-25T15:38:50.044 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : getByIsbn called isbn-1234
2023-01-25T15:38:50.044 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-1234 -->Book{isbn='isbn-1234', title='Some book'}
2023-01-25T15:38:53.045 08:00  WARN 31260 --- [           main] c.r.simplecache.SimpleBookRepository     : getByIsbn called isbn-4567
2023-01-25T15:38:53.045 08:00  INFO 31260 --- [           main] com.ruben.simplecache.AppRunner          : isbn-4567 -->Book{isbn='isbn-4567', title='Some book'}

0 人点赞