spring-boot-starter-data-redis可缓存(sync = true)看不起作用?

时间:2017-11-01 10:02:18

标签: spring spring-boot spring-data-redis

我想使用Cacheable(sync = true)来控制访问方法的并发行为

@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");
  }

}

写一个测试用例:

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);
            }
        });
    }

所有键都相同,因此同步方法可能有效,但我得到:

wait 3s...
wait 3s...
wait 3s...
wait 3s...
wait 3s...

这样使用redis缓存是错误的方法吗?

0 个答案:

没有答案