Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Index getIndex() {
- return calculateIndex();
- }
- public Index calculateIndex() {
- // 1 second or more
- }
- public boolean isIndexActual(Index index) {
- // 20ms or less
- }
- @Cacheable(cacheNames = CacheConfiguration.INDEX_CACHE_NAME)
- public Index getIndex() {
- return calculateIndex();
- }
- @Bean
- public Cache indexCache() {
- return new GuavaCache(INDEX_CACHE_NAME, CacheBuilder.newBuilder()
- .expireAfterWrite(indexCacheExpireAfterWriteSeconds, TimeUnit.SECONDS)
- .build());
- }
- @Bean
- public CacheManager indexCacheManager(List<Cache> caches) {
- SimpleCacheManager cacheManager = new SimpleCacheManager();
- cacheManager.setCaches(caches);
- return cacheManager;
- }
- @Cacheable(cacheNames = INDEX_CACHE_NAME)
- @CacheEvict(cacheNames = INDEX_CACHE_NAME, condition = "target.isObsolete(#result)")
- public Index getIndex() {
- return calculateIndex();
- }
- public static class Index {
- private final long timestamp;
- public Index(long timestamp) {
- this.timestamp = timestamp;
- }
- public long getTimestamp() {
- return timestamp;
- }
- }
- public interface IndexCalculator {
- public Index calculateIndex();
- public long getCurrentTimestamp();
- }
- @Service
- public static class IndexService {
- @Autowired
- private IndexCalculator indexCalculator;
- @Cacheable(cacheNames = "index")
- @CacheEvict(cacheNames = "index", condition = "target.isObsolete(#result)")
- public Index getIndex() {
- return indexCalculator.calculateIndex();
- }
- public boolean isObsolete(Index index) {
- long indexTimestamp = index.getTimestamp();
- long currentTimestamp = indexCalculator.getCurrentTimestamp();
- if (index == null || indexTimestamp < currentTimestamp) {
- return true;
- } else {
- return false;
- }
- }
- }
- @Test
- public void test() {
- final Index index100 = new Index(100);
- final Index index200 = new Index(200);
- when(indexCalculator.calculateIndex()).thenReturn(index100);
- when(indexCalculator.getCurrentTimestamp()).thenReturn(100L);
- assertThat(indexService.getIndex()).isSameAs(index100);
- verify(indexCalculator).calculateIndex();
- verify(indexCalculator).getCurrentTimestamp();
- when(indexCalculator.getCurrentTimestamp()).thenReturn(200L);
- when(indexCalculator.calculateIndex()).thenReturn(index200);
- assertThat(indexService.getIndex()).isSameAs(index100);
- verify(indexCalculator, times(2)).getCurrentTimestamp();
- // I'd like to see indexCalculator.calculateIndex() called after
- // indexService.getIndex() returns the old value but it does not happen
- // verify(indexCalculator, times(2)).calculateIndex();
- assertThat(indexService.getIndex()).isSameAs(index200);
- // Instead, indexCalculator.calculateIndex() os called on
- // the next call to indexService.getIndex()
- // I'd like to have it earlier
- verify(indexCalculator, times(2)).calculateIndex();
- verify(indexCalculator, times(3)).getCurrentTimestamp();
- verifyNoMoreInteractions(indexCalculator);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement