Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. public Index getIndex() {
  2. return calculateIndex();
  3. }
  4.  
  5. public Index calculateIndex() {
  6. // 1 second or more
  7. }
  8.  
  9. public boolean isIndexActual(Index index) {
  10. // 20ms or less
  11. }
  12.  
  13. @Cacheable(cacheNames = CacheConfiguration.INDEX_CACHE_NAME)
  14. public Index getIndex() {
  15. return calculateIndex();
  16. }
  17.  
  18. @Bean
  19. public Cache indexCache() {
  20. return new GuavaCache(INDEX_CACHE_NAME, CacheBuilder.newBuilder()
  21. .expireAfterWrite(indexCacheExpireAfterWriteSeconds, TimeUnit.SECONDS)
  22. .build());
  23. }
  24.  
  25. @Bean
  26. public CacheManager indexCacheManager(List<Cache> caches) {
  27. SimpleCacheManager cacheManager = new SimpleCacheManager();
  28. cacheManager.setCaches(caches);
  29. return cacheManager;
  30. }
  31.  
  32. @Cacheable(cacheNames = INDEX_CACHE_NAME)
  33. @CacheEvict(cacheNames = INDEX_CACHE_NAME, condition = "target.isObsolete(#result)")
  34. public Index getIndex() {
  35. return calculateIndex();
  36. }
  37.  
  38. public static class Index {
  39.  
  40. private final long timestamp;
  41.  
  42. public Index(long timestamp) {
  43. this.timestamp = timestamp;
  44. }
  45.  
  46. public long getTimestamp() {
  47. return timestamp;
  48. }
  49. }
  50.  
  51. public interface IndexCalculator {
  52. public Index calculateIndex();
  53.  
  54. public long getCurrentTimestamp();
  55. }
  56.  
  57. @Service
  58. public static class IndexService {
  59. @Autowired
  60. private IndexCalculator indexCalculator;
  61.  
  62. @Cacheable(cacheNames = "index")
  63. @CacheEvict(cacheNames = "index", condition = "target.isObsolete(#result)")
  64. public Index getIndex() {
  65. return indexCalculator.calculateIndex();
  66. }
  67.  
  68. public boolean isObsolete(Index index) {
  69. long indexTimestamp = index.getTimestamp();
  70. long currentTimestamp = indexCalculator.getCurrentTimestamp();
  71. if (index == null || indexTimestamp < currentTimestamp) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. }
  78.  
  79. @Test
  80. public void test() {
  81. final Index index100 = new Index(100);
  82. final Index index200 = new Index(200);
  83.  
  84. when(indexCalculator.calculateIndex()).thenReturn(index100);
  85. when(indexCalculator.getCurrentTimestamp()).thenReturn(100L);
  86. assertThat(indexService.getIndex()).isSameAs(index100);
  87. verify(indexCalculator).calculateIndex();
  88. verify(indexCalculator).getCurrentTimestamp();
  89.  
  90. when(indexCalculator.getCurrentTimestamp()).thenReturn(200L);
  91. when(indexCalculator.calculateIndex()).thenReturn(index200);
  92. assertThat(indexService.getIndex()).isSameAs(index100);
  93. verify(indexCalculator, times(2)).getCurrentTimestamp();
  94. // I'd like to see indexCalculator.calculateIndex() called after
  95. // indexService.getIndex() returns the old value but it does not happen
  96. // verify(indexCalculator, times(2)).calculateIndex();
  97.  
  98.  
  99. assertThat(indexService.getIndex()).isSameAs(index200);
  100. // Instead, indexCalculator.calculateIndex() os called on
  101. // the next call to indexService.getIndex()
  102. // I'd like to have it earlier
  103. verify(indexCalculator, times(2)).calculateIndex();
  104. verify(indexCalculator, times(3)).getCurrentTimestamp();
  105. verifyNoMoreInteractions(indexCalculator);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement