001package org.apache.commons.jcs.engine.memory.lru;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023
024import org.apache.commons.jcs.engine.behavior.ICacheElement;
025import org.apache.commons.jcs.engine.memory.AbstractDoubleLinkedListMemoryCache;
026import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
027
028/**
029 * A fast reference management system. The least recently used items move to the end of the list and
030 * get spooled to disk if the cache hub is configured to use a disk cache. Most of the cache
031 * bottlenecks are in IO. There are no io bottlenecks here, it's all about processing power.
032 * <p>
033 * Even though there are only a few adjustments necessary to maintain the double linked list, we
034 * might want to find a more efficient memory manager for large cache regions.
035 * <p>
036 * The LRUMemoryCache is most efficient when the first element is selected. The smaller the region,
037 * the better the chance that this will be the case. &lt; .04 ms per put, p3 866, 1/10 of that per get
038 */
039public class LRUMemoryCache<K, V>
040    extends AbstractDoubleLinkedListMemoryCache<K, V>
041{
042    /**
043     * Puts an item to the cache. Removes any pre-existing entries of the same key from the linked
044     * list and adds this one first.
045     * <p>
046     * @param ce The cache element, or entry wrapper
047     * @return MemoryElementDescriptor the new node
048     * @throws IOException
049     */
050    @Override
051    protected MemoryElementDescriptor<K, V> adjustListForUpdate( ICacheElement<K, V> ce )
052        throws IOException
053    {
054        return addFirst( ce );
055    }
056
057    /**
058     * Makes the item the first in the list.
059     * <p>
060     * @param me
061     */
062    @Override
063    protected void adjustListForGet( MemoryElementDescriptor<K, V> me )
064    {
065        list.makeFirst( me );
066    }
067}