【版权说明】
1. 请支持原创,转载请注明出处:https://blog.csdn.net/cbk861110/article/details/86665564
2. 项目源码&框架说明&技术更新实现 请移步:https://github.com/caobaokang419/WeatherApp(欢迎Github Fork&Star,框架和技术实现不妥之处,请帮忙指正),谢谢!
-------------------------------------------
基于MVVM框架的高德天气APP:
功能点实现说明:
- 数据缓存功能:封装DiskLruCache,实现磁盘缓存网络下载的图片&文本&Json及其他格式数据,改善用户体验和性能;
一、 技术背景:
1. DiskLruCache 磁盘缓存Android已公开支持,并公开相关源码:
2. 磁盘缓存技术,xUtils框架中已嵌套支持,此处单独实现,便于轻量化的使用和定制;
3. 版权说明:参考资料:https://github.com/hongyangAndroid/base-diskcache
二、技术实现:
1. DiskLruCache代理类DiskLruCacheProxy:
备注:代理模式作用:封装使用,便于用户跳过技术细节,掌握使用DiskLruCache功能open apis。
public class DiskLruCacheProxy {
private static final String TAG = "DiskLruCacheProxy";
private static final String DIR_NAME = "diskCache";
private static final int MAX_COUNT = 5 * 1024 * 1024;
private DiskLruCache mDiskLruCache;
public DiskLruCacheProxy(Context context) {
mDiskLruCache = generateCache(context, DIR_NAME, MAX_COUNT);
}
private DiskLruCache generateCache(Context context, File dir, int maxCount) {
/**
* 构造方法不建议throw exception
*/
if (!dir.exists() || !dir.isDirectory()) {
/*throw new IllegalArgumentException(
dir + " is not a directory or does not exists. ");*/
return null;
}
int appVersion = context == null ? DEFAULT_APP_VERSION : WtUtil.getAppVersion(context);
try {
return DiskLruCache.open(
dir,
appVersion,
DEFAULT_VALUE_COUNT,
maxCount);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取缓存文件路径
*/
private File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
/**
* 获取DiskLruCache.Editor
*/
public DiskLruCache.Editor editor(String key) {
try {
DiskLruCache.Editor edit = mDiskLruCache.edit(key);
if (edit == null) {
Log.w(TAG, "the entry spcified key:" + key + " is editing by other . ");
}
return edit;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取DiskLruCache.Snapshot
*/
public InputStream get(String key) {
try {
DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
if (snapshot == null) {
Log.e(TAG, "not find entry , or entry.readable = false");
return null;
}
//write READ
return snapshot.getInputStream(0);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 移除缓存项
*/
public boolean remove(String key) {
try {
return mDiskLruCache.remove(key);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 关闭缓存
*/
public void close() {
try {
mDiskLruCache.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除缓存
*/
public void delete() {
try {
mDiskLruCache.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 刷新缓存
*/
public void flush() throws IOException {
mDiskLruCache.flush();
}
/**
* 判断缓存是否关闭
*/
public boolean isClosed() {
return mDiskLruCache.isClosed();
}
/**
* 获取缓存大小
*/
public long size() {
return mDiskLruCache.size();
}
/**
* 设置缓存的最大容量
*/
public void setMaxSize(long maxSize) {
mDiskLruCache.setMaxSize(maxSize);
}
/**
* 获取缓存的最大容量
*/
public long getMaxSize() {
return mDiskLruCache.getMaxSize();
}
/**
* 设置缓存的目录
*/
public File getDirectory() {
return mDiskLruCache.getDirectory();
}
}
2. DiskLruCache使用方法DiskLruCacheClient.java:
/*用戶ID缓存*/
public void saveCacheUserId(String key,String userid) {
DiskLruCacheClient.getInstant().putKeyAndStringValue(key,userid);
}
public String getCacheUserId(String key) {
return DiskLruCacheClient.getInstant().getStringValueByKey(key);
}
/*用戶头像缓存*/
public void saveCacheUserPhoto(String key,Drawable photo) {
DiskLruCacheClient.getInstant().putKeyAndDrawable(key,photo);
}
public Drawable getCacheUserPhoto(String key) {
return DiskLruCacheClient.getInstant().getDrawableByKey(key);
}
-------------------------------------------
文章目录(未完,待续):
一:android 应用子功能及移动框架总述 https://blog.csdn.net/cbk861110/article/details/86665564
二:android 高德天气API说明及城市天气查询实现 https://blog.csdn.net/cbk861110/article/details/86665655
三:android 自定义控件实现(ActionBar + PageIndicatorView) https://blog.csdn.net/cbk861110/article/details/86665790
四:android ViewPager实现左右页面滑动切换 https://blog.csdn.net/cbk861110/article/details/86665964
五:android应用权限动态申请 https://blog.csdn.net/cbk861110/article/details/86666321
六:android RecyclerView 封装及使用 https://blog.csdn.net/cbk861110/article/details/86666392
七:android Xutils3文件下载实现(高德天气城市配置) https://blog.csdn.net/cbk861110/article/details/86666573
八:android DiskLruCache 磁盘缓存 封装和使用 https://blog.csdn.net/cbk861110/article/details/86666664
九:android ThreadPoolExecutor线程池 封装及使用 https://blog.csdn.net/cbk861110/article/details/86667101
十:android 天气网络请求框架(retrofit2&okhttp3&Gson) 封装及使用 https://blog.csdn.net/cbk861110/article/details/86667375
十一:android RxAndroid(响应式编程) 异步网络请求实现 https://blog.csdn.net/cbk861110/article/details/86669178
十二:android DataBinding 数据和UI双向绑定实现 https://blog.csdn.net/cbk861110/article/details/86669708
十三:android room数据库 天气数据读写实现 https://blog.csdn.net/cbk861110/article/details/86670354
十四:android LiveData 使用方法(实现城市天气自动刷新) https://blog.csdn.net/cbk861110/article/details/86670531
十五:android ViewModel 使用方法 https://blog.csdn.net/cbk861110/article/details/86670703
十六:android 集成友盟消息推送机制(U-Push) https://blog.csdn.net/cbk861110/article/details/86683849
-------------------------------------------
【版权说明】
1. 请支持原创,转载请注明出处:https://blog.csdn.net/cbk861110/article/details/86665564
2. 项目源码&框架说明&技术更新实现 请移步:https://github.com/caobaokang419/WeatherApp(欢迎Github Fork&Star,框架和技术实现不妥之处,请帮忙指正),谢谢!