Ejemplo n.º 1
0
  /** This method is simple get without any loading behavior */
  @SuppressWarnings("unchecked")
  @Override
  public synchronized V get(Object key) {
    Pair<Long, V> pair = this.map.get(key);

    if (pair == null) {
      if (cacheLoader != null) {
        load((K) key, true, true);
        pair = this.map.get(key);
      } else {
        return null;
      }
    }
    if (expiresInSecs > 0
        && TimeUtils.getCurrentTimeMillis() - pair.getFirst() > expiresInSecs * 1000) {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("expiring " + key);
      }
      map.remove(key);
      if (cacheLoader != null) {
        load((K) key, false, true);
      }
    }

    return pair.getSecond();
  }
Ejemplo n.º 2
0
 @Override
 public synchronized V put(K key, V value) {
   Pair<Long, V> previous =
       map.put(key, new Pair<Long, V>(TimeUtils.getCurrentTimeMillis(), value));
   return previous != null ? previous.getSecond() : null;
 }