/** * See {@link #testCacheEntryFactoryReturningElementMake} this test ensures the Refresh * functionality works */ @Test public void testCacheEntryFactoryReturningElementRefresh() throws Exception { final long specialVersionNumber = 54321L; final CacheEntryFactory elementReturningFactory = new CacheEntryFactory() { public Object createEntry(final Object key) throws Exception { Element e = new Element(key, "V_" + key); e.setVersion(specialVersionNumber); return e; } }; selfPopulatingCache = new SelfPopulatingCache(cache, elementReturningFactory); Element e = null; e = selfPopulatingCache.get("key1"); assertEquals("V_key1", e.getValue()); assertEquals(specialVersionNumber, e.getVersion()); e = selfPopulatingCache.get("key2"); assertEquals("V_key2", e.getValue()); assertEquals(specialVersionNumber, e.getVersion()); assertEquals(2, selfPopulatingCache.getSize()); selfPopulatingCache.refresh(); e = selfPopulatingCache.get("key1"); assertEquals("V_key1", e.getValue()); assertEquals(specialVersionNumber, e.getVersion()); e = selfPopulatingCache.get("key2"); assertEquals("V_key2", e.getValue()); assertEquals(specialVersionNumber, e.getVersion()); assertEquals(2, selfPopulatingCache.getSize()); }
@RequestMapping("{cacheName}/{key}/details") @ResponseBody public Object keyDetail( @PathVariable("cacheName") String cacheName, @PathVariable("key") String key, Model model) { Element element = cacheManager.getCache(cacheName).get(key); String dataPattern = "yyyy-MM-dd hh:mm:ss"; Map<String, Object> data = Maps.newHashMap(); data.put("objectValue", element.getObjectValue().toString()); data.put("size", PrettyMemoryUtils.prettyByteSize(element.getSerializedSize())); data.put("hitCount", element.getHitCount()); Date latestOfCreationAndUpdateTime = new Date(element.getLatestOfCreationAndUpdateTime()); data.put( "latestOfCreationAndUpdateTime", DateFormatUtils.format(latestOfCreationAndUpdateTime, dataPattern)); Date lastAccessTime = new Date(element.getLastAccessTime()); data.put("lastAccessTime", DateFormatUtils.format(lastAccessTime, dataPattern)); if (element.getExpirationTime() == Long.MAX_VALUE) { data.put("expirationTime", "不过期"); } else { Date expirationTime = new Date(element.getExpirationTime()); data.put("expirationTime", DateFormatUtils.format(expirationTime, dataPattern)); } data.put("timeToIdle", element.getTimeToIdle()); data.put("timeToLive", element.getTimeToLive()); data.put("version", element.getVersion()); return data; }
@Test public void testVersioningCanRevertToOldBehavior() { System.setProperty("net.sf.ehcache.element.version.auto", "true"); try { CacheManager cacheManager = CacheManager.getInstance(); cacheManager.addCache( new Cache( "mltest", 50, MemoryStoreEvictionPolicy.LRU, true, null, true, 0, 0, false, 120, null, null, 0, 2, false)); Cache cache = cacheManager.getCache("mltest"); Element a = new Element("a key", "a value", 1L); cache.put(a); Element aAfter = cache.get("a key"); assertEquals(aAfter.getLastUpdateTime(), aAfter.getVersion()); Element b = new Element("a key", "a value"); cache.put(b); Element bAfter = cache.get("a key"); assertEquals(bAfter.getLastUpdateTime(), bAfter.getVersion()); Element c = new Element("a key", "a value", 3L); cache.put(c); Element cAfter = cache.get("a key"); assertEquals(cAfter.getLastUpdateTime(), cAfter.getVersion()); } finally { System.getProperties().remove("net.sf.ehcache.element.version.auto"); } }
/** * Create a new write operation for a particular element and creation time * * @param element the element to write * @param creationTime the creation time of the operation */ public WriteOperation(Element element, long creationTime) { this.element = new Element( element.getObjectKey(), element.getObjectValue(), element.getVersion(), element.getCreationTime(), element.getLastAccessTime(), element.getHitCount(), false, element.getTimeToLive(), element.getTimeToIdle(), element.getLastUpdateTime()); this.creationTime = creationTime; }