MemoryJar
MemoryJar 是一个快速、高效且线程安全的持久字符串缓存库,包括容量管理(LRU)和基于年龄过期的支持。它利用内存和磁盘存储,支持异步写入以提高速度。此库受到了 Parse iOS SDK 中缓存机制的启发。
此缓存库最适合在构建用于管理 REST API 的缓存系统时使用。
安装
要安装它,只需将以下行添加到您的 Podfile 中:
pod "MemoryJar"
使用方法
import MemoryJar
// use shared, or create your own with MemoryJar()
let cache = MemoryJar.shared
// Simple
cache["company"] = "Modernistik"
// retrieve (no expiration)
let company = cache["company"]
// Some API response
let json = """
{
"name" : "Anthony Persaud",
"id" : 7,
"company" : {
"name" : "Modernistik",
"location": "San Diego, CA"
}
}
"""
let cacheKey = "https://some.api/?id=7"
// set the value
cache.set(value: json, forKey: cacheKey)
// fetch value only if it is not older than 1 day.
if let result = cache.get(forKey: cacheKey, maxAge: 86400) {
print(result)
}
// deletes all cache objects
cache.removeAllObjects()
待办事项
- 支持清除匹配特定 URL 路径模式的具体 URL。
作者
Anthony Persaud, https://www.modernistik.com
许可证
MemoryJar 可在 MIT 许可证下获得。更多信息请参阅 LICENSE 文件。