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) { 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 { init {
try { try {
@ -48,6 +90,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @param value 保存的String数据 * @param value 保存的String数据
*/ */
fun put(key: String, value: String) { fun put(key: String, value: String) {
mCache?.let { mCache ->
try { try {
val file = mCache.newFile(key) val file = mCache.newFile(key)
file.writeText(value) file.writeText(value)
@ -56,6 +99,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
e.printStackTrace() e.printStackTrace()
} }
} }
}
/** /**
* 保存 String数据 缓存中 * 保存 String数据 缓存中
@ -74,25 +118,27 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return String 数据 * @return String 数据
*/ */
fun getAsString(key: String): String? { fun getAsString(key: String): String? {
mCache?.let { mCache ->
val file = mCache[key] val file = mCache[key]
if (!file.exists()) if (!file.exists())
return null return null
var removeFile = false var removeFile = false
return try { try {
val text = file.readText() val text = file.readText()
if (!Utils.isDue(text)) { if (!Utils.isDue(text)) {
Utils.clearDateInfo(text) return Utils.clearDateInfo(text)
} else { } else {
removeFile = true removeFile = true
null
} }
} catch (e: IOException) { } catch (e: IOException) {
null e.printStackTrace()
} finally { } finally {
if (removeFile) if (removeFile)
remove(key) remove(key)
} }
} }
return null
}
// ======================================= // =======================================
// ========== JSONObject 数据 读写 ========= // ========== JSONObject 数据 读写 =========
@ -184,10 +230,12 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @param value 保存的数据 * @param value 保存的数据
*/ */
fun put(key: String, value: ByteArray) { fun put(key: String, value: ByteArray) {
mCache?.let { mCache ->
val file = mCache.newFile(key) val file = mCache.newFile(key)
file.writeBytes(value) file.writeBytes(value)
mCache.put(file) mCache.put(file)
} }
}
/** /**
* 保存 byte数据 缓存中 * 保存 byte数据 缓存中
@ -206,6 +254,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return byte 数据 * @return byte 数据
*/ */
fun getAsBinary(key: String): ByteArray? { fun getAsBinary(key: String): ByteArray? {
mCache?.let { mCache ->
var removeFile = false var removeFile = false
try { try {
val file = mCache[key] val file = mCache[key]
@ -221,12 +270,13 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
return null
} finally { } finally {
if (removeFile) if (removeFile)
remove(key) remove(key)
} }
} }
return null
}
/** /**
* 保存 Serializable数据到 缓存中 * 保存 Serializable数据到 缓存中
@ -269,7 +319,6 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
return ois.readObject() return ois.readObject()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
return null
} finally { } finally {
try { try {
bais?.close() bais?.close()
@ -371,6 +420,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return value 缓存的文件 * @return value 缓存的文件
*/ */
fun file(key: String): File? { fun file(key: String): File? {
mCache?.let { mCache ->
try { try {
val f = mCache.newFile(key) val f = mCache.newFile(key)
if (f.exists()) { if (f.exists()) {
@ -379,7 +429,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
}
return null return null
} }
@ -389,14 +439,14 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @return 是否移除成功 * @return 是否移除成功
*/ */
fun remove(key: String): Boolean { fun remove(key: String): Boolean {
return mCache.remove(key) return mCache?.remove(key) == true
} }
/** /**
* 清除所有数据 * 清除所有数据
*/ */
fun clear() { 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