🔥android缓存管理器,分为内存缓存和文件缓存两种 先取内存数据,没有再从文件缓存中取
android缓存管理器,分为两级缓存:内存缓存和文件缓存;先取内存数据,没有再从文件缓存中获取
modifications by mohapps:
To use AES encryption:
// Initialize
CacheUtilConfig cc = CacheUtilConfig.builder(context)
.setIEncryptStrategy(new KeyStoreEncryptStrategy(context, "cacheUtil"))
.allowMemoryCache(true)
.allowEncrypt(false)
.build();
CacheUtil.init(cc);
// Save value
CacheUtil.put(key, value, isEncrypt);
// Read value
CacheUtil.get(key, isEncrypt);
CacheUtilConfig cc = CacheUtilConfig.builder(getApplication())
.allowMemoryCache(true)//是否允许保存到内存
.allowEncrypt(false)//是否允许加密
.allowKeyEncrypt(true)//是否允许Key加密
.preventPowerDelete(true)//强力防止删除,将缓存数据存储在app数据库目录下的cachemanage文件夹下
.setACache(ACache.get(file1))//自定义ACache,file1为缓存自定义存储文件夹,设置该项,preventPowerDelete失效
.setAlias("")//默认KeyStore加密算法私钥,建议设置.自定义加密算法,该功能失效
.setIEncryptStrategy(
new Des3EncryptStrategy(MainActivity.this, "WLIJkjdsfIlI789sd87dnu==",
"haohaoha"))//自定义des3加密
.build();
CacheUtil.init(cc);//初始化,必须调用
默认 | 状态 |
---|---|
默认内存缓存 | 缓存 |
默认Key是否加密 | 加密 |
默认value是否加密 | 加密 |
默认加密算法 | keystore加密 |
默认加密私钥 | 包名 |
强力防止删除 | 否,需设置 |
CacheUtil.put("key1", "测试数据1");//默认加密
CacheUtil.put("key2", "测试数据2", true);//true代表加密存储
CacheUtil.put("key3", "~!@#$%^&*()_+{}[];':,.<>`");//特殊字符串测试
CacheUtil.put("key4", "~!@#$%^&*()_+{}[];':,.<>`", true);//加密特殊字符串测试
CacheUtil.put("key5", new Test(1, "2"));//实体对象测试
CacheUtil.put("key6", new Test(1, "2"), true);//加密实体对象测试
CacheUtil.put("key7", jsonObject);//jsonObject对象测试
CacheUtil.put("key8", jsonObject, true);//加密jsonObject对象测试
CacheUtil.put("key9", jsonArray);//jsonArray对象测试
CacheUtil.put("key10", jsonArray, true);//加密jsonArray对象测试
CacheUtil.put("key11", 1);//jsonArray对象测试
CacheUtil.put("key12", 1, true);//加密jsonArray对象测试
CacheUtil.put("key13", "测试数据1", 5);//保存数据5秒
CacheUtil.put("key14", new Test(1, "2"), 5);//保存对象数据5秒
CacheUtil.put("key15", "测试数据1", 5, true);//加密保存数据5秒
CacheUtil.put("key16", new Test(1, "2"), 5, true);//加密保存对象数据5秒
CacheUtil.put(CacheUtil.translateKey("key17"), "123456", true);//key加密
CacheUtil.put("key18", "测试数据18", false);//false代表不加密存储
String key1Value = CacheUtil.get("key1");
String key2Value = CacheUtil.get("key2", true);
String key3Value = CacheUtil.get("key3");
String key4Value = CacheUtil.get("key4", true);
Test key5Test = CacheUtil.get("key5", Test.class);//可能为null
String key5Value = key5Test == null ? "" : key5Test.toString();
Test key6Test = CacheUtil.get("key6", Test.class, true);
String key6Value = key6Test == null ? "" : key6Test.toString();
String key7Value = CacheUtil.get("key7");
String key8Value = CacheUtil.get("key8", true);
String key9Value = CacheUtil.get("key9");
String key10Value = CacheUtil.get("key10", true);
String key11Value = CacheUtil.get("key11");
String key12Value = CacheUtil.get("key12", true);
String key13Value = CacheUtil.get("key13");
Test key14Test = CacheUtil.get("key14", Test.class);
String key14Value = key14Test == null ? "" : key14Test.toString();
String key15Value = CacheUtil.get("key15", true);
Test key16Test = CacheUtil.get("key16", Test.class, true);
String key16Value = key16Test == null ? "" : key16Test.toString();
String key17Value = CacheUtil.get(CacheUtil.translateKey("key17"), true);
String key18Value = CacheUtil.get("key18", false);
Boolean key19Value =
CacheUtil.get(CacheUtil.translateKey("null"), Boolean.class, false, true);
//key19未存储的数据,返回默认值
Test key20Value = CacheUtil.get(CacheUtil.translateKey("null1"), Test.class,
new Test(1, "默认值"), true);
//key20未存储的数据,返回默认值
Test key21Value = CacheUtil.get(CacheUtil.translateKey("null1"), Test.class, true);
//key21未存储的数据,且无默认构造方法,返回null
CacheUtil.get("要查找的key")
CacheUtil.get("要查找的key", 是否加密)
CacheUtil.get("要查找的key",对应的实体对象)
CacheUtil.get("要查找的key",对应的实体对象, 是否加密)
CacheUtil.get("要查找的key",对应的实体对象, 错误情况下返回默认数据)
CacheUtil.get("要查找的key",对应的实体对象, 错误情况下返回默认数据,是否加密)
CacheUtil.clearMemory("key1");//指定key内存缓存删除
CacheUtil.clear("key8");//指定key内存缓存和文件缓存都删除
CacheUtil.clearAllMemory();//所有内存缓存删除
CacheUtil.clearAll();//所有内存缓存和文件缓存都删除
CacheObserver.getInstance().addObserver("key1", new IDataChangeListener() {
@Override
public void onDataChange(String str) {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
//String 转list
List<User> userList = gson.fromJson(string, new TypeToken<List<User>>() {}.getType());
//List转String
String string = new Gson().toJson(userList);
在根 build.gradle中添加
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
在项目build.gradle中添加
dependencies {
compile 'com.github.ronghao:CacheManage:1.3.6'
}
+ 添加keystore判空操作
+ 解决clear方法执行错误的问题
+ 提高底层加密算法、增加安全性
+ 修改默认缓存位置,防止被清理
+ 添加clearAll()和clearAllMemory()方法
Copyright 2016 haohaohu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.