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