android实现二级缓存,Android带过期时间的可加密二级缓存(LruCache+DiskLruCache)

android缓存管理器,分为两级缓存:内存缓存和文件缓存;先取内存数据,没有再从文件缓存中获取

特点

二级缓存

内存缓存(采用LruCache实现)

文件缓存(由DiskLruCache实现)

默认使用SD卡缓存

getCacheDir()获取的缓存文件较容易被删除

大于1M的缓存文件,google建议使用getExternalCacheDir()缓存存储

默认存储位置为app数据缓存位置

为处理防止被删除,在数据库文件夹下创建ACache文件夹,数据存储在该文件夹下

支持文件加密存储

默认Rsa加密内容

默认生成Rsa唯一密钥(建议使用默认生成的密钥)

默认生成的默认密钥,每个客户端都是唯一的,互不相同

默认密钥存储在KeyStore中,防逆向工程获取密钥

支持基本数据类型、String、JSONObject、JSONArray、实体对象(Test类)

不支持数据类型可转换成String存储

缓存数据可设置过期时间,到期自动销毁

允许内存缓存

key值加密

对应的本地缓存文件也加密

添加数据监控机制

支持自定义加密算法

使用方法

//初始化

ACache.init(

encryptStrategy = RsaEncrypt.getInstance(this@MainActivity),

cachePath = cacheDir.absolutePath + "/ACache",

encrypt = true

)

//存数据

ACache.getCache().putString("key1", "测试数据1")

//取数据

val key1Value = ACache.getCache().getString("key1")

具体方法见Demo

项目添加方法

暂时只支持源码依赖。

特别注意

关于

个人博客:简书

如果你也喜欢这个库,Star一下吧,欢迎Fork

参考:

License

Copyright 2016 Zhiqiang

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值