Ejemplo n.º 1
0
 protected Object clone() {
   LRUCacheEntry entry = new LRUCacheEntry();
   entry.hash = hash;
   entry.key = key;
   entry.value = value;
   entry.expirationTime = expirationTime;
   entry.next = (next != null) ? (LRUCacheEntry) next.clone() : null;
   return entry;
 }
Ejemplo n.º 2
0
  /**
   * Maps the specified <code>key</code> to the specified <code>value</code> in this hashtable.
   * Neither the key nor the value can be <code>null</code>.
   *
   * <p>The value can be retrieved by calling the <code>get</code> method with a key that is equal
   * to the original key.
   *
   * @param key the hashtable key.
   * @param value the value.
   * @return the previous value of the specified key in this hashtable, or <code>null</code> if it
   *     did not have one.
   * @exception NullPointerException if the key or value is <code>null</code>.
   * @see java.lang.Object#equals(java.lang.Object)
   * @see java.util.LRUCache#get(java.lang.Object)
   * @since JDK1.0
   */
  public Object put(Object key, Object value) {
    // Make sure the value is not null
    if (value == null) {
      throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    LRUCacheEntry tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    // LoggingService.getDefaultLogger().logDebug(this, "=====================>Index: "+index );
    for (LRUCacheEntry e = tab[index]; e != null; e = e.next) {
      if ((e.hash == hash) && e.key.equals(key)) {
        Object old = e.value;
        e.value = value;
        return old;
      }
    }

    if (count >= threshold) {
      // Rehash the table if the threshold is exceeded
      rehash();
      return put(key, value);
    }

    // Creates the new entry.
    LRUCacheEntry e = new LRUCacheEntry();
    e.hash = hash;
    e.key = key;
    e.value = value;
    e.expirationTime = (expirationTime > 0) ? (System.currentTimeMillis() + expirationTime) : -1;
    e.next = tab[index];
    tab[index] = e;
    count++;
    if (cacheMonitorThread == null || !cacheMonitorThread.isAlive()) {
      cacheMonitorThread = new CacheMonitor(); // new Thread(new CacheMonitor() );
      cacheMonitorThread.setDaemon(true);
      cacheMonitorThread.setPriority(Thread.NORM_PRIORITY - 1);
      cacheMonitorThread.start();
    }
    return null;
  }