public synchronized long incr(String key, int by) { Element e = ehCache.get(key); if (e == null) { return -1; } long newValue = ((Number) e.getObjectValue()).longValue() + by; Element newE = new Element(key, newValue); newE.setTimeToLive(e.getTimeToLive()); ehCache.put(newE); return newValue; }
@Override public Boolean removeURLHandlerFromCache(String mapKey) { Boolean success = Boolean.FALSE; if (mapKey != null) { Element e = getUrlHandlerCache().get(mapKey); if (e != null && e.getObjectValue() != null) { success = Boolean.valueOf(getUrlHandlerCache().remove(mapKey)); } } return success; }
public void add(String key, Object value, int expiration) { if (ehCache.get(key) != null) { return; } Element element = new Element(key, value); element.setTimeToLive(expiration); ehCache.put(element); }
private void clearCacheKey(String cacheId, String cacheKey) { final List<CacheManager> allCacheManagers = CacheManager.ALL_CACHE_MANAGERS; for (final CacheManager cacheManager : allCacheManagers) { final Cache cache = cacheManager.getCache(cacheId); if (cache != null) { final boolean removed = cache.remove(cacheKey); if (!removed) { // if keys are not Strings, we have to find the initial key for (final Object key : cache.getKeys()) { if (key != null && key.toString().equals(cacheKey)) { cache.remove(key); break; } } } } } }
Element element = get(key); if (element != null) { return element; element = getQuiet(key); if (element != null) { return element; long cacheLoaderTimeoutMillis = configuration.getCacheLoaderTimeoutMillis(); final Object value; if (cacheLoaderTimeoutMillis > 0) { final Future<AtomicReference<Object>> future = asynchronousLoad(key, loader, loaderArgument); value = future.get(cacheLoaderTimeoutMillis, TimeUnit.MILLISECONDS).get(); } else { value = loadValueUsingLoader(key, loader, loaderArgument); return getQuiet(key); } else { Element newElement = new Element(key, value); put(newElement, false); Element fromCache = getQuiet(key); if (fromCache == null) { return newElement; throw new LoaderTimeoutException("Timeout on load for key " + key, e); } catch (Exception e) { throw new CacheException("Exception on load for key " + key, e);
System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M"); new Cache(new CacheConfiguration("test", 0).// memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).// eternal(false).// timeToIdleSeconds(86400).// testCache.put(new Element("1", blob)); System.out.println(testCache.get("1") == null); System.out.println(testCache.getSize()); System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes()); System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M"); testCache.put(new Element("2", blob)); System.out.println(testCache.get("1") == null); System.out.println(testCache.get("2") == null); System.out.println(testCache.getSize()); System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes()); System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M"); testCache.put(new Element("3", blob)); System.out.println(testCache.get("1") == null); System.out.println(testCache.get("2") == null); System.out.println(testCache.get("3") == null); System.out.println(testCache.getSize()); System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes()); System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");
/** * ehcache不支持putIfAbsent原子操作 */ @Override public synchronized Object putIfAbsent(Object key, Object value) { Cache cache = manager.getCache(ehcacheConf.getPredefinedCacheName()); if (!cache.isKeyInCache(key)){ Element element = new Element(key, value); cache.put(element); } Element e = (Element)cache.get(key); return e.getObjectValue(); }
/** * Calls the CacheLoader and puts the result in the Cache */ public void run() throws CacheException { try { //Test to see if it has turned up in the meantime boolean existsOnRun = isKeyInCache(key); if (!existsOnRun) { Object value = loadValueUsingLoader(key, specificLoader, argument); if (value != null) { put(new Element(key, value), false); } } } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Problem during load. Load will not be completed. Cause was " + e.getCause(), e); } throw new CacheException("Problem during load. Load will not be completed. Cause was " + e.getCause(), e); } } });
public <T> T getCache(String group, String key) { try { createIfMissing(group); Cache c = cacheManager.getCache(group); return (T) (c.get(key) == null ? null : c.get(key).getObjectValue()); } catch (Exception e) { logger.warn("%s", e, e); return null; } }
public static void main(String[] args) throws CacheException, FileNotFoundException { CacheManager mgr = CacheManager.newInstance(); System.out.println(mgr.getConfiguration().getCacheConfigurations().toString()); Cache cache = mgr.getCache("sampleCache1"); cache.put(new Element("Frank", "Groer Junge!")); System.out.println(cache.get("Frank")); mgr.shutdown(); } }
@Bean public Cache cache() { CacheManager cacheManager = cacheManager(); Cache apiKeyCache = cacheManager.getCache(CACHE_NAME); if (apiKeyCache == null) { LOGGER.warn("EHCache cache for " + CACHE_NAME + " not found. Fallback to default EHCache configuration"); CacheConfiguration cacheConfiguration = new CacheConfiguration(CACHE_NAME, 1000); cacheManager.addCache(new Cache(cacheConfiguration)); } return cacheManager.getCache(CACHE_NAME); }
private static Ehcache createCacheIfRequired(String cacheName) { final CacheManager instance = CacheManager.newInstance(new Configuration().name(cacheName)); synchronized (instance) { if (!instance.cacheExists(cacheName)) { instance.addCache(new net.sf.ehcache.Cache(cacheConfiguration(cacheName))); } return instance.getCache(cacheName); } }
Element element = get(key); map.put(key, element.getObjectValue()); Future future = asynchronousLoadAll(missingKeys, loaderArgument); long cacheLoaderTimeoutMillis = configuration.getCacheLoaderTimeoutMillis(); if (cacheLoaderTimeoutMillis > 0) { try { Element element = get(key); if (element != null) { map.put(key, element.getObjectValue()); } else { map.put(key, null); throw new CacheException(e.getMessage() + " for key " + key, e); } catch (ExecutionException e) { throw new CacheException(e.getMessage() + " for key " + key, e); Element element = get(key); if (element != null) { map.put(key, element.getObjectValue()); } else { map.put(key, null);
public Object get(String key) { Element e = ehCache.get(key); return (e == null) ? null : e.getObjectValue(); }
private synchronized Cache getHeap() { if (heap == null) { if (CacheManager.getInstance().cacheExists("hydrated-cache")) { heap = CacheManager.getInstance().getCache("hydrated-cache"); } else { CacheConfiguration config = new CacheConfiguration("hydrated-cache", 0).eternal(true).overflowToDisk(false).maxElementsInMemory(100000); Cache cache = new Cache(config); CacheManager.create().addCache(cache); heap = cache; } } return heap; }
private synchronized Cache getHeap() { if (offHeap == null) { if (CacheManager.getInstance().cacheExists("hydrated-offheap-cache")) { offHeap = CacheManager.getInstance().getCache("hydrated-offheap-cache"); } else { CacheConfiguration config = new CacheConfiguration("hydrated-offheap-cache", 500).eternal(true).overflowToOffHeap(true).maxMemoryOffHeap("1400M"); Cache cache = new Cache(config); CacheManager.create().addCache(cache); offHeap = cache; } } return offHeap; }
private void initSupportCache() { this.supportConfig = new CacheConfiguration(); supportConfig.name(underlyingCache.getName() + "_" + getClass().getName() + "_refreshAheadSupport"); supportConfig = supportConfig.persistence(new PersistenceConfiguration().strategy(Strategy.NONE)); int activeSize = 2 * refreshAheadConfig.getBatchSize() * refreshAheadConfig.getNumberOfThreads(); supportConfig = supportConfig.maxEntriesLocalHeap(activeSize); if (underlyingCache.getCacheConfiguration().isTerracottaClustered()) { supportConfig = supportConfig.persistence(new PersistenceConfiguration().strategy(Strategy.DISTRIBUTED)); this.supportCache = new Cache(supportConfig); Ehcache prior = underlyingCache.getCacheManager().addCacheIfAbsent(supportCache); if (prior != supportCache) { throw new IllegalStateException("Unable to add refresh ahead support cache due to name collision: "