1 /* 2 * Copyright 2001-2004 The Apache Software Foundation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.commons.cache; 17 18 import java.io.Serializable; 19 20 /** 21 * A {@link Cache} defines an API for storing and later 22 * retrieving {@link Object}s based upon key values. 23 * <p> 24 * A {@link Cache} supports an event subscription/publication 25 * system. 26 * 27 * @version $Id: Cache.java 155435 2005-02-26 13:17:27Z dirkv $ 28 * @author Rodney Waldhoff 29 */ 30 public interface Cache extends Serializable { 31 /** 32 * Store the specified <i>val</i> under the specified 33 * <i>key</i>. 34 * 35 * @param key the key used to later obtain the <i>val</i> from me, 36 * which MUST NOT be <tt>null</tt>. 37 * @param val the val to store, which MUST NOT be <tt>null</tt>. 38 * @param expiry the timestamp at which the given <i>val</i> becomes stale, or <tt>null</tt>. 39 * @param cost the implemenation dependent cost of generating the <i>val</i>, or <tt>null</tt>. 40 * @return <tt>true</tt> if the <i>val</i> was stored, <tt>false</tt> otherwise. 41 */ 42 public boolean store(Serializable key, Serializable val, Long expiry, Long cost); 43 44 /** 45 * Store the specified <i>val</i> under the specified 46 * <i>key</i> and the specified <i>group</i>. 47 * 48 * @param key the key used to later obtain the <i>val</i> from me, 49 * which MUST NOT be <tt>null</tt>. 50 * @param group a meta-key which can be used to clear the object later 51 * @param val the val to store, which MUST NOT be <tt>null</tt>. 52 * @param expiry the timestamp at which the given <i>val</i> becomes stale, or <tt>null</tt>. 53 * @param cost the implemenation dependent cost of generating the <i>val</i>, or <tt>null</tt>. 54 * @return <tt>true</tt> if the <i>val</i> was stored, <tt>false</tt> otherwise. 55 */ 56 public boolean store(Serializable key, Serializable val, Long expiry, Long cost, Serializable group); 57 58 /** 59 * Obtain the value previously {@link #store stored} under 60 * the given <i>key</i>. 61 * 62 * @param key the key which MUST NOT be <tt>null</tt>. 63 * @return the previously {@link #store stored} value, or <tt>null</tt>. 64 */ 65 public Serializable retrieve(Serializable key); 66 67 public Serializable[] getKeysForGroup(Serializable group); 68 69 /** 70 * Returns <tt>true</tt> if I have a value associated with 71 * the given <i>key</i>, <tt>false</tt> otherwise. 72 * 73 * @param key the key which MUST NOT be <tt>null</tt>. 74 * @return <tt>true</tt> if I have a value associated with 75 * the given <i>key</i>, <tt>false</tt> otherwise. 76 */ 77 public boolean contains(Serializable key); 78 79 /** 80 * Remove any value previously {@link #store stored} under 81 * the given <i>key</i>. 82 * 83 * @param key the key which MUST NOT be <tt>null</tt>. 84 */ 85 public void clear(Serializable key); 86 87 /** 88 * Remove any value previously {@link #store stored} under 89 * the given <i>group</i>. 90 * 91 * @param group the group which MUST NOT be <tt>null</tt>. 92 */ 93 public void clearGroup(Serializable group); 94 95 /** 96 * Remove all values previously {@link #store stored}. 97 */ 98 public void clear(); 99 100 /** 101 * Add the given {@link StorageListener} to my 102 * set of {@link StorageListener}s. 103 * @link obs the observer to add 104 */ 105 public abstract void registerStorageListener(StorageListener obs); 106 107 /** 108 * Remove the given {@link StorageListener} from my 109 * set of {@link StorageListener}s. 110 * @link obs the observer to remove 111 */ 112 public abstract void unregisterStorageListener(StorageListener obs); 113 114 /** 115 * Clear my set of {@link StorageListener}s. 116 */ 117 public abstract void unregisterStorageListeners(); 118 119 /** 120 * Add the given {@link RetrievalListener} to my 121 * set of {@link RetrievalListener}s. 122 * @link obs the observer to add 123 */ 124 public abstract void registerRetrievalListener(RetrievalListener obs); 125 126 /** 127 * Remove the given {@link RetrievalListener} from my 128 * set of {@link RetrievalListener}s. 129 * @link obs the observer to remove 130 */ 131 public abstract void unregisterRetrievalListener(RetrievalListener obs); 132 133 /** 134 * Clear my set of {@link RetrievalListener}s. 135 */ 136 public abstract void unregisterRetrievalListeners(); 137 138 public abstract long getStat(CacheStat stat) throws UnsupportedOperationException; 139 }