public Object processRequest(ProceedingJoinPoint call) throws Throwable { CacheRequest cacheRequest = (CacheRequest) call.getArgs()[0]; Cache cache = ((ServiceResponseCacheable) call.getTarget()).getCache(); List<Serializable> cacheItemResponses = new ArrayList<Serializable>(); Iterator<CacheItemRequest> itr = cacheRequest.getCacheItemRequests().iterator(); while(itr.hasNext()) { CacheItemRequest itemRequest = itr.next(); if (cache.isKeyInCache(itemRequest.key())) { cacheItemResponses.add(cache.get(itemRequest.key()).getValue()); itr.remove(); } } CacheResponse returnValue = (CacheResponse) call.proceed(); Object[] responses = new Object[cacheItemResponses.size() + returnValue.getCacheItemResponses().length]; responses = cacheItemResponses.toArray(responses); for (int j=0; j<returnValue.getCacheItemResponses().length; j++) { Element element = new Element(cacheRequest.getCacheItemRequests().get(j).key(), returnValue.getCacheItemResponses()[j]); cache.put(element); } System.arraycopy(returnValue.getCacheItemResponses(), 0, responses, cacheItemResponses.size(), returnValue.getCacheItemResponses().length); returnValue.setCacheItemResponses(responses); return returnValue; }
public boolean contain(Object key) { Cache cache = manager.getCache(ehcacheConf.getPredefinedCacheName()); return cache.isKeyInCache(key); }
/** * 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) { result.set(value); } } } 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); } } }, result);
/** * The load method provides a means to "pre-load" the cache. This method will, asynchronously, load the specified * object into the cache using the associated CacheLoader. If the object already exists in the cache, no action is * taken. If no loader is associated with the object, no object will be loaded into the cache. If a problem is * encountered during the retrieving or loading of the object, an exception should be logged. If the "arg" argument * is set, the arg object will be passed to the CacheLoader.load method. The cache will not dereference the object. * If no "arg" value is provided a null will be passed to the load method. The storing of null values in the cache * is permitted, however, the get method will not distinguish returning a null stored in the cache and not finding * the object in the cache. In both cases a null is returned. * <p> * The Ehcache native API provides similar functionality to loaders using the * decorator {@link net.sf.ehcache.constructs.blocking.SelfPopulatingCache} * * @param key key whose associated value to be loaded using the associated CacheLoader if this cache doesn't contain it. * @throws CacheException in case of error */ public void load(final Object key) throws CacheException { if (registeredCacheLoaders.size() == 0) { LOG.debug("The CacheLoader is null. Returning."); return; } boolean existsOnCall = isKeyInCache(key); if (existsOnCall) { LOG.debug("The key {} exists in the cache. Returning.", key); return; } asynchronousPut(key, null, null); }
/** * Calls the CacheLoader and puts the result in the Cache */ public void run() { try { Set<Object> nonLoadedKeys = new HashSet<Object>(); for (Object key : keys) { if (!isKeyInCache(key)) { nonLoadedKeys.add(key); } } Map<?, ?> map = loadWithRegisteredLoaders(argument, nonLoadedKeys); for (Entry<?, ?> e : map.entrySet()) { put(new Element(e.getKey(), e.getValue())); } } catch (Throwable e) { if (LOG.isErrorEnabled()) { LOG.error("Problem during load. Load will not be completed. Cause was " + e.getCause(), e); } } } });
/** * 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); } } });
if (ehcache != null && ehcache.isKeyInCache(file)) { if (ehcache.isElementInMemory(file)) { final Element element = ehcache.get(file);
public boolean hasKey( V key ) { return ehcache.isKeyInCache( key ); }
@Override public boolean exists(String key) { return cache.isKeyInCache(key); }
@Override public boolean containsKey(Object o) { return delegate.isKeyInCache(o); }
@Override public boolean containsKey(Object o) { return delegate.isKeyInCache(o); }
public boolean isCached(String key) throws ExecutionIDNotFoundException { return cache.isKeyInCache(key); }
public boolean containsKey(String key) { return cache.isKeyInCache(key) && cache.get(key)!=null; }
/** * {@inheritDoc} */ @Override public boolean hasKey(String scope, Object key) throws Exception { return getScopeCache(scope).isKeyInCache(key); }
public boolean containsKey(Object key) { return getCache().isKeyInCache(key); }
public boolean containsKey(Object key) { return getCache().isKeyInCache(key); }
/** * This removes the key from the cache. Used for testing. */ public void removeKeyFromCache(String text) throws CachingCuratorException { if(curatorCache == null){ throw new CachingCuratorException("You need to open the cache before using it!"); } if (curatorCache.isKeyInCache(text)) { curatorCache.remove(text); logger.debug("successfully removed key from cache..."); } }
/** * Get a set of cached connections for the given key. * * @param key * The key corresponding to the cache entry whose connections are to be retrieved. * @return A {@link Set} of {@link Connection} objects for the given key. This set will be created if it does not * exist in the cache. */ private Set<CachedConnection> getCachedConnections(final String key) { Set<CachedConnection> connections; if (!cache.isKeyInCache(key)) { connections = new HashSet<CachedConnection>(); cache.put(new Element(key, connections)); } else connections = cast(cache.get(key).getObjectValue()); return connections; }
@Override protected <T> void storeElement(final String key, CacheElement<T> cacheElement) { if (!isEnabled()) { return; } final Element element = new Element(key, cacheElement); element.setTimeToLive(cacheTTL); if (cache.isKeyInCache(key)) { cache.replace(element); } else { cache.put(element); } }