View Javadoc
1   package org.oxerr.spring.cache.redis.scored.example.helloworld;
2   
3   import java.time.Duration;
4   
5   import org.apache.commons.lang3.StringUtils;
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   import org.springframework.boot.CommandLineRunner;
9   import org.springframework.cache.CacheManager;
10  import org.springframework.data.redis.cache.CacheStatistics;
11  import org.springframework.data.redis.cache.RedisCache;
12  import org.springframework.stereotype.Component;
13  
14  @Component
15  public class AppRunner implements CommandLineRunner {
16  
17  	private static final Logger log = LoggerFactory.getLogger(AppRunner.class);
18  
19  	private final RedisCache cache;
20  
21  	private final BookRepository bookRepository;
22  
23  	public AppRunner(CacheManager cacheManager, BookRepository bookRepository) {
24  		this.bookRepository = bookRepository;
25  
26  		this.cache = (RedisCache) cacheManager.getCache("books");
27  
28  		if (this.cache == null) {
29  			throw new IllegalArgumentException("Cache books should not be null.");
30  		}
31  
32  		log.info("Cache type: {}", this.cache.getClass());
33  		log.info("Native cache: {}", this.cache.getNativeCache());
34  	}
35  
36  	@Override
37  	public void run(String... args) throws Exception {
38  		// Clear cache before test.
39  		this.clearCache();
40  		this.stat();
41  
42  		String isbn = "isbn-1234";
43  
44  		this.saveBook(isbn);
45  		this.stat();
46  
47  		this.fetch(isbn);
48  		this.stat();
49  
50  		this.clearCache();
51  		this.stat();
52  
53  		this.fetch(isbn);
54  		this.stat();
55  	}
56  
57  	private void saveBook(String isbn) {
58  		this.sep(">");
59  		log.info("Saving book...");
60  		Book book = bookRepository.save(new Book(isbn, "version 0"));
61  		log.info("Book saved: {}.", book);
62  
63  		for (int i = 1; i <= 3; i++) {
64  			log.info("Updating book...");
65  			book.setTitle("version " + i);
66  			book = bookRepository.save(book);
67  			log.info("Book updated: {}.", book);
68  		}
69  		this.sep("<");
70  	}
71  
72  	private void fetch(String isbn) {
73  		this.sep(">");
74  		log.info("Fetching books...");
75  		for (int i = 0; i < 3; i++) {
76  			long startTime = System.nanoTime();
77  
78  			log.info("Getting book...");
79  			Book book = bookRepository.getByIsbn(isbn);
80  			log.info("{} -->{}", isbn, book);
81  
82  			long elapsedNanos = System.nanoTime() - startTime;
83  
84  			log.info("Elapsed: {}", Duration.ofNanos(elapsedNanos));
85  		}
86  		this.sep("<");
87  	}
88  
89  	private void stat() {
90  		this.sep(">");
91  		CacheStatistics cacheStatistics = cache.getStatistics();
92  		log.info("Hits: {}.", cacheStatistics.getHits());
93  		log.info("Puts: {}.", cacheStatistics.getPuts());
94  		this.sep("<");
95  	}
96  
97  	private void clearCache() {
98  		log.info("Clearing cache...");
99  		cache.clear();
100 		log.info("Cache cleared.");
101 	}
102 
103 	private void sep(String s) {
104 		log.info("{}", StringUtils.repeat(s, 80));
105 	}
106 
107 }