-
Notifications
You must be signed in to change notification settings - Fork 3
cache v0.1.0
DovSnier edited this page Jul 22, 2019
·
1 revision
一个简单易用的cache 库,此版本的cache 支持多实例模式.
- 简单
- 实用
- 方便
- 多实例
implementation 'com.dvsnier:cache:0.1.0'
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
// 在application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
CacheManager.getInstance().initialize(this);
...
}
@Override
public void onTerminate() {
super.onTerminate();
CacheManager.getInstance().close();
}
// 在application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
// 默认磁盘配置512M 缓存空间
CacheManager.getInstance().initialize(this);
...
}
// 在application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
// 自定义磁盘1G 缓存空间
int cacheMaxSizeOfDisk = 1024 * 1024 * 1024; // 1G
CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
// CacheManager.getInstance().initialize(new ICacheConfig.Builder(this).setAppVersion(getAppVersionCode(this)).setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk).create());
...
}
public static int getAppVersionCode(Context context) {
int versionCode = 1;
PackageManager pm = context.getPackageManager();
try {
PackageInfo packInfo = pm.getPackageInfo(context.getPackageName(), 0);
versionCode = packInfo.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
// 在application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
// 自定义磁盘1G 缓存空间
int cacheMaxSizeOfDisk = Double.valueOf(CacheStorage.INSTANCE().getFormatted(1, AbstractStorage.SCU.G)).intValue(); // 1G < Integer.MAX_VALUE ~ 2G
// the configure cache alias, here in the downloads directory
CacheManager.getInstance().initialize(IType.TYPE_DOWNLOADS, new ICacheConfig.Builder(this)
.setContext(this)
.setAppVersion(getAppVersionCode(this))
.setCacheMaxSizeOfDisk(cacheMaxSizeOfDisk)
// .setUniqueName(IType.TYPE_DEFAULT)
.setUniqueName(IType.TYPE_DOWNLOADS)
.setDebug(true) // debug mode
.create());
...
}
public static int getAppVersionCode(Context context) {
int versionCode = 1;
PackageManager pm = context.getPackageManager();
try {
PackageInfo packInfo = pm.getPackageInfo(context.getPackageName(), 0);
versionCode = packInfo.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
CacheManager.getInstance().put(key0, "测试数据: " + System.currentTimeMillis())
.putString(key1, "测试字符串: " + view.toString())
.putObject(key2, new Bean("cache object " + System.currentTimeMillis(), BuildConfig.VERSION_NAME))
.putString(getKey(), view.toString())
.commit();
CacheManager.getInstance().put(IType.TYPE_DOWNLOADS, getKey(), getValue())
.commit(IType.TYPE_DOWNLOADS);
- 默认缓存文件保存目录为:
/mnt/sdcard/Android/data/package_your_name/cache/
- 配置缓存(IType.TYPE_DOWNLOADS)文件保存目录为:
/mnt/sdcard/Android/data/package_your_name/cache/downloads
, 如果配置不符合规范,则提供的默认配置为:/mnt/sdcard/Android/data/package_your_name/cache/local
0.0.6
版本的Cache SDK 和0.1.0
版本的Cache SDK 不兼容,如需简单使用缓存请使用v0.0.6
- Email: [email protected]
- 有任何建议或者使用中遇到问题都可以给我发邮件,欢迎技术交流QQ:578562841