@@ -2,8 +2,79 @@ package com.moumoux.quinine.reactive
2
2
3
3
import io.reactivex.Single
4
4
5
+ /* *
6
+ * A semi-persistent mapping from keys to values. Values are automatically loaded by the cache,
7
+ * and are stored in the cache until either evicted or manually invalidated.
8
+ * <p>
9
+ * Implementations of this interface are expected to be thread-safe, and can be safely accessed
10
+ * by multiple concurrent threads.
11
+ *
12
+ * @author light.tsing@gmail.com (Akase Cho)
13
+ * @param K the most general key type this builder will be able to create caches for.
14
+ * @param V the most general value type this builder will be able to create caches for.
15
+ */
5
16
interface QuinineLoadingCache <K : Any , V > : QuinineCache <K , V > {
17
+ /* *
18
+ * Returns the value associated with the [key] in this cache, obtaining that value from
19
+ * loader if necessary.
20
+ * <p>
21
+ * If another call to [get] is currently loading the value for the [key], this thread
22
+ * simply waits for that thread to finish and returns its loaded value. Note that multiple threads
23
+ * can concurrently load values for distinct keys.
24
+ * <p>
25
+ * If the specified key is not already associated with a value, attempts to compute its value and
26
+ * enters it into this cache unless <pre>null</pre>. The entire method invocation is performed
27
+ * atomically, so the function is applied at most once per key. Some attempted update operations
28
+ * on this cache by other threads may be blocked while the computation is in progress, so the
29
+ * computation should be short and simple, and must not attempt to update any other mappings of
30
+ * this cache.
31
+ *
32
+ * @param key key with which the specified value is to be associated
33
+ * @return the current (existing or computed) value associated with the specified key, or null if
34
+ * the computed value is null
35
+ * @throws IllegalStateException if the computation detectably attempts a recursive update to this
36
+ * cache that would otherwise never complete
37
+ * @throws RuntimeException or Error if the loader does so, in which case the mapping
38
+ * is left unestablished
39
+ */
6
40
fun get (key : K ): Single <V >
41
+
42
+ /* *
43
+ * Returns a map of the values associated with the [keys], creating or retrieving those
44
+ * values if necessary. The returned map contains entries that were already cached, combined with
45
+ * the newly loaded entries; it will never contain null keys or values.
46
+ * <p>
47
+ * Caches loaded by a loader will issue a single request to for all keys which are not already
48
+ * present in the cache. All entries returned by loader will be stored in the cache, over-writing
49
+ * any previously cached values. If another call to [get] tries to load the value for a key in
50
+ * [keys], implementations may either have that thread load the entry or simply wait for
51
+ * this thread to finish and returns the loaded value. In the case of overlapping non-blocking
52
+ * loads, the last load to complete will replace the existing entry. Note that multiple threads
53
+ * can concurrently load values for distinct keys.
54
+ * <p>
55
+ * Note that duplicate elements in [keys], as determined by [Any.equals], will be
56
+ * ignored.
57
+ *
58
+ * @param keys the keys whose associated values are to be returned
59
+ * @return the readonly mapping of keys to values for the specified keys in this cache
60
+ * @throws RuntimeException or Error if the loader does so, if
61
+ * loader returns <pre>null</pre>, returns a map containing null keys or
62
+ * values, or fails to return an entry for each requested key. In all cases, the mapping
63
+ * is left unestablished
64
+ */
7
65
fun getAll (keys : Iterable <K >): Map <K , Single <V >>
66
+
67
+ /* *
68
+ * Loads a new value for the [key] , asynchronously. While the new value is loading the
69
+ * previous value (if any) will continue to be returned by [get] unless it is evicted.
70
+ * If the new value is loaded successfully it will replace the previous value in the cache; if an
71
+ * exception is thrown while refreshing the previous value will remain, <i>and the exception will
72
+ * be logged (using [java.util.logging.Logger]) and swallowed</i>.
73
+ * <p>
74
+ * Caches loaded by a loader will be called if the cache currently contains a value for the [key],
75
+ * and load otherwise. Loading is asynchronous by delegating to the default executor.
76
+ *
77
+ * @param key key with which a value may be associated
78
+ */
8
79
fun refresh (key : K )
9
80
}
0 commit comments