java version of spring cloud + spring boot + redis social e-commerce platform (xiii) springboot integrated spring cache

Declarative Cache

Spring Cache interface is used to define CacheManager and unify different caching techniques. For example JCache, EhCache, Hazelcast, Guava, Redis like. When using Spring Integration Cache, we need to realize Bean CacheManager of registration.

Spring Boot for us to automatically configure the JcacheCacheConfiguration, EhCacheCacheConfiguration, HazelcastCacheConfiguration, GuavaCacheConfiguration, RedisCacheConfiguration, SimpleCacheConfiguration and so on.

Default ConcurrenMapCacheManager

When we do not use other third-party caching dependent, springboot automatically use ConcurrenMapCacheManager as the cache manager.

Environmental dependent

The introduction of spring-boot-starter-cache environment depend in pom file:

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

Create a book Data Access Layer

Create an entity class

public class Book {

private String isbn;
private String title;

public Book(String isbn, String title) {
    this.isbn = isbn;
    this.title = title;
}
….getter 
….setter

}

Create a data access interface

public interface BookRepository {

    Book getByIsbn(String isbn);

}

This you can write a very complex data query operations, such as operating mysql, nosql and so on. To demonstrate this chestnut, I only had about a delay operation thread, as is the time to query the database.

Implement an interface class:

@Component
public class SimpleBookRepository implements BookRepository {

    @Override

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

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

}

Test category

@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"));
    }

}

Start the program, you'll find in a console in turn printed:

2014-06-05 12:15:35.783 … : …. Fetching books

2014-06-05 12:15:40.783 … : isbn-1234 –> >Book{isbn=’isbn-1234’, title=’Some book’}

2014-06-05 12:15:43.784 … : isbn-1234 –>Book{isbn=’isbn-1234’, title=’Some book’}

2014-06-05 12:15:46.786 … : isbn-1234 –>Book{isbn=’isbn-1234’, title=’Some book’}

You will find the program in order 3s print line of the log. Then not open the cache technology.

Open caching technology

Join @ EnableCaching open caching at the entrance of the program:

@SpringBootApplication
@EnableCaching
public class DemoApplication {

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

Join @Cacheable comment needs to be cached in place, such as adding @Cacheable ( "books") on getByIsbn () method, which will open the caching policy, when there is the data cache, it returns the data directly, without waiting for to query the database.

@Component
public class SimpleBookRepository implements BookRepository {

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

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

}

The next time you start the program, you will find the program prints:

isbn-1234 –>Book{isbn=’isbn-1234’, title=’Some book’} 
2017-04-23 18:17:09.479 INFO 8054 — [ main] forezp.AppRunner : isbn-4567 –>Book{isbn=’isbn-4567’, title=’Some book’} 
2017-04-23 18:17:09.480 INFO 8054 — [ main] forezp.AppRunner : isbn-1234 –>Book{isbn=’isbn-1234’, title=’Some book’} 
2017-04-23 18:17:09.480 INFO 8054 — [ main] forezp.AppRunner : isbn-4567 –>Book{isbn=’isbn-4567’, title=’Some book’} 
2017-04-23 18:17:09.481 INFO 8054 — [ main] forezp.AppRunner : isbn-1234 –>Book{isbn=’isbn-1234’, title=’Some book’} 
2017-04-23 18:17:09.481 INFO 8054 — [ main] forezp.AppRunner : isbn-1234 –>Book{isbn=’isbn-1234’, title=’Some book’}

Social e-commerce platform source code, please add penguin beg: 3536247259

Guess you like

Origin www.cnblogs.com/springfresh/p/11001930.html