Home >Java >javaTutorial >Code example of java integrated spring cache
This article mainly introduces the sample code of springboot integrated spring cache. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
This article introduces how to use the default spring cache in springboot,
Declarative cache
Spring defines CacheManager The Cache interface is used to unify different caching technologies. For example, JCache, EhCache, Hazelcast, Guava, Redis, etc. When using Spring to integrate Cache, we need to register the bean of the implemented CacheManager.
Spring Boot automatically configures JcacheCacheConfiguration, EhCacheCacheConfiguration, HazelcastCacheConfiguration, GuavaCacheConfiguration, RedisCacheConfiguration, SimpleCacheConfiguration, etc. for us.
Use ConcurrenMapCacheManager by default
When we do not use other third-party cache dependencies, springboot automatically uses ConcurrenMapCacheManager as the cache manager.
Environment dependencies
Introduce spring-boot-starter-cache environment dependencies into the pom file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
Create a book data access layer
First 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); }
You can write a very complex data query operation, such as operationMySQL, nosql, etc. In order to demonstrate this chestnut, I only performed a delay operation on the thread and regarded it as the time to query the database.
Implementation 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 class
@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 and you will find The program prints in sequence on the console:
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 that the program prints a line of logs in 3 seconds. Caching technology has not been turned on at this time.
Enable caching technology
Add @EnableCaching to the entry of the program to enable caching technology:
@SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
In Add the @Cacheable annotation where caching is required, such as adding @Cacheable("books") to the getByIsbn() method. This method turns on the caching strategy. When the cache has this data, the data will be returned directly without waiting. 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); } } }
Start the program again at this time, you will find that 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'}
Only the first 2 data are printed , the program waited for 3 seconds, and the subsequent data was instantly printed on the console, which shows that the cache is working.
【Related Recommendations】
1. Special Recommendation:"php Programmer Toolbox" V0.1 version Download
3. Geek Academy Java Video Tutorial
The above is the detailed content of Code example of java integrated spring cache. For more information, please follow other related articles on the PHP Chinese website!