SimpleMessageCache xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.store.cache;
    19
    20  import org.codehaus.activemq.message.ActiveMQMessage;
    21  import org.codehaus.activemq.util.LRUCache;
    22
    23  /***
    24   * A simple cache that stores messages in memory.  Cache entries are evicted
    25   * when they they get to old (A LRU cache is used).
    26   *
    27   * @version $Revision: 1.2 $
    28   */
    29  public class SimpleMessageCache implements MessageCache {
    30
    31  	private final LRUCache messages;
    32
    33  	public SimpleMessageCache() {
    34  		this(1000);
    35  	}
    36
    37  	public SimpleMessageCache(int cacheSize) {
    38  		this.messages = new LRUCache(cacheSize);
    39  	}
    40
    41  	/***
    42  	 * Gets a message that was previously <code>put</code> into this object.
    43  	 *
    44  	 * @param msgid
    45  	 * @return null if the message was not previously put or if the message has expired out of the cache.
    46  	 */
    47  	synchronized public ActiveMQMessage get(String msgid) {
    48  		return (ActiveMQMessage) messages.get(msgid);
    49  	}
    50
    51  	/***
    52  	 * Puts a message into the cache.
    53  	 *
    54  	 * @param messageID
    55  	 * @param message
    56  	 */
    57  	synchronized public void put(String messageID, ActiveMQMessage message) {
    58  		messages.put(messageID, message);
    59  	}
    60
    61  	/***
    62  	 * Remvoes a message from the cache.
    63  	 *
    64  	 * @param messageID
    65  	 */
    66  	synchronized public void remove(String messageID) {
    67  		messages.remove(messageID);
    68  	}
    69
    70  }