pull/36/head
kunfei 5 years ago
parent fe8c4e02e5
commit 4227b8ccc7
  1. 110
      app/src/main/java/io/legado/app/utils/ACache.kt

@ -23,7 +23,49 @@ import kotlin.math.min
* 本地缓存
*/
class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) {
private lateinit var mCache: ACacheManager
companion object {
const val TIME_HOUR = 60 * 60
const val TIME_DAY = TIME_HOUR * 24
private const val MAX_SIZE = 1000 * 1000 * 50 // 50 mb
private const val MAX_COUNT = Integer.MAX_VALUE // 不限制存放数据的数量
private val mInstanceMap = HashMap<String, ACache>()
@JvmOverloads
fun get(
ctx: Context,
cacheName: String = "ACache",
maxSize: Long = MAX_SIZE.toLong(),
maxCount: Int = MAX_COUNT,
cacheDir: Boolean = true
): ACache {
val f = if (cacheDir) File(ctx.cacheDir, cacheName) else File(ctx.filesDir, cacheName)
return get(f, maxSize, maxCount)
}
@JvmOverloads
fun get(
cacheDir: File,
maxSize: Long = MAX_SIZE.toLong(),
maxCount: Int = MAX_COUNT
): ACache {
synchronized(this) {
var manager = mInstanceMap[cacheDir.absoluteFile.toString() + myPid()]
if (manager == null) {
manager = ACache(cacheDir, maxSize, maxCount)
mInstanceMap[cacheDir.absolutePath + myPid()] = manager
}
return manager
}
}
private fun myPid(): String {
return "_" + android.os.Process.myPid()
}
}
private var mCache: ACacheManager? = null
init {
try {
@ -48,6 +90,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @param value 保存的String数据
*/
fun put(key: String, value: String) {
mCache?.let { mCache ->
try {
val file = mCache.newFile(key)
file.writeText(value)
@ -56,6 +99,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
e.printStackTrace()
}
}
}
/**
* 保存 String数据 缓存中
@ -74,25 +118,27 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return String 数据
*/
fun getAsString(key: String): String? {
mCache?.let { mCache ->
val file = mCache[key]
if (!file.exists())
return null
var removeFile = false
return try {
try {
val text = file.readText()
if (!Utils.isDue(text)) {
Utils.clearDateInfo(text)
return Utils.clearDateInfo(text)
} else {
removeFile = true
null
}
} catch (e: IOException) {
null
e.printStackTrace()
} finally {
if (removeFile)
remove(key)
}
}
return null
}
// =======================================
// ========== JSONObject 数据 读写 =========
@ -184,10 +230,12 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @param value 保存的数据
*/
fun put(key: String, value: ByteArray) {
mCache?.let { mCache ->
val file = mCache.newFile(key)
file.writeBytes(value)
mCache.put(file)
}
}
/**
* 保存 byte数据 缓存中
@ -206,6 +254,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return byte 数据
*/
fun getAsBinary(key: String): ByteArray? {
mCache?.let { mCache ->
var removeFile = false
try {
val file = mCache[key]
@ -221,12 +270,13 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
}
} catch (e: Exception) {
e.printStackTrace()
return null
} finally {
if (removeFile)
remove(key)
}
}
return null
}
/**
* 保存 Serializable数据到 缓存中
@ -269,7 +319,6 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
return ois.readObject()
} catch (e: Exception) {
e.printStackTrace()
return null
} finally {
try {
bais?.close()
@ -371,6 +420,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return value 缓存的文件
*/
fun file(key: String): File? {
mCache?.let { mCache ->
try {
val f = mCache.newFile(key)
if (f.exists()) {
@ -379,7 +429,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
} catch (e: Exception) {
e.printStackTrace()
}
}
return null
}
@ -389,14 +439,14 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return 是否移除成功
*/
fun remove(key: String): Boolean {
return mCache.remove(key)
return mCache?.remove(key) == true
}
/**
* 清除所有数据
*/
fun clear() {
mCache.clear()
mCache?.clear()
}
/**
@ -726,44 +776,4 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
}
}
companion object {
const val TIME_HOUR = 60 * 60
const val TIME_DAY = TIME_HOUR * 24
private const val MAX_SIZE = 1000 * 1000 * 50 // 50 mb
private const val MAX_COUNT = Integer.MAX_VALUE // 不限制存放数据的数量
private val mInstanceMap = HashMap<String, ACache>()
@JvmOverloads
fun get(
ctx: Context,
cacheName: String = "ACache",
maxSize: Long = MAX_SIZE.toLong(),
maxCount: Int = MAX_COUNT,
cacheDir: Boolean = true
): ACache {
val f = if (cacheDir) File(ctx.cacheDir, cacheName) else File(ctx.filesDir, cacheName)
return get(f, maxSize, maxCount)
}
@JvmOverloads
fun get(
cacheDir: File,
maxSize: Long = MAX_SIZE.toLong(),
maxCount: Int = MAX_COUNT
): ACache {
synchronized(this) {
var manager = mInstanceMap[cacheDir.absoluteFile.toString() + myPid()]
if (manager == null) {
manager = ACache(cacheDir, maxSize, maxCount)
mInstanceMap[cacheDir.absolutePath + myPid()] = manager
}
return manager
}
}
private fun myPid(): String {
return "_" + android.os.Process.myPid()
}
}
}
Loading…
Cancel
Save