/
CacheMemoryStaticUtils.java
188 lines (168 loc) · 5.88 KB
/
CacheMemoryStaticUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.blankj.utilcode.util;
import androidx.annotation.NonNull;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2019/01/04
* desc : utils about memory cache
* </pre>
*/
public final class CacheMemoryStaticUtils {
private static CacheMemoryUtils sDefaultCacheMemoryUtils;
/**
* Set the default instance of {@link CacheMemoryUtils}.
*
* @param cacheMemoryUtils The default instance of {@link CacheMemoryUtils}.
*/
public static void setDefaultCacheMemoryUtils(final CacheMemoryUtils cacheMemoryUtils) {
sDefaultCacheMemoryUtils = cacheMemoryUtils;
}
/**
* Put bytes in cache.
*
* @param key The key of cache.
* @param value The value of cache.
*/
public static void put(@NonNull final String key, final Object value) {
put(key, value, getDefaultCacheMemoryUtils());
}
/**
* Put bytes in cache.
*
* @param key The key of cache.
* @param value The value of cache.
* @param saveTime The save time of cache, in seconds.
*/
public static void put(@NonNull final String key, final Object value, int saveTime) {
put(key, value, saveTime, getDefaultCacheMemoryUtils());
}
/**
* Return the value in cache.
*
* @param key The key of cache.
* @param <T> The value type.
* @return the value if cache exists or null otherwise
*/
public static <T> T get(@NonNull final String key) {
return get(key, getDefaultCacheMemoryUtils());
}
/**
* Return the value in cache.
*
* @param key The key of cache.
* @param defaultValue The default value if the cache doesn't exist.
* @param <T> The value type.
* @return the value if cache exists or defaultValue otherwise
*/
public static <T> T get(@NonNull final String key, final T defaultValue) {
return get(key, defaultValue, getDefaultCacheMemoryUtils());
}
/**
* Return the count of cache.
*
* @return the count of cache
*/
public static int getCacheCount() {
return getCacheCount(getDefaultCacheMemoryUtils());
}
/**
* Remove the cache by key.
*
* @param key The key of cache.
* @return {@code true}: success<br>{@code false}: fail
*/
public static Object remove(@NonNull final String key) {
return remove(key, getDefaultCacheMemoryUtils());
}
/**
* Clear all of the cache.
*/
public static void clear() {
clear(getDefaultCacheMemoryUtils());
}
///////////////////////////////////////////////////////////////////////////
// dividing line
///////////////////////////////////////////////////////////////////////////
/**
* Put bytes in cache.
*
* @param key The key of cache.
* @param value The value of cache.
* @param cacheMemoryUtils The instance of {@link CacheMemoryUtils}.
*/
public static void put(@NonNull final String key,
final Object value,
@NonNull final CacheMemoryUtils cacheMemoryUtils) {
cacheMemoryUtils.put(key, value);
}
/**
* Put bytes in cache.
*
* @param key The key of cache.
* @param value The value of cache.
* @param saveTime The save time of cache, in seconds.
* @param cacheMemoryUtils The instance of {@link CacheMemoryUtils}.
*/
public static void put(@NonNull final String key,
final Object value,
int saveTime,
@NonNull final CacheMemoryUtils cacheMemoryUtils) {
cacheMemoryUtils.put(key, value, saveTime);
}
/**
* Return the value in cache.
*
* @param key The key of cache.
* @param cacheMemoryUtils The instance of {@link CacheMemoryUtils}.
* @param <T> The value type.
* @return the value if cache exists or null otherwise
*/
public static <T> T get(@NonNull final String key, @NonNull final CacheMemoryUtils cacheMemoryUtils) {
return cacheMemoryUtils.get(key);
}
/**
* Return the value in cache.
*
* @param key The key of cache.
* @param defaultValue The default value if the cache doesn't exist.
* @param cacheMemoryUtils The instance of {@link CacheMemoryUtils}.
* @param <T> The value type.
* @return the value if cache exists or defaultValue otherwise
*/
public static <T> T get(@NonNull final String key,
final T defaultValue,
@NonNull final CacheMemoryUtils cacheMemoryUtils) {
return cacheMemoryUtils.get(key, defaultValue);
}
/**
* Return the count of cache.
*
* @param cacheMemoryUtils The instance of {@link CacheMemoryUtils}.
* @return the count of cache
*/
public static int getCacheCount(@NonNull final CacheMemoryUtils cacheMemoryUtils) {
return cacheMemoryUtils.getCacheCount();
}
/**
* Remove the cache by key.
*
* @param key The key of cache.
* @param cacheMemoryUtils The instance of {@link CacheMemoryUtils}.
* @return {@code true}: success<br>{@code false}: fail
*/
public static Object remove(@NonNull final String key, @NonNull final CacheMemoryUtils cacheMemoryUtils) {
return cacheMemoryUtils.remove(key);
}
/**
* Clear all of the cache.
*
* @param cacheMemoryUtils The instance of {@link CacheMemoryUtils}.
*/
public static void clear(@NonNull final CacheMemoryUtils cacheMemoryUtils) {
cacheMemoryUtils.clear();
}
private static CacheMemoryUtils getDefaultCacheMemoryUtils() {
return sDefaultCacheMemoryUtils != null ? sDefaultCacheMemoryUtils : CacheMemoryUtils.getInstance();
}
}