1 package org.oxerr.spring.cache.redis.scored.example.spring.data.redis.cache;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.springframework.cache.annotation.CacheEvict;
7 import org.springframework.cache.annotation.Cacheable;
8 import org.springframework.stereotype.Component;
9
10 @Component
11 public class SimpleBookRepository implements BookRepository {
12
13 private final Map<String, Book> books = new HashMap<>();
14
15 @Override
16 @Cacheable("books")
17 public Book getByIsbn(String isbn) {
18 Book book = this.books.get(isbn);
19
20 simulateSlowService();
21
22 return book;
23 }
24
25 @Override
26 @CacheEvict("books")
27 public Book saveBook(Book book) {
28 this.books.put(book.getIsbn(), book);
29 return book;
30 }
31
32
33 private void simulateSlowService() {
34 try {
35 long time = 3000L;
36 Thread.sleep(time);
37 } catch (InterruptedException e) {
38 Thread.currentThread().interrupt();
39 throw new IllegalStateException(e);
40 }
41 }
42
43 }