SwiftlyCache 1.1.8

SwiftlyCache 1.1.8

hlc 维护。



  • by
  • LC

SwiftlyCache

Build Status

Swiftlycache 是一个线程安全的 iOS 通用缓存库,使用 swift 5 编写。

功能

  • 支持所有符合 codable 协议的数据类型
  • 可以使用最近最少使用算法移除对象
  • 可以配置为在接收到内存警告或应用进入后台时自动回收对象或手动回收对象
  • 使用下标可以使得读取和写入数据更方便
  • 支持使用序列生成器读取数据

安装

CocoaPods

1. 在您的 Podfile 中添加 pod 'SwiftlyCache'
2. 运行 pod installpod update
3. 导入 SwiftlyCache

手动安装

1. 下载 SwiftlyCache 子目录下的所有文件
2. 将源文件添加到您的 Xcode 项目中

示例

缓存符合codable协议的结构体

struct Student:Codable {
     var name:String
     var age:Int
     
     init(name:String,age:Int) {
         self.name = name
         self.age = age
     }
 }
let cache = MultiCache<Student>()

let shirley = Student(name: "shirley", age: 30)

设置要缓存的keyvalue

cache.set(forKey: "shirley10", value: shirley)

根据给定的key查询对应的值

if let object = cache.object(forKey: "shirley1"){
    print("当前Student是:\(object)")
}

根据key查询对应的value是否存在于缓存中


let isExists = cache.isExistsObjectForKey(forKey: "shirley20")

查看swiftlycachedemo以获取更多测试代码和案例



中文介绍

SwiftlyCache是用 Swift 5编写的一个线程安全的iOS通用缓存库。

特性

  • 支持所有遵守 Codable协议的数据类型
  • 支持LRU淘汰算法
  • 当收到内存警告或者App进入后台时,内存缓存可以配置为自动清空或者手动清空
  • 支持使用 Subscript,使读写数据更加方便
  • 提供了 MultiCacheGennerator、 MemoryCacheGenerator、 DiskCacheGenerator用于支持 for..in、 compactMap、 map、 filter等方法

使用方法

CocoaPods

1.在Podfile中添加 pod 'SwiftlyCache'
2.执行 pod install或者 pod update
3.导入 SwiftlyCache

手动导入

1.下载 SwiftlyCache文件夹内所有内容
2.将 SwiftlyCache内的源文件添加到你的工程

示例

将遵循Codable协议的struct进行缓存

struct Student:Codable {
     var name:String
     var age:Int
     
     init(name:String,age:Int) {
         self.name = name
         self.age = age
     }
 }
let cache = MultiCache<Student>()

let shirley = Student(name: "shirley", age: 30)

设置需要缓存的KeyValue

cache.set(forKey: "shirley10", value: shirley)

根据给定Key查询对应的Value

if let object = cache.object(forKey: "shirley1"){
    print("当前Student是:\(object)")
}

根据Key查询缓存中是否存在对应的Value


let isExists = cache.isExistsObjectForKey(forKey: "shirley20")

更多测试代码和用例见 SwiftlyCacheDemo

相关链接

SwiftlyCache实现