spring-boot-starter-data-redis Cacheable(sync=true) sees not work?

别说谁变了你拦得住时间么 提交于 2020-01-02 12:04:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!