net.sf.ehcache.Cache.isKeyInCache()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(194)

本文整理了Java中net.sf.ehcache.Cache.isKeyInCache()方法的一些代码示例,展示了Cache.isKeyInCache()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.isKeyInCache()方法的具体详情如下:
包路径:net.sf.ehcache.Cache
类名称:Cache
方法名:isKeyInCache

Cache.isKeyInCache介绍

[英]An inexpensive check to see if the key exists in the cache.

This method is not synchronized. It is possible that an element may exist in the cache and be removed before the check gets to it, or vice versa. Since no assertions are made about the state of the Element it is possible that the Element is expired, but this method still returns true.
[中]

代码示例

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

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;
}

代码示例来源:origin: banq/jdonframework

public boolean contain(Object key) {
  Cache cache = manager.getCache(ehcacheConf.getPredefinedCacheName());
  return cache.isKeyInCache(key);
}

代码示例来源:origin: banq/jdonframework

/**
 * 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();
}

代码示例来源:origin: net.sf.ehcache/ehcache

/**
   * 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);

代码示例来源:origin: net.sf.ehcache/ehcache

/**
 * 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);
}

代码示例来源:origin: net.sf.ehcache/ehcache

/**
   * 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);
      }
    }
  }
});

代码示例来源:origin: net.sf.ehcache/ehcache

/**
   * 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);
    }
  }
});

代码示例来源:origin: geotools/geotools

if (ehcache != null && ehcache.isKeyInCache(file)) {
  if (ehcache.isElementInMemory(file)) {
    final Element element = ehcache.get(file);

代码示例来源:origin: org.apache.archiva.redback.components.cache/spring-cache-ehcache

public boolean hasKey( V key )
{
  return ehcache.isKeyInCache( key );
}

代码示例来源:origin: net.oschina.j2cache/j2cache-core

@Override
public boolean exists(String key) {
  return cache.isKeyInCache(key);
}

代码示例来源:origin: apache/marmotta

@Override
public boolean containsKey(Object o) {
  return delegate.isKeyInCache(o);
}

代码示例来源:origin: org.apache.marmotta/kiwi-caching-ehcache

@Override
public boolean containsKey(Object o) {
  return delegate.isKeyInCache(o);
}

代码示例来源:origin: org.astrogrid/astrogrid-cea-server

public boolean isCached(String key) throws ExecutionIDNotFoundException {
  return cache.isKeyInCache(key);
}

代码示例来源:origin: dayatang/dddlib

public boolean containsKey(String key) {
  return cache.isKeyInCache(key) && cache.get(key)!=null;
}

代码示例来源:origin: org.craftercms/crafter-core

/**
 * {@inheritDoc}
 */
@Override
public boolean hasKey(String scope, Object key) throws Exception {
  return getScopeCache(scope).isKeyInCache(key);
}

代码示例来源:origin: ujmp/universal-java-matrix-package

public boolean containsKey(Object key) {
  return getCache().isKeyInCache(key);
}

代码示例来源:origin: org.ujmp/ujmp-ehcache

public boolean containsKey(Object key) {
  return getCache().isKeyInCache(key);
}

代码示例来源:origin: edu.illinois.cs.cogcomp/cachingcurator

/**
 * 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...");
  }
}

代码示例来源:origin: com.cerner.ftp/jsch-util

/**
 * 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;
}

代码示例来源:origin: org.dd4t/dd4t-caching

@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);
  }
}

相关文章

Cache类方法