日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android对象缓存,Android简单实现 缓存数据

發布時間:2023/12/1 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android对象缓存,Android简单实现 缓存数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

1、每一種要緩存的數據都是有對應的versionCode,通過versionCode請求網絡獲取是否需要更新

2、提前將要緩存的數據放入assets文件夾中,打包上線。

緩存設計

代碼實現

/**

* Created by huangbo on 2017/6/19.

*

* 主要是緩存的工具類

*

* 緩存設計:

* 0.從內存中讀取數據 :0.1 讀取成功-> 取出versionCode ->3

* 0.2 讀取失敗-> 1

*

* 1.從文件中讀取數據:1.1讀取成成功-> 取出versionCode ->3

* 1.2讀取失敗-> 2

* 2.從Assets中讀取數據:2.1讀取成功-> 取出versionCode ->3

* 2.2讀取失敗-> versionCode==0 ->3

*

* 3.用versionCode請求網絡 3.1請求成功(有版本更新)將文件寫入內存,寫入文件;

* 3.1 請求失敗,(沒有版本更新)

*

*/

public class CacheData {

public static CacheData cacheData;

public static CacheData getInstance() {

if (cacheData == null) {

cacheData = new CacheData();

}

return cacheData;

}

String mFileName;

public CacheData cacheName(String mFileName) {

this.mFileName = mFileName;

return this;

}

ExecutorService cachedThreadPool;

private CacheData() {

cachedThreadPool = Executors.newCachedThreadPool();

}

/**

* 從assets 中讀取文件

*

* @return cacheData 的Json串

*/

private String readDataFromAssets() {

try {

InputStream ips = AppUtils.ApplicationContext.getAssets().open(mFileName);

byte[] bytes = new byte[ips.available()];

ips.read(bytes);

ips.close();

return new String(bytes);

} catch (IOException e) {

e.printStackTrace();

}

return "";

}

public void readDataFromAssets(final Handler handler) {

cachedThreadPool.execute(new Runnable() {

@Override

public void run() {

String json = readDataFromAssets();

Message message = handler.obtainMessage(1, json);

handler.sendMessage(message);

}

});

}

public void readDataFromFile(final Handler handler) {

cachedThreadPool.execute(new Runnable() {

@Override

public void run() {

Message message = handler.obtainMessage(1, readDataFromFile());

handler.sendMessage(message);

}

});

}

/**

* 將region 更新到指定文件里

* @param

*/

public void writeData2FileWithThread(final String Data) {

cachedThreadPool.execute(new Runnable() {

@Override

public void run() {

writeRegion2File(Data);

}

});

}

/**

* @return cacheData 的Json串

*/

private String readDataFromFile() {

try {

File file = new File(AppUtils.getCacheDirectory(), mFileName);

if (!file.exists()) {

return null;

}

FileInputStream fis = new FileInputStream(file);

byte[] bytes = new byte[fis.available()];

fis.read(bytes);

fis.close();

return new String(bytes);

} catch (IOException e) {

e.printStackTrace();

}

return "";

}

private void writeData2File(String jsonData) {

try {

File cityFile = new File(AppUtils.getCacheDirectory(), mFileName);

if (!cityFile.exists()) {

cityFile.createNewFile();

}

FileOutputStream fos = new FileOutputStream(cityFile);

fos.write(regionJsonData.getBytes("UTF-8"));

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

使用方法

/**

* Created by huangbo on 2017/6/8.

*/

public class Region {

public static Region region;

public static Region getInstance() {

if (region == null) {

region = new Region();

}

return region;

}

ProvinceCityBean provinceCityBean;

public int getVersion() {

return provinceCityBean == null ? 0 : provinceCityBean.getVersion();

}

public ProvinceCityBean getProvinceCityBean() {

return provinceCityBean;

}

public void setProvinceCityBean(final String mRegionJson, final Handler handler) {

if (TextUtils.isEmpty(mRegionJson)) {

return;

}

cachedThreadPool.execute(new Runnable() {

@Override

public void run() {

provinceCityBean = GsonUtils.GsonToBean(mRegionJson, ProvinceCityBean.class);

if (handler != null) {

handler.sendEmptyMessage(1);

}

}

});

}

ExecutorService cachedThreadPool;

CacheData cacheData;

private Region() {

cachedThreadPool = Executors.newCachedThreadPool();

cacheData = CacheData.getInstance().cacheName(Const.REGION_JSON);

}

/**

* 具體調用方法

*/

public void cacheRegion() {

if (provinceCityBean == null) {

readRegionFromFile();

} else {

readRegionFromHttp();

}

}

private void readRegionFromFile() {

cacheData.readDataFromFile(new Handler() {

@Override

public void handleMessage(Message msg) {

String jsonRegion = (String) msg.obj;

onReadRegionFromFileBack(jsonRegion);

}

});

}

/**

* 從文件中讀取數據,若為null 繼續從Asset中獲取

*

* @param jsonRegion

*/

private void onReadRegionFromFileBack(String jsonRegion) {

if (!TextUtils.isEmpty(jsonRegion)) {/*文件中讀取成功 設置到Region中更新json 取出version請求網絡判斷是否為最新的版本 */

setProvinceCityBean(jsonRegion, httpHandler);

} else {/*文件中讀取失敗 從assets 中繼續讀取*/

cacheData.readDataFromAssets(new Handler() {

@Override

public void handleMessage(Message msg) {

String jsonRegion = (String) msg.obj;

onReadRegionFromAssetsBack(jsonRegion);

}

});

}

}

private void onReadRegionFromAssetsBack(String jsonRegion) {

if (!TextUtils.isEmpty(jsonRegion)) {/*從assets中讀取成功 設置到Region中更新json*/

setProvinceCityBean(jsonRegion, httpHandler);

} else {

readRegionFromHttp();

}

}

private void readRegionFromHttp() {

ReqRegion reqRegion = new ReqRegion();

reqRegion.sendRequest(false, new OnHttpResult() {

@Override

public void onHttpSuccess(String data) {

setProvinceCityBean(data, null);

cacheData.writeData2FileWithThread(data);

}

@Override

public void onHttpFailure(String message) {

}

});

}

Handler httpHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

readRegionFromHttp();

}

};

}

Region.getInstance().cacheRegion();//實現緩存

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

總結

以上是生活随笔為你收集整理的android对象缓存,Android简单实现 缓存数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。