KituraCache
是一个内存中的、线程安全的缓存,允许您使用唯一的、Hashable 键来存储对象。
最新版本的 KituraCache 需要 Swift 4.0,但建议使用 4.1.2 或更高版本。您可以按照此链接下载此版本的 Swift 二进制文件。 不保证与其他 Swift 版本的兼容性。
将 Kitura-Cache
包添加到应用程序 Package.swift
文件中的依赖项。 使用最新的 Kitura-Cache
发行版替换 "x.x.x"
。
.package(url: "https://github.com/Kitura/Kitura-Cache.git", from: "x.x.x")
将 KituraCache
添加到你的 target 的依赖项
.target(name: "example", dependencies: ["KituraCache"]),
import KituraCache
要使用 KituraCache,请按照上述定义添加依赖项并导入包,然后初始化
let cache = KituraCache()
如果不提供任何参数,则默认缓存将不会过期,并且每 10 分钟检查一次以确定是否需要删除任何条目。
要向缓存添加条目,或者如果键已存在则更新条目
在以下示例中,item 是具有整数 id 字段的 struct
。
cache.setObject(item, forKey: item.id)
要从缓存中检索条目
let cache = KituraCache()
...
if let item = cache.object(forKey: 1) {
//Object with key of 1 retrieved from cache.
...
}
else {
//No object stored in cache with key of 1.
...
}
要删除单个条目,请将该条目的键作为参数传递
cache.removeObject(forKey: 1)
要重置缓存及其 Statistics
cache.flush()
有关更多信息,请访问我们的 API 参考。
我们喜欢讨论服务器端 Swift 和 Kitura。 加入我们的 Slack 来认识团队!
此库在 Apache 2.0 许可下获得许可。 完整的许可文本可在 LICENSE 中找到。