001 002package org.cache2k; 003 004/* 005 * #%L 006 * cache2k API only package 007 * %% 008 * Copyright (C) 2000 - 2016 headissue GmbH, Munich 009 * %% 010 * This program is free software: you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License as 012 * published by the Free Software Foundation, either version 3 of the 013 * License, or (at your option) any later version. 014 * 015 * This program is distributed in the hope that it will be useful, 016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 018 * GNU General Public License for more details. 019 * 020 * You should have received a copy of the GNU General Public 021 * License along with this program. If not, see 022 * <http://www.gnu.org/licenses/gpl-3.0.html>. 023 * #L% 024 */ 025 026import java.io.Closeable; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030 031/** 032 * Interface to the cache2k cache implementation. To obtain a cache 033 * instance use the {@link CacheBuilder}. 034 * 035 * @see CacheBuilder to create a new cache 036 * @author Jens Wilke 037 */ 038@SuppressWarnings("UnusedDeclaration") 039public interface Cache<K, T> extends KeyValueSource<K,T>, Iterable<CacheEntry<K, T>>, Closeable { 040 041 public abstract String getName(); 042 043 /** 044 * Clear the cache contents 045 */ 046 public abstract void clear(); 047 048 /** 049 * Returns object in the cache that is mapped to the key. 050 */ 051 public abstract T get(K key); 052 053 /** 054 * Returns a mapped entry from the cache or null. If no entry is present or the entry 055 * is expired, null is also returned. 056 * 057 * <p>If an exception was thrown during fetching the entry via the cache source, 058 * method does not follow the same schema of rethrowing the exception like in get() 059 * and peek(), instead the exception can be retrieved via, 060 * {@link org.cache2k.CacheEntry#getException()} 061 * 062 * <p>The reason for the existence of this method is, that in the presence of 063 * null values it cannot be determined by peek() and get() if there is a mapping 064 * or a null value. 065 * 066 * <p>Multiple calls for the same key may return different instances of the entry 067 * object. 068 */ 069 public CacheEntry<K, T> getEntry(K key); 070 071 /** 072 * Signals the intent to call a get on the same key in the near future. 073 * 074 * <p/>Triggers a prefetch and returns immediately. If the entry is in 075 * the cache already and still fresh nothing will happen. If the entry 076 * is not in the cache or expired a fetch will be triggered. The fetch 077 * will take place with the same thread pool then the one used 078 * for background refresh. 079 */ 080 public abstract void prefetch(K key); 081 082 /** 083 * Signals the intend to call get on the set of keys in the near future. 084 * 085 * <p/>Without threads: Issues a bulk fetch on the set of keys not in 086 * the cache. 087 * 088 * <p/>With threads: If a thread is available than start the 089 * fetch operation on the missing key mappings and return after the 090 * keys are locked for data fetch within the fetch. A sequential get 091 * on a key will stall until the value is loaded. 092 */ 093 public abstract void prefetch(Set<K> keys); 094 095 public abstract void prefetch(List<K> keys, int _startIndex, int _afterEndIndex); 096 097 /** 098 * Returns the value if it is mapped within the cache and not expired, or null. 099 * No request on the cache source is made. 100 * 101 * @throws org.cache2k.PropagatedCacheException if an exception happened 102 * when the value was fetched by the cache source 103 */ 104 public abstract T peek(K key); 105 106 /** 107 * Returns a mapped entry from the cache or null. If no entry is present or the entry 108 * is expired, null is also returned. As with {@link #peek(Object)}, no request to the 109 * {@link CacheSource} is made, if no entry is available for the requested key. 110 * 111 * <p>If an exception was thrown during fetching the entry via the cache source, 112 * method does not follow the same schema of rethrowing the exception like in get() 113 * and peek(), instead the exception can be retrieved via, 114 * {@link org.cache2k.CacheEntry#getException()} 115 * 116 * <p>The reason for the existence of this method is, that in the presence of 117 * null values it cannot be determined by peek() and get() if there is a mapping 118 * or a null value. 119 * 120 * <p>Multiple calls for the same key may return different instances of the entry 121 * object. 122 */ 123 public abstract CacheEntry<K, T> peekEntry(K key); 124 125 /** 126 * Returns true if the there is a mapping for the specified key. Does not invoke 127 * the {@link CacheSource} if no entry exists. 128 */ 129 public abstract boolean contains(K key); 130 131 /** 132 * Set object value for the key 133 */ 134 public abstract void put(K key, T value); 135 136 public abstract boolean putIfAbsent(K key, T value); 137 138 /** 139 * Remove the object mapped to key from the cache. 140 */ 141 public abstract void remove(K key); 142 143 /** 144 * Remove the mappings for the keys atomically. Missing keys 145 * will be ignored. 146 * 147 * <p/>Parallel fetches from the cache source, that are currently 148 * ongoing for some of the given keys, will be ignored. 149 */ 150 public abstract void removeAllAtOnce(Set<K> key); 151 152 /** 153 * Disclaimer: This method is here to be able to support known coding similar 154 * to JSR107. Do not use it. Just use prefetch() and the normal Cache.get(). 155 * Since Cache.get() is almost as fast as a HashMap there is no need to 156 * build up mini-caches. The caller code will also look much cleaner. 157 * 158 * <p/>Bulk get: gets all values associated with the keys. If an exception 159 * happened during the fetch of any key, this exception will be thrown wrapped 160 * into a {@link PropagatedCacheException}. If more exceptions exist, the 161 * selection is arbitrary. 162 * 163 * <p/>The cache source does not need to support the bulk operation. It is 164 * neither guaranteed that the bulk get is called on the cache source if it 165 * exists. 166 * 167 * <p/>The operation may be split into chunks and not performed atomically. 168 * The entries that are processed within a chunk will be locked, to avoid 169 * duplicate fetches from the cache source. To avoid deadlocks there is a 170 * fallback non-bulk operation if a fetch is ongoing and the keys overlap. 171 * 172 * <p/>In contrast to JSR107 the following guarantees are met 173 * if the operation returns without exception: map.size() == keys.size() 174 * 175 * <p/>Exception handling: The method may terminate normal, even if a cache 176 * fetch via cache source failed. In this case the exception will be thrown 177 * when the value is requested from the map. 178 * 179 * @exception PropagatedCacheException may be thrown if the fetch fails. 180 */ 181 public Map<K, T> getAll(Set<? extends K> keys); 182 183 /** 184 * Number of entries the cache holds in total. When iterating the entries 185 * the cache will always return less or an identical number of entries. 186 * The reason for this is, that duplicate entries may exist in different 187 * storage layers (typically in heap and in persistent storage), or may be 188 * expired already. 189 * 190 * <p>The method has more statistical value and the result depends on the 191 * actual configuration of the cache. 192 * 193 * <p>TODO-API: Keep this for the final API? 194 */ 195 public abstract int getTotalEntryCount(); 196 197 /** 198 * Iterate all entries in the cache. 199 * 200 * <p>Contract: All entries present in the cache by the call of the method call will 201 * be iterated if not removed during the iteration goes on. The iteration may or may not 202 * iterate entries inserted during the iteration is in progress. The iteration never 203 * iterates duplicate entries. 204 * 205 * <p>The iteration is usable concurrently. Concurrent operations will not be 206 * influenced. Mutations of the cache, like remove or put, will not stop the iteration. 207 * 208 * <p>The iterator itself is not thread safe. Calls to one iterator instance from 209 * different threads need to be properly synchronized. 210 * 211 * <p>The iterator holds resources. If an iteration is aborted, the resources should 212 * be freed by calling {@link org.cache2k.ClosableIterator#close}, e.g. with a 213 * try with resources pattern. 214 */ 215 @Override 216 ClosableIterator<CacheEntry<K, T>> iterator(); 217 218 /** 219 * Remove persistent entries, that are not longer needed. Only has an effect 220 * if a storage is defined. 221 */ 222 public abstract void purge(); 223 224 /** 225 * Ensure that any transient data is stored in the persistence storage. 226 * Nothing will be done if no persistent storage is configured. 227 */ 228 public abstract void flush(); 229 230 /** 231 * @deprecated use {@link #close()} 232 */ 233 public abstract void destroy(); 234 235 /** 236 * Free all resources and remove the cache from the CacheManager. 237 * If persistence support is enabled, the cache may flush unwritten data. Depending on the 238 * configuration of the persistence, this method only returns after all unwritten data is 239 * flushed to disk. 240 * 241 * <p>The method is designed to free resources and finish operations as gracefully and fast 242 * as possible. Some cache operations take an unpredictable long time such as the call of 243 * the {@link CacheSource#get(Object)}, so it may happen that the cache still has threads 244 * in use when this method returns. 245 * 246 * <p>After close, subsequent cache operations will throw a {@link org.cache2k.CacheException}. 247 * Cache operations currently in progress, may or may not terminated with an exception. 248 * 249 * <p>If all caches need to be closed it is more effective to use {@link CacheManager#close()} 250 */ 251 public abstract void close(); 252 253 /** 254 * Returns information about the caches internal information. Calling {@link #toString} 255 * on the cache object is an expensive operation, since internal statistics are 256 * collected and other thread users need to be locked out, to have a consistent 257 * view. 258 */ 259 public String toString(); 260 261}