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; } }
@Override @SuppressWarnings("unchecked") public List<SearchFacetDTO> getSearchFacets() { List<SearchFacetDTO> facets = null; String cacheKey = CACHE_KEY_PREFIX + "blc-search"; Element element = cache.get(cacheKey); if (element != null) { facets = (List<SearchFacetDTO>) element.getValue(); } if (facets == null) { facets = buildSearchFacetDtos(searchFacetDao.readAllSearchFacets(FieldEntity.PRODUCT)); element = new Element(cacheKey, facets); cache.put(element); } return facets; }
public void set(String key, Object value, int expiration) { Element element = new Element(key, value); element.setTimeToLive(expiration); ehCache.put(element); }
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; }
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); }
CacheManager cacheManager = CacheManager.create(conf); persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE))); cacheManager.addCache(testCache); testCache.put(new Element("1", blob)); System.out.println(testCache.get("1") == null); System.out.println(testCache.getSize()); System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes()); testCache.put(new Element("2", blob)); System.out.println(testCache.get("1") == null); System.out.println(testCache.get("2") == null); blob[i] = (byte) random.nextInt(); testCache.put(new Element("3", blob)); System.out.println(testCache.get("1") == null); System.out.println(testCache.get("2") == null); System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M"); cacheManager.shutdown();
/** * 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(); }
public <T> T get(String key, Supplier<T> compute) { Element element = ehcache.get(key); if (element != null) { return (T) element.getObjectValue(); } synchronized (key.intern()) { element = ehcache.get(key); if (element != null) { return (T) element.getObjectValue(); } T object = compute.get(); ehcache.put(new Element(key, object)); return object; } }
@Override public void putIntoCache(Object key, Object value, SharedSessionContractImplementor session) { try { final Element element = new Element( key, value ); getCache().put( element ); } catch (IllegalArgumentException | IllegalStateException e) { throw new CacheException( e ); } catch (net.sf.ehcache.CacheException e) { if ( e instanceof NonStopCacheException ) { HibernateNonstopCacheExceptionHandler.getInstance() .handleNonstopCacheException( (NonStopCacheException) e ); } else { throw new CacheException( e ); } } }
/** * 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; }
/** * {@inheritDoc} */ public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException { try { if (minimalPutOverride && ehcache.get(key) != null) { return false; } //OptimisticCache? versioning? ehcache.put(new Element(key, value)); return true; } catch (net.sf.ehcache.CacheException e) { throw new CacheException(e); } }
/** * {@inheritDoc} */ public Element putIfAbsent(Element element) throws NullPointerException { if (element == null) { return null; } if (searchManager != null) { searchManager.put(cache.getName(), -1, element, null, attributeExtractors, cache.getCacheConfiguration().getDynamicExtractor()); } long delta = poolAccessor.add(element.getObjectKey(), element.getObjectValue(), map.storedObject(element), storePinned); if (delta > -1) { Element old = map.putIfAbsent(element.getObjectKey(), element, delta); if (old == null) { checkCapacity(element); } else { poolAccessor.delete(delta); } return old; } else { notifyDirectEviction(element); return null; } }
public Object get(String key) { Element e = ehCache.get(key); return (e == null) ? null : e.getObjectValue(); }
@Override public Object getFromCache(Object key, SharedSessionContractImplementor session) { try { final Element element = getCache().get( key ); if ( element == null ) { return null; } else { return element.getObjectValue(); } } catch (net.sf.ehcache.CacheException e) { if ( e instanceof NonStopCacheException ) { HibernateNonstopCacheExceptionHandler.getInstance() .handleNonstopCacheException( (NonStopCacheException) e ); return null; } else { throw new CacheException( e ); } } }
@Override public synchronized long incr(String key, int by) { Element e = cache.get(key); if (e == null) { return -1; } long newValue = ((Number) e.getValue()).longValue() + by; Element newE = new Element(key, newValue); newE.setTimeToLive(e.getTimeToLive()); cache.put(newE); return newValue; }
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 { return getQuiet(key); } else { Element newElement = new Element(key, value); put(newElement, false); Element fromCache = getQuiet(key);