i want to use Cacheable(sync=true) to controll concurrent behaviors to access a method
@Service
@CacheConfig(cacheNames = "book")
public class BookService {
@Cacheable(key = "#isbn",sync = true)
public Book book(String isbn,int id) {
System.out.println("wait 3s...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Book(isbn, "xxxx");
}
}
write a test case:
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; i++) {
final int index = i;
executorService.execute(new Runnable() {
@Override
public void run() {
bookService.book("1100011", index);
}
});
}
all keys are the same,so sync method may works, but i get :
wait 3s...
wait 3s...
wait 3s...
wait 3s...
wait 3s...
is it a wrong way to use redis cache like this?
来源:https://stackoverflow.com/questions/47052312/spring-boot-starter-data-redis-cacheablesync-true-sees-not-work