BucketList 0.1.2

BucketList 0.1.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2017年3月
SwiftSwift 版本3.0
SPM支持 SPM

Ben Bahrenburg 维护。



BucketList

需要缓存?注重安全性?BucketList 使得处理加密缓存变得容易。同时也支持标准键值缓存。

特性

  • 加密磁盘缓存
  • 内存缓存
  • 加密内存缓存[进行中的功能]

要求

  • Xcode 8.2 或更新
  • Swift 3.0
  • iOS 10 或更高

安装

LockedBucket 通过 CocoaPods 提供使用。要安装它,只需将以下行添加到您的 Podfile 中:

pod "BucketList"

手动

BucketList/Classes/ 目录中的所有 *.swift 文件复制到您的项目中。

使用

BucketList 中有六个主要类

  1. SecureBucket - 所有安全缓存提供者必须遵守的协议
  2. Bucket - 所有非安全缓存提供者必须遵守的协议
  3. EncryptedDiskCache - 此缓存提供者对放入缓存的所有对象进行加密并将值持久保存在磁盘上。
  4. EncryptedMemoryCache - 此缓存提供者对放入缓存的所有对象进行加密,并且将加密项存储在 NSCache 集合中。
  5. MemoryCache - 您的标准 NSCache 支持的缓存
  6. DictionaryCache - 使用 NSMutableDictionary 的一种老式缓存方法

示例

所有缓存提供者都有一个类似的 API,唯一的区别是安全协议中使用的密钥字段。

将项目存储到缓存

BucketList 使得将项目存储到缓存变得很简单。以下是如何使用 EncryptedDiskCache 来做的示例。

//Let's create an instance of the EncryptedDiskCache provider
//The first things we need to do is create a caching name. This will be the folder the files are cached within.
let cache = EncryptedDiskCache(cacheName: "foo")

//Now let's add a image to cache
let myImage = .....
let result = cache.add(secret: "a password", forKey: "my-secret-image", image: myImage) 
print("Image was successfull added? \(result)")

//Now let's add a AnyObject to cache
let myObject = .....
let result = cache.add(secret: "a password", forKey: "my-secret-object", object: myObject) 
print("myObject was successfull added? \(result)")

//Now let's add a Data to cache
let myData = .....
let result = cache.add(secret: "a password", forKey: "my-secret-data", data: myData) 
print("myData was successfull added? \(result)")

从缓存获取项目

您可以从缓存中轻松地获取项目。以下是如何使用 EncryptedDiskCache 来做到这一点的示例。

//Let's create an instance of the EncryptedDiskCache provider
//The first things we need to do is create a caching name. This will be the folder the files are cached within.
let cache = EncryptedDiskCache(cacheName: "foo")

//Let's get our image from cache
let myImage = cache.getImage(secret: "a password", forKey: "my-secret-image") 

//Now let's get our AnyObject from cache
let myObject = cache.getObject(secret: "a password", forKey: "my-secret-object") 
print("myObject was successfull added? \(result)")

//Now let's get our Data from cache
let myData = cache.getData(secret: "a password", forKey: "my-secret-data") 

检查项目是否在缓存中

您可以使用 exist 函数轻松检查项目是否已在缓存中。以下是使用 EncryptedDiskCache 完成此操作的示例。

//Let's create an instance of the EncryptedDiskCache provider
//The first things we need to do is create a caching name. This will be the folder the files are cached within.
let cache = EncryptedDiskCache(cacheName: "foo")

let isThere = cache.exists(forKey: "my-secret-image")
print("Is my image already in cache? \(isThere)")

移除缓存项目

您还可以从缓存中移除项目。以下是使用 EncryptedDiskCache 完成此操作的示例。

//Let's create an instance of the EncryptedDiskCache provider
//The first things we need to do is create a caching name. This will be the folder the files are cached within.
let cache = EncryptedDiskCache(cacheName: "foo")

let wasRemoved = cache.remove(forKey: "my-secret-image")
print("Is my image was removed from cache? \(wasRemoved)")

清空缓存

您可以轻松地清空所有的缓存项目。以下是使用 EncryptedDiskCache 完成此操作的示例。

//Let's create an instance of the EncryptedDiskCache provider
//The first things we need to do is create a caching name. This will be the folder the files are cached within.
let cache = EncryptedDiskCache(cacheName: "foo")

let allRemoved = cache.empty()
print("All items removed from cache? \(allRemoved)")

重要注意事项

  • 生命周期:默认情况下,所有的缓存提供者在 deinit 时都会清空它们的缓存项目。
  • 持久性:所有提供者都使用 iOS 空闲内存或磁盘空间时可以被清空的缓存对象。不要假设缓存项始终存在,一定要验证!!!

依赖项

  • 所有加密提供者都使用 RNCryptor

作者

Ben Bahrenburg,@bencoding

许可证

LockedBucket 在 MIT 许可证下可用。请参阅 LICENSE 文件以获取更多信息。