Android uses cache to load data and android Cache
Recently, the app is almost finished, but a lot of list loading and news consultation data have been requested from the network, which is slow and affects the user experience. Therefore, I am thinking about using the cache to load some data with low Update Requirements.
Let's talk about the code.
First, create a cache storage tool class.
Import java. io. File;
Import java. io. IOException;
Import android. content. Context;
Import android. OS. Environment;
Import android. util. Log;
/**
* Cache Tool
*/
Public class ConfigCacheUtil {
Private static final String TAG = ConfigCacheUtil. class. getName ();
/** 1 second timeout */
Public static final int CONFIG_CACHE_SHORT_TIMEOUT = 1000*60*5; // 5 minutes
/** 5-minute timeout */
Public static final int CONFIG_CACHE_MEDIUM_TIMEOUT = 1000*3600*2; // 2 hours
/** Long cache time */
Public static final int CONFIG_CACHE_ML_TIMEOUT = 1000*60*60*24*1; // one day
/** Maximum Cache Time */
Public static final int CONFIG_CACHE_MAX_TIMEOUT = 1000*60*60*24*7; // 7 days
/**
* CONFIG_CACHE_MODEL_LONG: long time (7 days) cache mode <br>
* CONFIG_CACHE_MODEL_ML: long time (12 hours) cache mode <br>
* CONFIG_CACHE_MODEL_MEDIUM: Medium time (2 hours) cache mode <br>
* CONFIG_CACHE_MODEL_SHORT: short time (5 minutes) cache Mode
*/
Public enum ConfigCacheModel {
CONFIG_CACHE_MODEL_SHORT, CONFIG_CACHE_MODEL_MEDIUM, CONFIG_CACHE_MODEL_ML, CONFIG_CACHE_MODEL_LONG;
}
/**
* Get Cache
* @ Param url refers to the URL used to access the network.
* @ Return cache data
*/
Public static String getUrlCache (String url, ConfigCacheModel model, Context context ){
If (url = null ){
Return null;
}
String result = null;
String path = Constant. ENVIROMENT_DIR_CACHE + MD5Utils. md5Encrypt (url) + ". txt ";
File file = new File (path );
If (file. exists () & file. isFile ()){
Long expiredTime = System. currentTimeMillis ()-file. lastModified ();
Log. d (TAG, file. getAbsolutePath () + "expiredTime:" + expiredTime/60000 + "min ");
// 1. If the system time is incorrect
// 2. When the network is invalid, you can only read the cache
If (UIUtils. isNetWorkConnected (context )){
If (expiredTime <0 ){
Return null;
}
If (model = ConfigCacheModel. CONFIG_CACHE_MODEL_SHORT ){
If (expiredTime> CONFIG_CACHE_SHORT_TIMEOUT ){
Return null;
}
} Else if (model = ConfigCacheModel. CONFIG_CACHE_MODEL_MEDIUM ){
If (expiredTime> CONFIG_CACHE_MEDIUM_TIMEOUT ){
Return null;
}
} Else if (model = ConfigCacheModel. CONFIG_CACHE_MODEL_ML ){
If (expiredTime> CONFIG_CACHE_ML_TIMEOUT ){
Return null;
}
} Else if (model = ConfigCacheModel. CONFIG_CACHE_MODEL_LONG ){
If (expiredTime> CONFIG_CACHE_MEDIUM_TIMEOUT ){
Return null;
}
} Else {
If (expiredTime> CONFIG_CACHE_MAX_TIMEOUT ){
Return null;
}
}
}
Try {
Result = FileUtils. readTextFile (file );
} Catch (IOException e ){
E. printStackTrace ();
}
}
Return result;
}
/**
* Set Cache
* @ Param data
* @ Param url
*/
Public static void setUrlCache (String data, String url ){
If (Constant. ENVIROMENT_DIR_CACHE = null ){
Return;
}
File dir = new File (Constant. ENVIROMENT_DIR_CACHE );
If (! Dir. exists () & Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED )){
Dir. mkdirs ();
}
File file = new File (Constant. ENVIROMENT_DIR_CACHE + MD5Utils. md5Encrypt (url) + ". txt ");
Try {
// Create cached data to the disk, that is, create a file
FileUtils. writeTextFile (file, data );
} Catch (IOException e ){
Log. d (TAG, "write" + file. getAbsolutePath () + "data failed! ");
E. printStackTrace ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
/**
* Delete historical cache files
* @ Param cacheFile
*/
Public static void upload AchE (File cacheFile ){
If (cacheFile = null ){
If (Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED )){
Try {
File cacheDir = new File (Environment. getExternalStorageDirectory (). getPath () + "/gdxz/cache /");
If (cacheDir. exists ()){
Using AchE (cacheDir );
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
} Else if (cacheFile. isFile ()){
CacheFile. delete ();
} Else if (cacheFile. isDirectory ()){
File [] childFiles = cacheFile. listFiles ();
For (int I = 0; I <childFiles. length; I ++ ){
Using AchE (childFiles [I]);
}
}
}
}
You only need to call ConfigCacheUtil where the cache is needed. the getUrlCache method is used to pass in the corresponding url. The type and context determine whether the result is null. If the result is null, the cache request network does not load data. If it is not null, data in the cache is directly read and cached. If the result is returned, it is OK to process the json data.