@Override protected Rows<RowKeyType, String> getFallback() { return this.fallbackCache.fetchQuery(); }
/** * 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; }
@Before public void before() { MockitoAnnotations.initMocks(this); cacheMap = new ConcurrentHashMap<String, HashMap<String, String>>(); cache = new DefaultCassandraCache<HashMap<String, String>>(cacheMap); cache = spy(cache); }
@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 */ 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); }
fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), Arrays.toString(rowKeys), String.valueOf(startKey), String.valueOf(endKey), startToken, endToken, String.valueOf(maxRows), Arrays.toString(columns)); } else { fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), Arrays.toString(rowKeys), String.valueOf(startKey), String.valueOf(endKey), startToken, endToken, String.valueOf(maxRows));
if (zuulCassKeyspace != null) return zuulCassKeyspace; try { setAstynaxConfiguration(ConfigurationManager.getConfigInstance());
@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); }
@Override protected Rows<RowKeyType, String> getFallback() { return (this.fallbackCache != null) ? this.fallbackCache.fetchQuery() : null; }
fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), Arrays.toString(rowKeys), String.valueOf(startKey), String.valueOf(endKey), startToken, endToken, String.valueOf(maxRows), Arrays.toString(columns)); } else { fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), Arrays.toString(rowKeys), String.valueOf(startKey), String.valueOf(endKey), startToken, endToken, String.valueOf(maxRows));
/** * 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; }
@Before public void before() { MockitoAnnotations.initMocks(this); cacheMap = new ConcurrentHashMap<String, HashMap<String, String>>(); cache = new DefaultCassandraCache<HashMap<String, String>>(cacheMap); cache = spy(cache); }
if (zuulCassKeyspace != null) return zuulCassKeyspace; try { setAstynaxConfiguration(ConfigurationManager.getConfigInstance());
@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()); }
@Override protected Rows<RowKeyType, String> getFallback() { return (this.fallbackCache != null) ? this.fallbackCache.fetchQuery() : null; }
@Override protected Rows<RowKeyType, String> run() throws Exception { try { CqlResult<RowKeyType, String> cqlresult = keyspace.prepareQuery(columnFamily).withCql(cql).execute().getResult(); Rows<RowKeyType, String> result = cqlresult.getRows(); if (fallbackCache != null) { try { /* store the response in the cache for fallback if we have a cache */ fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), cql); } catch (Exception e) { // don't blow up on cache population since this is non-essential } } return result; } catch (ConnectionException e) { throw e; } }
@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()); }
@Override protected Rows<RowKeyType, String> getFallback() { return this.fallbackCache.fetchQuery(); }
@Override protected Rows<RowKeyType, String> run() throws Exception { try { CqlResult<RowKeyType, String> cqlresult = keyspace.prepareQuery(columnFamily).withCql(cql).execute().getResult(); Rows<RowKeyType, String> result = cqlresult.getRows(); if (fallbackCache != null) { try { /* store the response in the cache for fallback if we have a cache */ fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), cql); } catch (Exception e) { // don't blow up on cache population since this is non-essential } } return result; } catch (ConnectionException e) { throw e; } }