Пример #1
0
  public synchronized V put(K key, V value) {
    V oldValue = null;
    // Delete an old entry if it exists.
    if (map.containsKey(key)) {
      oldValue = remove(key, true);
    }

    CacheObject<V> cacheObject = new CacheObject<V>(value);
    map.put(key, cacheObject);
    // Make an entry into the cache order list.
    // Store the cache order list entry so that we can get back to it
    // during later lookups.
    cacheObject.lastAccessedListNode = lastAccessedList.addFirst(key);
    // Add the object to the age list
    LinkedListNode ageNode = ageList.addFirst(key);
    ageNode.timestamp = System.currentTimeMillis();
    cacheObject.ageListNode = ageNode;

    // If cache is too full, remove least used cache entries until it is not too full.
    cullCache();

    return oldValue;
  }