001 002package org.cache2k; 003 004/* 005 * #%L 006 * cache2k API 007 * %% 008 * Copyright (C) 2000 - 2016 headissue GmbH, Munich 009 * %% 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * #L% 022 */ 023 024import org.cache2k.expiry.ExpiryPolicy; 025import org.cache2k.expiry.ExpiryTimeValues; 026import org.cache2k.integration.CacheLoader; 027import org.cache2k.integration.CacheWriter; 028import org.cache2k.integration.CacheLoaderException; 029import org.cache2k.integration.CacheWriterException; 030import org.cache2k.processor.EntryProcessingException; 031import org.cache2k.processor.EntryProcessor; 032import org.cache2k.processor.EntryProcessingResult; 033 034import java.io.Closeable; 035import java.util.Iterator; 036import java.util.List; 037import java.util.Map; 038import java.util.concurrent.ConcurrentMap; 039import java.util.concurrent.Executor; 040import java.util.concurrent.TimeUnit; 041 042/* 043 * Descriptions derive partly from the java.util.concurrent.ConcurrentMap. 044 * Original copyright: 045 * 046 * Written by Doug Lea with assistance from members of JCP JSR-166 047 * Expert Group and released to the public domain, as explained at 048 * http://creativecommons.org/publicdomain/zero/1.0/ 049 */ 050 051/** 052 * A cache is similar to a map or a key value store, allowing to retrieve and 053 * update values which are associated to keys. In contrast to a {@code HashMap} the 054 * cache allows concurrent access and modification to its content and 055 * automatically controls the amount of entries in the cache to stay within 056 * configured resource limits. 057 * 058 * <p>A cache can be obtained via a {@link Cache2kBuilder}, for example: 059 * 060 * <pre>{@code 061 * Cache<Long, List<String>> cache = 062 * new Cache2kBuilder<Long, List<String>>() {} 063 * .name("myCache") 064 * .eternal(true) 065 * .build(); 066 * }</pre> 067 * 068 * <p><b>Basic operation:</b> To mutate and retrieve the cache content the operations 069 * {@link #put} and {@link #peek} can be used, for example: 070 * 071 * <pre>{@code 072 * cache.put(1, "one"); 073 * cache.put(2, "two"); 074 * // might fail: 075 * assertTrue(cache.containsKey(1)); 076 * assertEquals("two", cache.peek(2)); 077 * }</pre> 078 * 079 * It is important to note that the two assertion in the above example may fail. 080 * A cache has not the same guarantees as a data storage, because it needs to remove 081 * content automatically as soon as resource limits are reached. This is called <em>eviction</em>. 082 * 083 * <p><b>Populating:</b> A cache may automatically populate its contents via a {@link CacheLoader}. 084 * For typical read mostly caching this has several advantages, 085 * for details see {@link CacheLoader}. When using a cache loader the 086 * additional methods for mutating the cache directly may not be needed. Some 087 * methods, that do not interact with the loader such as {@link #containsKey} 088 * may be false friends. To make the code more obvious and protect against 089 * the accidental use of methods that do not invoke the loader transparently 090 * a subset interface, for example the {@link KeyValueSource} can be used. 091 * 092 * <p><b>CAS-Operations:</b> The cache has a set of operations that examine an entry 093 * and do a mutation in an atomic way, for example {@link #putIfAbsent}, {@link #containsAndRemove} and 094 * {@link #replaceIfEquals}. To allow arbitrary semantics that operate atomically on an {@link EntryProcessor} 095 * can be implemented and executed via {@link Cache#invoke}. 096 * 097 * @param <K> type of the key 098 * @param <V> type of the stores values 099 * @see Cache2kBuilder 100 * @author Jens Wilke 101 */ 102@SuppressWarnings("UnusedDeclaration") 103public interface Cache<K, V> extends 104 AdvancedKeyValueSource<K, V>, KeyValueStore<K,V>, 105 Iterable<CacheEntry<K, V>>, Closeable { 106 107 /** 108 * A configured or generated name of this cache instance. 109 * 110 * @see Cache2kBuilder#name(String) 111 * @return name of this cache or null for anonymous caches. 112 */ 113 String getName(); 114 115 /** 116 * Returns a value associated by the given key, that contains a non expired value. 117 * If no value is present or it is expired the cache loader 118 * is invoked, if configured, or {@code null} is returned. 119 * 120 * <p>If the loader is invoked, subsequent requests of the same key will block 121 * until the loading is completed. Details see {@link CacheLoader}. 122 * 123 * <p>As an alternative {@link #peek} can be used if the loader should 124 * not be invoked. 125 * 126 * @param key key with which the specified value is associated 127 * @return the value associated with the specified key, or 128 * {@code null} if there was no mapping for the key. 129 * (If nulls are permitted a {@code null} can also indicate that the cache 130 * previously associated {@code null} with the key) 131 * @throws ClassCastException if the class of the specified key 132 * prevents it from being stored in this cache 133 * @throws NullPointerException if the specified key is null 134 * @throws IllegalArgumentException if some property of the specified key 135 * prevents it from being stored in this cache 136 * @throws CacheLoaderException if the loading produced an exception . 137 */ 138 @Override 139 V get(K key); 140 141 /** 142 * Returns an entry associated by the given key, that contains a non expired value. 143 * If no entry is present or the value is expired, either the loader is invoked 144 * or {@code null} is returned. 145 * 146 * <p>If the loader is invoked, subsequent requests of the same key will block 147 * until the loading is completed, details see {@link CacheLoader} 148 * 149 * <p>In case the cache loader yields an exception, the entry object will 150 * be returned. The exception can be retrieved via {@link CacheEntry#getException()}. 151 * 152 * <p>If {@code null} values are present the method can be used to 153 * check for an existent mapping and retrieve the value in one API call. 154 * 155 * <p>The alternative method {@link #peekEntry} can be used if the loader 156 * should not be invoked. 157 * 158 * @param key key to retrieve the associated with the cache entry 159 * @throws ClassCastException if the class of the specified key 160 * prevents it from being stored in this cache 161 * @throws NullPointerException if the specified key is null 162 * @throws IllegalArgumentException if some property of the specified key 163 * prevents it from being stored in this cache 164 * @return An entry representing the cache mapping. Multiple calls for the same key may 165 * return different instances of the entry object. 166 */ 167 CacheEntry<K, V> getEntry(K key); 168 169 /** 170 * Notifies about the intend to retrieve the value for this key in the 171 * near future. 172 * 173 * <p>The method will return immediately and the cache will load the 174 * the value asynchronously if not yet present in the cache. Prefetching 175 * is done via a separate thread pool if specified via 176 * {@link Cache2kBuilder#prefetchExecutor(Executor)}. 177 * 178 * <p>No action is performed, if no reasonable action can be taken 179 * for a cache configuration, for example no {@link CacheLoader} is defined. 180 * 181 * <p>This method doesn't throw an exception in case the loader produced 182 * an exception. Exceptions will be propagated when the value is accessed. 183 * 184 * @param key the key that should be loaded, not {@code null} 185 * @see Cache2kBuilder#loaderThreadCount(int) 186 * @see Cache2kBuilder#prefetchExecutor(Executor) 187 */ 188 void prefetch(K key); 189 190 /** 191 * Notifies about the intend to retrieve the value for this key in the 192 * near future. See {@link #prefetch(Object)} for detailed explanation. 193 * 194 * <p>Exceptions from the loader will not be propagated via the listener. 195 * 196 * @param key the key that should be loaded, not {@code null} 197 * @param listener A listener that gets notified when the prefetch operation is finished 198 */ 199 void prefetch(CacheOperationCompletionListener listener, K key); 200 201 /** 202 * @deprecated Renamed to {@link #prefetchAll} 203 */ 204 void prefetch(Iterable<? extends K> keys); 205 206 /** 207 * Notifies about the intend to retrieve the value for the keys in the 208 * near future. See {@link #prefetch(Object)} for detailed explanation. 209 * 210 * @param keys the keys which should be loaded 211 */ 212 void prefetchAll(Iterable<? extends K> keys); 213 214 /** 215 * Notifies about the intend to retrieve the value for this key in the 216 * near future. See {@link #prefetch(Object)} for detailed explanation. 217 * 218 * <p>Exceptions from the loader will not be propagated via the listener. 219 * 220 * @param keys the keys which should be loaded 221 * @param listener A listener that gets notified when the prefetch operation is finished 222 */ 223 void prefetchAll(CacheOperationCompletionListener listener, Iterable<? extends K> keys); 224 225 /** 226 * Notifies about the intend to retrieve the value for this key in the 227 * near future. See {@link #prefetch(Object)} for detailed explanation. 228 * 229 * @param keys the keys which should be loaded 230 * @param listener A listener that gets notified when the prefetch operation is finished 231 */ 232 void prefetchAll(CacheOperationCompletionListener listener, K... keys); 233 234 /** 235 * @deprecated use a sublist and {@link #prefetch(Iterable)} 236 */ 237 void prefetch(List<? extends K> keys, int _startIndex, int _afterEndIndex); 238 239 /** 240 * Returns the value if it is mapped within the cache and it is not 241 * expired, or null. 242 * 243 * <p>In contrast to {@link #get(Object)} this method solely operates 244 * on the cache content and does not invoke the loader. 245 * 246 * <p>API rationale: Consequently all methods that do not invoke the loader 247 * but return a value or a cache entry are prefixed with {@code peek} within this interface 248 * to make the different semantics immediately obvious by the name. 249 * 250 * @param key key with which the specified value is associated 251 * @return the value associated with the specified key, or 252 * {@code null} if there was no mapping for the key. 253 * (If nulls are permitted a {@code null} can also indicate that the cache 254 * previously associated {@code null} with the key) 255 * @throws ClassCastException if the class of the specified key 256 * prevents it from being stored in this cache 257 * @throws NullPointerException if the specified key is null 258 * @throws IllegalArgumentException if some property of the specified key 259 * prevents it from being stored in this cache 260 * @throws CacheLoaderException if the loading produced an exception . 261 */ 262 V peek(K key); 263 264 /** 265 * Returns an entry associated by the given key, that contains a non expired value. 266 * The loader will not be invoked by this method. 267 * 268 * <p>In case the cache loader yields an exception, the entry object will 269 * be returned. The exception can be retrieved via {@link CacheEntry#getException()}. 270 * 271 * <p>If {@code null} values are present the method can be used to 272 * check for an existent mapping and retrieve the value in one API call. 273 ** 274 * @param key key to retrieve the associated with the cache entry 275 * @throws ClassCastException if the class of the specified key 276 * prevents it from being stored in this cache 277 * @throws NullPointerException if the specified key is null 278 * @throws IllegalArgumentException if some property of the specified key 279 * prevents it from being stored in this cache 280 * @return An entry representing the cache mapping. Multiple calls for the same key may 281 * return different instances of the entry object. 282 */ 283 CacheEntry<K, V> peekEntry(K key); 284 285 /** 286 * @deprecated use {@link #containsKey(Object)} 287 */ 288 boolean contains(K key); 289 290 /** 291 * Returns true, if there is a mapping for the specified key. 292 * 293 * <p>Statistics: The operation does increase the usage counter if a mapping is present, 294 * but does not count as read and therefore does not influence miss or hit values. 295 * 296 * @param key key which association should be checked 297 * @return {@code true}, if this cache contains a mapping for the specified 298 * key 299 * @throws ClassCastException if the key is of an inappropriate type for 300 * this cache 301 * @throws NullPointerException if the specified key is null 302 */ 303 boolean containsKey(K key); 304 305 /** 306 * Updates an existing cache entry for the specified key, so it associates 307 * the given value, or, insert a new cache entry for this key and value. 308 * 309 * <p>If an {@link ExpiryPolicy} is specified in the 310 * cache configuration it is called and will determine the expiry time. 311 * If a {@link CacheWriter} is configured in, then it is called with the 312 * new value. If the {@link ExpiryPolicy} or {@link CacheWriter} 313 * yield an exception the operation will be aborted and the previous 314 * mapping will be preserved. 315 * 316 * @param key key with which the specified value is associated 317 * @param value value to be associated with the specified key 318 * @throws ClassCastException if the class of the specified key or value 319 * prevents it from being stored in this cache. 320 * @throws NullPointerException if the specified key is null or the 321 * value is null and the cache does not permit null values 322 * @throws IllegalArgumentException if some property of the specified key 323 * or value prevents it from being stored in this cache. 324 * @throws CacheException if the cache was unable to process the request 325 * completely, for example, if an exceptions was thrown 326 * by a {@link CacheWriter} 327 */ 328 @Override 329 void put(K key, V value); 330 331 /** 332 * If the specified key is not already associated 333 * with a value, associate it with the given value. 334 * This is equivalent to 335 * <pre> {@code 336 * if (!cache.containsKey(key)) { 337 * cache.put(key, value); 338 * return true; 339 * } else { 340 * return false; 341 * }}</pre> 342 * 343 * except that the action is performed atomically. 344 * 345 * <p>See {@link #put(Object, Object)} for the effects on the cache writer and 346 * expiry calculation. 347 * 348 * <p>Statistics: If an entry exists this operation counts as a hit, if the entry 349 * is missing, a miss and put is counted. This definition is identical to the JSR107 350 * statistics semantics. This is not consistent with other operations like 351 * {@link #containsAndRemove(Object)} and {@link #containsKey(Object)} that don't update 352 * the hit and miss counter if solely the existence of an entry is tested and not the 353 * value itself is requested. This counting is subject to discussion and future change. 354 * 355 * @param key key with which the specified value is to be associated 356 * @param value value to be associated with the specified key 357 * @return {@code true}, if no entry was present and the value was associated with the key 358 * @throws ClassCastException if the class of the specified key or value 359 * prevents it from being stored in this cache 360 * @throws NullPointerException if the specified key is null or the 361 * value is null and the cache does not permit null values 362 * @throws IllegalArgumentException if some property of the specified key 363 * or value prevents it from being stored in this cache 364 */ 365 boolean putIfAbsent(K key, V value); 366 367 /** 368 * Replaces the entry for a key only if currently mapped to some value. 369 * This is equivalent to 370 * <pre> {@code 371 * if (cache.containsKey(key)) { 372 * cache.put(key, value); 373 * return cache.peek(key); 374 * } else 375 * return null; 376 * }</pre> 377 * 378 * except that the action is performed atomically. 379 * 380 * <p>As with {@link #peek(Object)}, no request to the {@link CacheLoader} is made, 381 * if no entry is associated to the requested key. 382 * 383 * @param key key with which the specified value is associated 384 * @param value value to be associated with the specified key 385 * @return the previous value associated with the specified key, or 386 * {@code null} if there was no mapping for the key. 387 * (A {@code null} return can also indicate that the cache 388 * previously associated {@code null} with the key) 389 * @throws ClassCastException if the class of the specified key or value 390 * prevents it from being stored in this cache 391 * @throws NullPointerException if the specified key is null or the 392 * value is null and the cache does not permit null values 393 * @throws IllegalArgumentException if some property of the specified key 394 * or value prevents it from being stored in this cache 395 * @throws CacheLoaderException if the loading of the entry produced 396 * an exception, which was not suppressed and is not yet expired 397 */ 398 V peekAndReplace(K key, V value); 399 400 /** 401 * Replaces the entry for a key only if currently mapped to some value. 402 * This is equivalent to 403 * <pre> {@code 404 * if (cache.containsKey(key)) { 405 * cache.put(key, value); 406 * return true 407 * } else 408 * return false; 409 * }</pre> 410 * 411 * except that the action is performed atomically. 412 * 413 * <p>Statistics: If an entry exists this operation counts as a hit, if the entry 414 * is missing, a miss and put is counted. This definition is identical to the JSR107 415 * statistics semantics. This is not consistent with other operations like 416 * {@link #containsAndRemove(Object)} and {@link #containsKey(Object)} that don't update 417 * the hit and miss counter if solely the existence of an entry is tested and not the 418 * value itself is requested. This counting is subject to discussion and future change. 419 * 420 * @param key key with which the specified value is associated 421 * @param value value to be associated with the specified key 422 * @return {@code true} if a mapping is present and the value was replaced. 423 * {@code false} if no entry is present and no action was performed. 424 * @throws ClassCastException if the class of the specified key or value 425 * prevents it from being stored in this cache. 426 * @throws NullPointerException if the specified key is null or the 427 * value is null and the cache does not permit null values 428 * @throws IllegalArgumentException if some property of the specified key 429 * or value prevents it from being stored in this cache. 430 */ 431 boolean replace(K key, V value); 432 433 /** 434 * Replaces the entry for a key only if currently mapped to a given value. 435 * This is equivalent to 436 * <pre> {@code 437 * if (cache.containsKey(key) && Objects.equals(cache.get(key), oldValue)) { 438 * cache.put(key, newValue); 439 * return true; 440 * } else 441 * return false; 442 * }</pre> 443 * 444 * except that the action is performed atomically. 445 * 446 * @param key key with which the specified value is associated 447 * @param oldValue value expected to be associated with the specified key 448 * @param newValue value to be associated with the specified key 449 * @return {@code true} if the value was replaced 450 * @throws ClassCastException if the class of a specified key or value 451 * prevents it from being stored in this map 452 * @throws NullPointerException if a specified key or value is null, 453 * and this map does not permit null keys or values 454 * @throws IllegalArgumentException if some property of a specified key 455 * or value prevents it from being stored in this map 456 */ 457 boolean replaceIfEquals(K key, V oldValue, V newValue); 458 459 /** 460 * Removes the mapping for a key from the cache if it is present. 461 * 462 * <p>Returns the value to which the cache previously associated the key, 463 * or {@code null} if the cache contained no mapping for the key. 464 * 465 * <p>If the cache does permit null values, then a return value of 466 * {@code null} does not necessarily indicate that the cache 467 * contained no mapping for the key. It is also possible that the cache 468 * explicitly associated the key to the value {@code null}. 469 * 470 * This is equivalent to 471 * <pre> {@code 472 * V tmp = cache.peek(key); 473 * cache.remove(key); 474 * return tmp; 475 * }</pre> 476 * 477 * except that the action is performed atomically. 478 * 479 * <p>As with {@link #peek(Object)}, no request to the {@link CacheLoader} is made, 480 * if no entry is associated to the requested key. 481 * 482 * @param key key whose mapping is to be removed from the cache 483 * @return the previous value associated with <tt>key</tt>, or 484 * <tt>null</tt> if there was no mapping for <tt>key</tt>. 485 * @throws NullPointerException if a specified key is null 486 * @throws ClassCastException if the key is of an inappropriate type for 487 * the cache. This check is optional depending on the cache 488 * configuration. 489 */ 490 V peekAndRemove(K key); 491 492 /** 493 * Removes the mapping for a key from the cache and returns true if it 494 * one was present. 495 * 496 * @param key key whose mapping is to be removed from the cache 497 * @return {@code true} if the cache contained a mapping for the specified key 498 * @throws NullPointerException if a specified key is null 499 * @throws ClassCastException if the key is of an inappropriate type for 500 * the cache. This check is optional depending on the cache 501 * configuration. 502 */ 503 boolean containsAndRemove(K key); 504 505 /** 506 * Removes the mapping for a key from the cache if it is present. 507 * 508 * <p>If a writer is registered {@link CacheWriter#delete(Object)} will get called. 509 * 510 * <p>These alternative versions of the remove operation exist: 511 * <ul> 512 * <li>{@link #containsAndRemove(Object)}, returning a success flag</li> 513 * <li>{@link #peekAndRemove(Object)}, returning the removed value</li> 514 * <li>{@link #removeIfEquals(Object, Object)}, conditional removal matching on the current value</li> 515 * </ul> 516 * 517 * @param key key which mapping is to be removed from the cache, not null 518 * @throws NullPointerException if a specified key is null 519 * @throws ClassCastException if the key is of an inappropriate type for 520 * this map 521 * @throws CacheWriterException if the writer call failed 522 */ 523 @Override 524 void remove(K key); 525 526 /** 527 * Remove the mapping if the stored value is the equal to the comparison value. 528 * 529 * @param key key whose mapping is to be removed from the cache 530 * @param expectedValue value that must match with the existing value in the cache 531 * @throws NullPointerException if a specified key is null 532 * @throws ClassCastException if the key is of an inappropriate type for 533 * this map 534 * @return true, if mapping was removed 535 */ 536 boolean removeIfEquals(K key, V expectedValue); 537 538 /** 539 * This was for atomic removal of a bunch of entries. Not supported any more. 540 * 541 * @deprecated no replacement planed, will be removed 542 * @throws UnsupportedOperationException always throws exceptions 543 */ 544 void removeAllAtOnce(java.util.Set<K> key); 545 546 /** 547 * Removes a set of keys. This has the same semantics of calling 548 * remove to every key, except that the cache is trying to optimize the 549 * bulk operation. 550 * 551 * @param keys a set of keys to remove 552 * @throws NullPointerException if a specified key is null 553 */ 554 @Override 555 void removeAll(Iterable<? extends K> keys); 556 557 /** 558 * Updates an existing cache entry for the specified key, so it associates 559 * the given value, or, insert a new cache entry for this key and value. The previous 560 * value will returned, or null if none was available. 561 * 562 * <p>Returns the value to which the cache previously associated the key, 563 * or {@code null} if the cache contained no mapping for the key. 564 * 565 * <p>If the cache does permit null values, then a return value of 566 * {@code null} does not necessarily indicate that the cache 567 * contained no mapping for the key. It is also possible that the cache 568 * explicitly associated the key to the value {@code null}. 569 * 570 * This is equivalent to 571 * <pre> {@code 572 * V tmp = cache.peek(key); 573 * cache.put(key, value); 574 * return tmp; 575 * }</pre> 576 * 577 * except that the action is performed atomically. 578 * 579 * <p>As with {@link #peek(Object)}, no request to the {@link CacheLoader} is made, 580 * if no entry is associated to the requested key. 581 * 582 * <p>See {@link #put(Object, Object)} for the effects on the cache writer and 583 * expiry calculation. 584 * 585 * @param key key with which the specified value is associated 586 * @param value value to be associated with the specified key 587 * @return {@code true} if a mapping is present and the value was replaced. 588 * {@code false} if no entry is present and no action was performed. 589 * @throws ClassCastException if the class of the specified key or value 590 * prevents it from being stored in this cache. 591 * @throws NullPointerException if the specified key is null or the 592 * value is null and the cache does not permit null values 593 * @throws IllegalArgumentException if some property of the specified key 594 * or value prevents it from being stored in this cache. 595 */ 596 V peekAndPut(K key, V value); 597 598 /** 599 * Updates an existing not expired mapping to expire at the given point in time. 600 * If there is no mapping associated with the key or it is already expired, this 601 * operation has no effect. The special values {@link org.cache2k.expiry.Expiry#NOW} and 602 * {@link org.cache2k.expiry.Expiry#REFRESH} also effect an entry that was just 603 * refreshed. 604 * 605 * <p>If the expiry time is in the past, the entry will expire immediately and 606 * refresh ahead is triggered, if enabled. 607 * 608 * <p>Also the special time value {@link org.cache2k.expiry.Expiry#NOW} might turn 609 * into a removal, the writer is not called, since the method is for cache control only. 610 * 611 * <p>If the cache must be configured with a {@link ExpiryPolicy} or 612 * {@link Cache2kBuilder#expireAfterWrite(long, TimeUnit)} otherwise expiry 613 * timing is not available and this method will throw an exception. 614 * 615 * @param key key with which the specified value is associated 616 * @param millis Time in milliseconds since epoch when the entry should expire. 617 * Also see {@link ExpiryTimeValues} 618 * @throws IllegalArgumentException if no expiry was enabled during cache setup. 619 */ 620 void expireAt(K key, long millis); 621 622 /** 623 * Asynchronously loads the given set of keys into the cache. Only missing or expired 624 * values will be loaded. 625 * 626 * <p>The cache uses multiple threads to load the values in parallel. If thread resources 627 * are not sufficient, meaning the used executor is throwing {@link java.util.concurrent.RejectedExecutionException} 628 * the calling thread is used to produce back pressure. 629 * 630 * <p>If no loader is defined, the method will throw an immediate exception. 631 * 632 * <p>After the load is completed, the completion listener will be called, if provided. 633 * 634 * @param l Listener interface that is invoked upon completion. May be null if no 635 * completion notification is needed. 636 * @param keys The keys to be loaded 637 * @throws UnsupportedOperationException if no loader is defined 638 */ 639 void loadAll(CacheOperationCompletionListener l, Iterable<? extends K> keys); 640 641 /** 642 * Asynchronously loads the given set of keys into the cache. Always invokes load for all keys 643 * and replaces values already in the cache. 644 * 645 * <p>The cache uses multiple threads to load the values in parallel. If thread resources 646 * are not sufficient, meaning the used executor is throwing {@link java.util.concurrent.RejectedExecutionException} 647 * the calling thread is used to produce back pressure. 648 * 649 * <p>If no loader is defined, the method will throw an immediate exception. 650 * 651 * <p>After the load is completed, the completion listener will be called, if provided. 652 * 653 * @param l Listener interface that is invoked upon completion. May be null if no 654 * completion notification is needed. 655 * @param keys The keys to be loaded 656 * @throws UnsupportedOperationException if no loader is defined 657 */ 658 void reloadAll(CacheOperationCompletionListener l, Iterable<? extends K> keys); 659 660 /** 661 * Invoke a user defined function on a cache entry. 662 * 663 * For examples and further details consult the documentation of {@link EntryProcessor} 664 * and {@link org.cache2k.processor.MutableCacheEntry}. 665 * 666 * @param key the key of the cache entry that should be processed 667 * @param entryProcessor processor instance to be invoked 668 * @param <R> type of the result 669 * @throws EntryProcessingException if the {@code invoke} had thrown an exception 670 * @return result provided by the entry processor 671 * @see EntryProcessor 672 * @see org.cache2k.processor.MutableCacheEntry 673 */ 674 <R> R invoke(K key, EntryProcessor<K, V, R> entryProcessor); 675 676 /** 677 * Invoke a user defined function on multiple cache entries specified by the 678 * {@code keys} parameter. 679 * 680 * <p>The order of the invocation is unspecified. To speed up processing the cache 681 * may invoke the entry processor in parallel. 682 * 683 * For examples and further details consult the documentation of {@link EntryProcessor} 684 * and {@link org.cache2k.processor.MutableCacheEntry}. 685 * 686 * @param keys the keys of the cache entries that should be processed 687 * @param entryProcessor processor instance to be invoked 688 * @param <R> type of the result 689 * @return map containing the invocation results for every cache key 690 * @see EntryProcessor 691 * @see org.cache2k.processor.MutableCacheEntry 692 */ 693 <R> Map<K, EntryProcessingResult<R>> invokeAll( 694 Iterable<? extends K> keys, EntryProcessor<K , V, R> entryProcessor); 695 696 /** 697 * Retrieve values from the cache associated with the provided keys. If the 698 * value is not yet in the cache, the loader is invoked. 699 * 700 * <p>Executing the request, the cache may do optimizations like 701 * utilizing multiple threads for invoking the loader or using the bulk 702 * methods on the loader. This is not yet fully exploited and will improve 703 * with further cache2k releases. 704 * 705 * <p>Exception handling: The method may terminate normal, even if the cache 706 * loader failed to provide values for some keys. The cache will generally 707 * do everything to delay the propagation of the exception until the key is requested, 708 * to be most specific. If the loader has permanent failures this method may 709 * throw an exception immediately. 710 * 711 * <p>The operation is not performed atomically. This operation may call different 712 * loader methods either {@link CacheLoader#loadAll(Iterable, Executor)} or 713 * {@link CacheLoader#load(Object)}. 714 * 715 * <p>Performance: A better technique is using {@link Cache#prefetchAll(Iterable)} 716 * and then {@link Cache#get(Object)} to request the the values. 717 * 718 * @throws NullPointerException if one of the specified keys is null 719 * @throws CacheLoaderException in case the loader has permanent failures. 720 * Otherwise the exception is thrown when the key is requested. 721 */ 722 @Override 723 Map<K, V> getAll(Iterable<? extends K> keys); 724 725 /** 726 * Bulk version for {@link #peek(Object)} 727 * 728 * <p>If the cache permits null values, the map will contain entries 729 * mapped to a null value. 730 * 731 * <p>If the loading of an entry produced an exception, which was not 732 * suppressed and is not yet expired. This exception will be thrown 733 * as {@link CacheLoaderException} when the entry is accessed 734 * via the map interface. 735 * 736 * <p>The operation is not performed atomically. Mutations of the cache during 737 * this operation may or may not affect the result. 738 * 739 * @throws NullPointerException if one of the specified keys is null 740 * @throws IllegalArgumentException if some property of the specified key 741 * prevents it from being stored in this cache 742 */ 743 Map<K, V> peekAll(Iterable<? extends K> keys); 744 745 /** 746 * Insert all elements of the map into the cache. 747 * 748 * <p>See {@link Cache#put(Object, Object)} for information about the 749 * interaction with the {@link CacheWriter} and {@link ExpiryPolicy} 750 * 751 * @param valueMap Map of keys with associated values to be inserted in the cache 752 * @throws NullPointerException if one of the specified keys is null 753 */ 754 @Override 755 void putAll(Map<? extends K, ? extends V> valueMap); 756 757 /** 758 * Will be removed. Returns always -1. Use the JMX bean. 759 */ 760 @Deprecated 761 int getTotalEntryCount(); 762 763 /** 764 * Iterate all entries in the cache. 765 * 766 * <p>Contract: All entries present in the cache by the call of the method call will 767 * be iterated if not removed during the iteration goes on. The iteration may or may not 768 * iterate entries inserted while the iteration is in progress. The iteration never 769 * iterates duplicate entries. 770 * 771 * <p>The iteration is usable concurrently. Concurrent operations will not be 772 * influenced. Mutations of the cache, like remove or put, will not stop the iteration. 773 * 774 * <p>The iterator itself is not thread safe. Calls to one iterator instance from 775 * different threads are illegal or need proper synchronization. 776 * 777 * 778 * <p>Statistics: Iteration is neutral to the cache statistics. Counting hits for iterated 779 * entries would effectively render the hitrate metric meaningless if iterations are used. 780 * 781 * <p>In case a storage (off heap or persistence) is attached the iterated entries are 782 * always inserted into the heap cache. This will affect statistics. 783 * 784 * <p>{@link Cache2kBuilder#refreshAhead(boolean)} is enabled there is a minimal chance 785 * that an entry in the cache will not be iterated if the iteration takes 786 * longer then the refresh/expiry time of one entry. 787 * 788 * @deprecated use {@link #keys()} 789 */ 790 @Override 791 Iterator<CacheEntry<K, V>> iterator(); 792 793 /** 794 * Iterate all keys in the cache. 795 * 796 * <p>Contract: The iteration is usable while concurrent operations happen on the cache. 797 * All entry keys will be iterated when present in the cache in the moment 798 * of the call to {@link Iterable#iterator()}. An expiration or mutation of an entry 799 * happening during the iteration, may or may not be reflected. Separate calls to 800 * {@link Iterable#iterator()} to the identical {@code Iterable} instance start 801 * a separate iteration. It is ensured that every key is only iterated once. 802 * 803 * <p>The iterator itself is not thread safe. Calls to one iterator instance from 804 * different threads are illegal or need proper synchronization. 805 * 806 * <p><b>Statistics:</b> Iteration is neutral to the cache statistics. 807 * 808 * <p><b>Efficiency:</b> Iterating keys is faster as iterating complete entries. 809 */ 810 Iterable<K> keys(); 811 812 /** 813 * Iterate all entries in the cache. 814 * 815 * <p>See {@link #keys()} for the general iterator contract. 816 * 817 * <p><b>Efficiency:</b> Iterating entries is less efficient then just iterating keys. The cache 818 * needs to create a new entry object and employ some sort of synchronisation to supply a 819 * consistent and immutable entry. 820 * 821 * @see #keys() 822 */ 823 Iterable<CacheEntry<K,V>> entries(); 824 825 /** 826 * Removes all cache contents. This has the same semantics of calling 827 * remove to every key, except that the cache is trying to optimize the 828 * bulk operation. Same as {@code clear} but listeners will be called. 829 */ 830 void removeAll(); 831 832 /** 833 * Clear the cache in a fast way, causing minimal disruption. Not calling the listeners. 834 */ 835 void clear(); 836 837 /** 838 * Remove persistent entries, that are not longer needed. Only has an effect 839 * if a storage is defined. 840 * @deprecated 841 */ 842 void purge(); 843 844 /** 845 * Ensure that any transient data is stored in the persistence storage. 846 * Nothing will be done if no persistent storage is configured. 847 * @deprecated 848 */ 849 void flush(); 850 851 /** 852 * @deprecated use {@link #close()} 853 */ 854 void destroy(); 855 856 /** 857 * This is currently identical to {@link Cache#close()}. 858 * 859 * <p>This method is to future proof the API, when a persistence feature is added. 860 * In this case the method will stop cache operations and remove all stored external data. 861 * 862 * <p>Rationale: The corresponding method in JSR107 is {@code CacheManager.destroyCache()}. 863 * Decided to put it on the cache interface, because: An application can finish its operation on 864 * a cache with one API call; to destroy all the cached data the cache must read the configuration 865 * and build its internal representation, which leads to a "half activated" cache; the additional 866 * call may lead to a race conditions. 867 */ 868 void clearAndClose(); 869 870 /** 871 * Free all resources and remove the cache from the CacheManager. 872 * 873 * <p>The method is designed to free resources and finish operations as gracefully and fast 874 * as possible. Some cache operations take an unpredictable long time such as the call of 875 * the {@link CacheLoader}, so it may happen that the cache still has threads 876 * in use when this method returns. 877 * 878 * <p>After close, subsequent cache operations will throw a {@link IllegalStateException}. 879 * Cache operations currently in progress, may or may not terminated with an exception. A subsequent 880 * call to close will not throw an exception. 881 * 882 * <p>If all caches need to be closed it is more effective to use {@link CacheManager#close()} 883 * 884 * <p>The next releases of cache2k may store cache entries between restarts. If an application will 885 * never use the cached content again, the method {@link #clearAndClose()} should be used instead. 886 */ 887 void close(); 888 889 /** 890 * Return the cache manager for this cache instance. 891 */ 892 CacheManager getCacheManager(); 893 894 /** 895 * True if cache was closed or closing is in progress. 896 */ 897 boolean isClosed(); 898 899 /** 900 * Returns internal information. This is an expensive operation, since internal statistics are 901 * collected. During the call, concurrent operations on the cache may be blocked, to check 902 * consistency. 903 */ 904 String toString(); 905 906 /** 907 * Request an alternative interface for this cache instance. 908 */ 909 <X> X requestInterface(Class<X> _type); 910 911 /** 912 * Returns a map interface for operating with this cache. Operations on the map 913 * affect the cache directly, as well as modifications on the cache will affect the map. 914 * 915 * <p>The returned map supports {@code null} values if enabled via 916 * {@link Cache2kBuilder#permitNullValues(boolean)}. 917 * 918 * <p>The {@code equals} and {@code hashCode} methods of the {@code Map} are forwarded to the cache, so a 919 * map is considered identical when from the same cache instance. This is not compatible to the general 920 * {@code Map} contract. 921 * 922 * <p>Multiple calls to this method return a new object instance which is a wrapper of the cache 923 * instance. Calling this method is a cheap operation. 924 * 925 * <p>The current {@code ConcurrentMap} implementation minimalistic and not optimized for all 926 * usage aspects. Calling the cache methods directly could be more effective. 927 * 928 * @return {@code ConcurrentMap} wrapper for this cache instance 929 */ 930 ConcurrentMap<K,V> asMap(); 931 932}