@Before public void before() { MockitoAnnotations.initMocks(this); cacheMap = new ConcurrentHashMap<String, HashMap<String, String>>(); cache = new DefaultCassandraCache<HashMap<String, String>>(cacheMap); cache = spy(cache); }
/** * Returns cached query by keys * * @param keys * @return */ public K fetchQuery(String... keys) { final String key = buildKey(keys); final K result = cacheMap.get(key); if (result != null) this.cacheHits.incrementAndGet(); else this.cacheMisses.incrementAndGet(); final long currentHits = this.cacheHits.get(); this.cacheHitRatio = (float) currentHits / (currentHits + this.cacheMisses.get()); return result; }
@Test public void storesItemIfCacheIsNotFull() { when(cache.getMaxCacheSize()).thenReturn(5); final String cacheKey1 = buildKey(DOMAIN, QUERY1); final String cacheKey2 = buildKey(DOMAIN, QUERY2); cache.storeQuery(response1, DOMAIN, QUERY1); cache.storeQuery(response2, DOMAIN, QUERY2); assertSame(response1, cache.fetchQuery(DOMAIN, QUERY1)); assertSame(response1, cacheMap.get(cacheKey1)); assertSame(response2, cache.fetchQuery(DOMAIN, QUERY2)); assertSame(response2, cacheMap.get(cacheKey2)); }
/** * stores a given response by keys * * @param response * @param keys */ @Override public void storeQuery(K response, String... keys) { final String key = buildKey(keys); if (!this.cacheMap.containsKey(key)) { // checks if inserting this item will increase cache over desired size final int removeCount = (this.cacheMap.size() + 1) - this.getMaxCacheSize(); if (removeCount > 0) { // I am simply going to remove as many items as necessary in the order that the iterator returns them. // in practice it will usually only need to remove one item, but if we updated the fast property // to half the cache size there would be a lot more final Iterator<String> it = this.cacheMap.keySet().iterator(); for (int i = 0; i < removeCount; i++) { if (it.hasNext()) { it.next(); it.remove(); } } } } this.cacheMap.put(key, response); }
/** * stores a given response by keys * * @param response * @param keys */ public void storeQuery(K response, String... keys) { final String key = buildKey(keys); if (!this.cacheMap.containsKey(key)) { // checks if inserting this item will increase cache over desired size final int removeCount = (this.cacheMap.size() + 1) - this.getMaxCacheSize(); if (removeCount > 0) { // I am simply going to remove as many items as necessary in the order that the iterator returns them. // in practice it will usually only need to remove one item, but if we updated the fast property // to half the cache size there would be a lot more final Iterator<String> it = this.cacheMap.keySet().iterator(); for (int i = 0; i < removeCount; i++) { if (it.hasNext()) { it.next(); it.remove(); } } } } this.cacheMap.put(key, response); }
@Test public void storesItemIfCacheIsNotFull() { when(cache.getMaxCacheSize()).thenReturn(5); final String cacheKey1 = buildKey(DOMAIN, QUERY1); final String cacheKey2 = buildKey(DOMAIN, QUERY2); cache.storeQuery(response1, DOMAIN, QUERY1); cache.storeQuery(response2, DOMAIN, QUERY2); assertSame(response1, cache.fetchQuery(DOMAIN, QUERY1)); assertSame(response1, cacheMap.get(cacheKey1)); assertSame(response2, cache.fetchQuery(DOMAIN, QUERY2)); assertSame(response2, cacheMap.get(cacheKey2)); }
@Before public void before() { MockitoAnnotations.initMocks(this); cacheMap = new ConcurrentHashMap<String, HashMap<String, String>>(); cache = new DefaultCassandraCache<HashMap<String, String>>(cacheMap); cache = spy(cache); }
/** * Returns cached query by keys * * @param keys * @return */ @Override public K fetchQuery(String... keys) { final String key = buildKey(keys); final K result = cacheMap.get(key); if (result != null) this.cacheHits.incrementAndGet(); else this.cacheMisses.incrementAndGet(); final long currentHits = this.cacheHits.get(); this.cacheHitRatio = (float) currentHits / (currentHits + this.cacheMisses.get()); return result; }
@Test public void removesItemIfCacheIsFull() { when(cache.getMaxCacheSize()).thenReturn(1); final String cacheKey1 = buildKey(DOMAIN, QUERY1); final String cacheKey2 = buildKey(DOMAIN, QUERY2); cache.storeQuery(response1, DOMAIN, QUERY1); assertSame(response1, cache.fetchQuery(DOMAIN, QUERY1)); assertSame(response1, cacheMap.get(cacheKey1)); assertEquals(1, cacheMap.size()); cache.storeQuery(response2, DOMAIN, QUERY2); assertSame(response2, cache.fetchQuery(DOMAIN, QUERY2)); assertSame(response2, cacheMap.get(cacheKey2)); assertEquals(1, cacheMap.size()); }
@Test public void removesItemIfCacheIsFull() { when(cache.getMaxCacheSize()).thenReturn(1); final String cacheKey1 = buildKey(DOMAIN, QUERY1); final String cacheKey2 = buildKey(DOMAIN, QUERY2); cache.storeQuery(response1, DOMAIN, QUERY1); assertSame(response1, cache.fetchQuery(DOMAIN, QUERY1)); assertSame(response1, cacheMap.get(cacheKey1)); assertEquals(1, cacheMap.size()); cache.storeQuery(response2, DOMAIN, QUERY2); assertSame(response2, cache.fetchQuery(DOMAIN, QUERY2)); assertSame(response2, cacheMap.get(cacheKey2)); assertEquals(1, cacheMap.size()); }