001 /**
002 *
003 * Copyright 2004 Hiram Chirino
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.store.cache;
019
020 import org.activemq.message.ActiveMQMessage;
021 import org.activemq.util.LRUCache;
022
023 /**
024 * A simple cache that stores messages in memory. Cache entries are evicted
025 * when they they get to old (A LRU cache is used).
026 *
027 * @version $Revision: 1.1.1.1 $
028 */
029 public class SimpleMessageCache implements MessageCache {
030
031 private final LRUCache messages;
032
033 public SimpleMessageCache() {
034 this(1000);
035 }
036
037 public SimpleMessageCache(int cacheSize) {
038 this.messages = new LRUCache(cacheSize);
039 }
040
041 /**
042 * Gets a message that was previously <code>put</code> into this object.
043 *
044 * @param msgid
045 * @return null if the message was not previously put or if the message has expired out of the cache.
046 */
047 synchronized public ActiveMQMessage get(String msgid) {
048 return (ActiveMQMessage) messages.get(msgid);
049 }
050
051 /**
052 * Puts a message into the cache.
053 *
054 * @param messageID
055 * @param message
056 */
057 synchronized public void put(String messageID, ActiveMQMessage message) {
058 messages.put(messageID, message);
059 }
060
061 /**
062 * Remvoes a message from the cache.
063 *
064 * @param messageID
065 */
066 synchronized public void remove(String messageID) {
067 messages.remove(messageID);
068 }
069
070 public void close() {
071 messages.clear();
072 }
073
074 }