@Test public void testSelfPopulatingCacheUpdates() throws Exception { UpdatingCacheEntryFactory cacheEntryFactory = createMock(UpdatingCacheEntryFactory.class); StringBuilder value = new StringBuilder("b"); expect(cacheEntryFactory.createEntry("a")).andReturn(value); cacheEntryFactory.updateEntryValue("a", value); expectLastCall().andAnswer(new IAnswer<Object>() { @Override public Object answer() throws Throwable { String key = (String) getCurrentArguments()[0]; StringBuilder value = (StringBuilder) getCurrentArguments()[1]; System.out.println("key = " + key + ", value = " + value); value.append("c"); return null; } }); replay(cacheEntryFactory); net.sf.ehcache.config.Configuration managerConfig = new net.sf.ehcache.config.Configuration(); managerConfig.setMaxBytesLocalHeap("10%"); CacheManager manager = CacheManager.create(managerConfig); Cache cache = new Cache("test", 0, false, false, 10000, 10000); UpdatingSelfPopulatingCache testCache = new UpdatingSelfPopulatingCache(cache, cacheEntryFactory); manager.addCache(testCache); Assert.assertEquals("b", testCache.get("a").getObjectValue().toString()); Assert.assertEquals("bc", testCache.get("a").getObjectValue().toString()); verify(cacheEntryFactory); }
@Override public Element get(Object key) throws LockTimeoutException { Element element = this.getQuiet(key); if (element != null) { if (LOG.isTraceEnabled()) { LOG.trace("key : " + element.getObjectKey()); LOG.trace("value : " + element.getObjectValue()); } TimelineAppMetricCacheKey existingKey = (TimelineAppMetricCacheKey) element.getObjectKey(); LOG.debug("Existing temporal info: " + existingKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); TimelineAppMetricCacheKey newKey = (TimelineAppMetricCacheKey) key; existingKey.setTemporalInfo(newKey.getTemporalInfo()); LOG.debug("New temporal info: " + newKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); if (existingKey.getSpec() == null || !existingKey.getSpec().equals(newKey.getSpec())) { existingKey.setSpec(newKey.getSpec()); LOG.debug("New spec: " + newKey.getSpec() + " for : " + existingKey.getMetricNames()); } } return super.get(key); }
TimelineMetricCache extends UpdatingSelfPopulatingCache { @Override public Element get(Object key) throws LockTimeoutException { Element element = this.getQuiet(key); if (element != null) { if (LOG.isTraceEnabled()) { LOG.trace("key : " + element.getObjectKey()); LOG.trace("value : " + element.getObjectValue()); } TimelineAppMetricCacheKey existingKey = (TimelineAppMetricCacheKey) element.getObjectKey(); LOG.debug("Existing temporal info: " + existingKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); TimelineAppMetricCacheKey newKey = (TimelineAppMetricCacheKey) key; existingKey.setTemporalInfo(newKey.getTemporalInfo()); LOG.debug("New temporal info: " + newKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); if (existingKey.getSpec() == null || !existingKey.getSpec().equals(newKey.getSpec())) { existingKey.setSpec(newKey.getSpec()); LOG.debug("New spec: " + newKey.getSpec() + " for : " + existingKey.getMetricNames()); } } return super.get(key); } }
TimelineMetricCache extends UpdatingSelfPopulatingCache { @Override public Element get(Object key) throws LockTimeoutException { Element element = this.getQuiet(key); if (element != null) { if (LOG.isTraceEnabled()) { LOG.trace("key : " + element.getObjectKey()); LOG.trace("value : " + element.getObjectValue()); } TimelineAppMetricCacheKey existingKey = (TimelineAppMetricCacheKey) element.getObjectKey(); LOG.debug("Existing temporal info: " + existingKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); TimelineAppMetricCacheKey newKey = (TimelineAppMetricCacheKey) key; existingKey.setTemporalInfo(newKey.getTemporalInfo()); LOG.debug("New temporal info: " + newKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); if (existingKey.getSpec() == null || !existingKey.getSpec().equals(newKey.getSpec())) { existingKey.setSpec(newKey.getSpec()); LOG.debug("New spec: " + newKey.getSpec() + " for : " + existingKey.getMetricNames()); } } return super.get(key); } TimelineMetricCache(Ehcache cache, UpdatingCacheEntryFactory factory); }
TimelineMetricCache extends UpdatingSelfPopulatingCache { @Override public Element get(Object key) throws LockTimeoutException { Element element = this.getQuiet(key); if (element != null) { if (LOG.isTraceEnabled()) { LOG.trace("key : " + element.getObjectKey()); LOG.trace("value : " + element.getObjectValue()); } TimelineAppMetricCacheKey existingKey = (TimelineAppMetricCacheKey) element.getObjectKey(); LOG.debug("Existing temporal info: " + existingKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); TimelineAppMetricCacheKey newKey = (TimelineAppMetricCacheKey) key; existingKey.setTemporalInfo(newKey.getTemporalInfo()); LOG.debug("New temporal info: " + newKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); if (existingKey.getSpec() == null || !existingKey.getSpec().equals(newKey.getSpec())) { existingKey.setSpec(newKey.getSpec()); LOG.debug("New spec: " + newKey.getSpec() + " for : " + existingKey.getMetricNames()); } } return super.get(key); } TimelineMetricCache(Ehcache cache, UpdatingCacheEntryFactory factory); TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key); @Override Element get(Object key); }
TimelineMetricCache extends UpdatingSelfPopulatingCache { @Override public Element get(Object key) throws LockTimeoutException { Element element = this.getQuiet(key); if (element != null) { if (LOG.isTraceEnabled()) { LOG.trace("key : " + element.getObjectKey()); LOG.trace("value : " + element.getObjectValue()); } TimelineAppMetricCacheKey existingKey = (TimelineAppMetricCacheKey) element.getObjectKey(); LOG.debug("Existing temporal info: " + existingKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); TimelineAppMetricCacheKey newKey = (TimelineAppMetricCacheKey) key; existingKey.setTemporalInfo(newKey.getTemporalInfo()); LOG.debug("New temporal info: " + newKey.getTemporalInfo() + " for : " + existingKey.getMetricNames()); if (existingKey.getSpec() == null || !existingKey.getSpec().equals(newKey.getSpec())) { existingKey.setSpec(newKey.getSpec()); LOG.debug("New spec: " + newKey.getSpec() + " for : " + existingKey.getMetricNames()); } } return super.get(key); } TimelineMetricCache(Ehcache cache, UpdatingCacheEntryFactory factory); TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key); @Override Element get(Object key);  }