Exemplo n.º 1
0
 /**
  * Refresh a single element.
  *
  * <p>Refreshes bypass the {@link BlockingCache} and act directly on the backing {@link Ehcache}.
  * This way, {@link BlockingCache} gets can continue to return stale data while the refresh, which
  * might be expensive, takes place.
  *
  * <p>If the element is absent it is created
  *
  * <p>Quiet methods are used if argument 1 is true, so that statistics are not affected, but note
  * that replication will then not occur
  *
  * @param key
  * @param quiet whether the backing cache is quietly updated or not, if true replication will not
  *     occur
  * @return the refreshed Element
  * @throws CacheException
  * @since 1.6.1
  */
 public Element refresh(Object key, boolean quiet) throws CacheException {
   try {
     Ehcache backingCache = getCache();
     Element element = backingCache.getQuiet(key);
     if (element != null) {
       return refreshElement(element, backingCache, quiet);
     } else {
       // need to create
       return get(key);
     }
   } catch (CacheException ce) {
     throw ce;
   } catch (Exception e) {
     throw new CacheException(e.getMessage() + " on refresh with key " + key, e);
   }
 }
Exemplo n.º 2
0
  /**
   * Refresh the elements of this cache.
   *
   * <p>Refreshes bypass the {@link BlockingCache} and act directly on the backing {@link Ehcache}.
   * This way, {@link BlockingCache} gets can continue to return stale data while the refresh, which
   * might be expensive, takes place.
   *
   * <p>Quiet methods are used if argument 0 is true, so that statistics are not affected, but note
   * that replication will then not occur
   *
   * <p>Configure ehcache.xml to stop elements from being refreshed forever:
   *
   * <ul>
   *   <li>use timeToIdle to discard elements unused for a period of time
   *   <li>use timeToLive to discard elmeents that have existed beyond their allotted lifespan
   * </ul>
   *
   * @param quiet whether the backing cache is quietly updated or not, if true replication will not
   *     occur
   * @throws CacheException
   * @since 1.6.1
   */
  public void refresh(boolean quiet) throws CacheException {
    Exception exception = null;
    Object keyWithException = null;

    // Refetch the entries
    final Collection keys = getKeys();

    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine(getName() + ": found " + keys.size() + " keys to refresh");
    }

    // perform the refresh
    for (Iterator iterator = keys.iterator(); iterator.hasNext(); ) {
      final Object key = iterator.next();

      try {
        Ehcache backingCache = getCache();
        final Element element = backingCache.getQuiet(key);

        if (element == null) {
          if (LOG.isLoggable(Level.FINE)) {
            LOG.fine(getName() + ": entry with key " + key + " has been removed - skipping it");
          }

          continue;
        }

        refreshElement(element, backingCache, quiet);
      } catch (final Exception e) {
        // Collect the exception and keep going.
        // Throw the exception once all the entries have been refreshed
        // If the refresh fails, keep the old element. It will simply become staler.
        LOG.log(Level.WARNING, getName() + "Could not refresh element " + key, e);
        exception = e;
      }
    }

    if (exception != null) {
      throw new CacheException(exception.getMessage() + " on refresh with key " + keyWithException);
    }
  }