@@ -200,14 +200,21 @@ public Exception downloadFail(@NonNull HttpRequest<?> httpRequest, @NonNull Exce
200
200
@ Override
201
201
public Object readCache (@ NonNull HttpRequest <?> httpRequest , @ NonNull Type type , long cacheTime ) {
202
202
String cacheKey = HttpCacheManager .generateCacheKey (httpRequest );
203
- String cacheValue = HttpCacheManager .getMmkv (). getString ( cacheKey , null );
203
+ String cacheValue = HttpCacheManager .readHttpCache ( cacheKey );
204
204
if (cacheValue == null || "" .equals (cacheValue ) || "{}" .equals (cacheValue )) {
205
205
return null ;
206
206
}
207
- EasyLog .printLog (httpRequest , "----- readCache cacheKey -----" );
207
+ EasyLog .printLog (httpRequest , "----- read cache key -----" );
208
208
EasyLog .printJson (httpRequest , cacheKey );
209
- EasyLog .printLog (httpRequest , "----- readCache cacheValue -----" );
209
+ EasyLog .printLog (httpRequest , "----- read cache value -----" );
210
210
EasyLog .printJson (httpRequest , cacheValue );
211
+ EasyLog .printLog (httpRequest , "cacheTime = " + cacheTime );
212
+ boolean cacheInvalidate = HttpCacheManager .isCacheInvalidate (cacheKey , cacheTime );
213
+ EasyLog .printLog (httpRequest , "cacheInvalidate = " + cacheInvalidate );
214
+ if (cacheInvalidate ) {
215
+ // 表示缓存已经过期了,直接返回 null 给外层,表示缓存不可用
216
+ return null ;
217
+ }
211
218
return GsonFactory .getSingletonGson ().fromJson (cacheValue , type );
212
219
}
213
220
@@ -218,16 +225,19 @@ public boolean writeCache(@NonNull HttpRequest<?> httpRequest, @NonNull Response
218
225
if (cacheValue == null || "" .equals (cacheValue ) || "{}" .equals (cacheValue )) {
219
226
return false ;
220
227
}
221
- EasyLog .printLog (httpRequest , "----- writeCache cacheKey -----" );
228
+ EasyLog .printLog (httpRequest , "----- write cache key -----" );
222
229
EasyLog .printJson (httpRequest , cacheKey );
223
- EasyLog .printLog (httpRequest , "----- writeCache cacheValue -----" );
230
+ EasyLog .printLog (httpRequest , "----- write cache value -----" );
224
231
EasyLog .printJson (httpRequest , cacheValue );
225
- return HttpCacheManager .getMmkv ().putString (cacheKey , cacheValue ).commit ();
232
+ boolean writeHttpCacheResult = HttpCacheManager .writeHttpCache (cacheKey , cacheValue );
233
+ EasyLog .printLog (httpRequest , "writeHttpCacheResult = " + writeHttpCacheResult );
234
+ boolean refreshHttpCacheTimeResult = HttpCacheManager .setHttpCacheTime (cacheKey , System .currentTimeMillis ());
235
+ EasyLog .printLog (httpRequest , "refreshHttpCacheTimeResult = " + refreshHttpCacheTimeResult );
236
+ return writeHttpCacheResult && refreshHttpCacheTimeResult ;
226
237
}
227
238
228
239
@ Override
229
240
public void clearCache () {
230
- HttpCacheManager .getMmkv ().clearMemoryCache ();
231
- HttpCacheManager .getMmkv ().clearAll ();
241
+ HttpCacheManager .clearCache ();
232
242
}
233
243
}
0 commit comments