@@ -3,19 +3,131 @@ package com.moumoux.quinine.reactive
3
3
import com.moumoux.quinine.QuinineCacheStats
4
4
import io.reactivex.Single
5
5
6
+ /* *
7
+ * A semi-persistent mapping from keys to values. Cache entries are manually added using
8
+ * [get] or [put], and are stored in the cache until either evicted or manually invalidated.
9
+ * <p>
10
+ * Implementations of this interface are expected to be thread-safe, and can be safely accessed by
11
+ * multiple concurrent threads.
12
+ *
13
+ * @author light.tsing@gmail.com (Akase Cho)
14
+ * @param K the most general key type this builder will be able to create caches for.
15
+ * @param V the most general value type this builder will be able to create caches for.
16
+ */
6
17
interface QuinineCache <K : Any , V > {
18
+ /* *
19
+ * Returns a current snapshot of this cache's cumulative statistics. All statistics are
20
+ * initialized to zero, and are monotonically increasing over the lifetime of the cache.
21
+ * <p>
22
+ * Due to the performance penalty of maintaining statistics, some implementations may not record
23
+ * the usage history immediately or at all.
24
+ *
25
+ * @return [QuinineCacheStats] snapshot instance
26
+ */
7
27
val stats: QuinineCacheStats
8
- val estimatedSize: Long
9
28
10
- fun cleanUp ()
29
+ /* *
30
+ * Returns the approximate number of entries in this cache. The value returned is an estimate; the
31
+ * actual count may differ if there are concurrent insertions or removals, or if some entries are
32
+ * pending removal due to expiration or weak/soft reference collection. In the case of stale
33
+ * entries this inaccuracy can be mitigated by performing a [cleanUp] first.
34
+ */
35
+ val estimatedSize: Long
11
36
37
+ /* *
38
+ * Discards any cached value for the [key]. The behavior of this operation is undefined for
39
+ * an entry that is being loaded (or reloaded) and is otherwise not present.
40
+ *
41
+ * @param key the key whose mapping is to be removed from the cache
42
+ */
12
43
fun invalidate (key : Any )
44
+
45
+ /* *
46
+ * Discards any cached values for the [keys]. The behavior of this operation is undefined
47
+ * for an entry that is being loaded (or reloaded) and is otherwise not present.
48
+ *
49
+ * @param keys the keys whose associated values are to be removed
50
+ */
13
51
fun invalidateAll (keys : Iterable <* >)
52
+
53
+ /* *
54
+ * Discards all entries in the cache. The behavior of this operation is undefined for an entry
55
+ * that is being loaded (or reloaded) and is otherwise not present.
56
+ */
14
57
fun invalidateAll ()
15
58
59
+
60
+ /* *
61
+ * Associates the [value] with the [key] in this cache. If the cache previously contained
62
+ * a value associated with the [key], the old value is replaced by the new [value]
63
+ * <p>
64
+ * Prefer [get] when using the conventional "if cached, return; otherwise create, cache and return" pattern.
65
+ *
66
+ * @param key the key with which the specified value is to be associated
67
+ * @param value value to be associated with the specified key
68
+ */
16
69
fun put (key : K , value : Single <V >)
70
+
71
+ /* *
72
+ * Copies all of the mappings from the specified map to the cache. The effect of this call is
73
+ * equivalent to that of calling [put] on this map once for each mapping from key
74
+ * [K] to value [V] in the specified map. The behavior of this operation is undefined
75
+ * if the specified map is modified while the operation is in progress.
76
+ *
77
+ * @param map the mappings to be stored in this cache
78
+ */
17
79
fun putAll (map : Map <out K , Single <V >>)
80
+
81
+ /* *
82
+ * Returns the value associated with the [key] in this cache, obtaining that value from the
83
+ * [mappingFunction] if necessary. This method provides a simple substitute for the
84
+ * conventional "if cached, return; otherwise create, cache and return" pattern.
85
+ * <p>
86
+ * If the specified key is not already associated with a value, attempts to compute its value
87
+ * using the given mapping function and enters it into this cache unless <pre>null</pre>. The entire
88
+ * method invocation is performed atomically, so the function is applied at most once per key.
89
+ * Some attempted update operations on this cache by other threads may be blocked while the
90
+ * computation is in progress, so the computation should be short and simple, and must not attempt
91
+ * to update any other mappings of this cache.
92
+ * <p>
93
+ * <b>Warning:</b> [mappingFunction] <b>must not</b> attempt to update any other mappings of this cache.
94
+ *
95
+ * @param key the key with which the specified value is to be associated
96
+ * @param mappingFunction the function to compute a value
97
+ * @return the current (existing or computed) value associated with the specified key, or null if
98
+ * the computed value is null
99
+ * @throws IllegalStateException if the computation detectably attempts a recursive update to this
100
+ * cache that would otherwise never complete
101
+ * @throws RuntimeException or Error if the mappingFunction does so, in which case the mapping is
102
+ * left unestablished
103
+ */
18
104
fun get (key : K , mappingFunction : (K ) -> Single <V >): Single <V >
105
+
106
+ /* *
107
+ * Returns the value associated with the [key] in this cache, or <pre>null</pre> if there is no
108
+ * cached value for the [key].
109
+ *
110
+ * @param key the key whose associated value is to be returned
111
+ * @return the value to which the specified key is mapped, or <pre>null</pre> if this cache contains
112
+ * no mapping for the key
113
+ */
19
114
fun getIfPresent (key : Any ): Single <V >
115
+
116
+ /* *
117
+ * Returns a map of the values associated with the [keys] in this cache. The returned map
118
+ * will only contain entries which are already present in the cache.
119
+ * <p>
120
+ * Note that duplicate elements in [keys], as determined by [Any.equals] , will be ignored.
121
+ *
122
+ * @param keys the keys whose associated values are to be returned
123
+ * @return the readonly mapping of keys to values for the specified keys found in this cache
124
+ */
20
125
fun getAllPresent (keys : Iterable <* >): Map <K , Single <V >>
126
+
127
+
128
+ /* *
129
+ * Performs any pending maintenance operations needed by the cache. Exactly which activities are
130
+ * performed -- if any -- is implementation-dependent.
131
+ */
132
+ fun cleanUp ()
21
133
}
0 commit comments