Need caching? Focused on security? BucketList makes working with encrypted caching easy. Also supports stand your standard key value caching as well.
- Encrypted Disk Caching
- In Memory Caching
- Encrypted In Memory Caching [feature in process]
- Xcode 8.2 or newer
- Swift 3.0
- iOS 10 or greater
LockedBucket is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "BucketList"
Carthage
github "benbahrenburg/BucketList"
Manually
Copy all *.swift
files contained in BucketList/Classes/
directory into your project.
There are six main classes in BucketList:
- SecureBucket - The protocol all secure cache providers must conform
- Bucket - The protocol all non secure cache providers must conform
- EncryptedDiskCache - This Cache provider encrypts all objects you place into cache and persists the values to disk.
- EncryptedMemoryCache - This Cache provider encrypts all objects you place into cache and stores the encrypted items in an NSCache collection.
- MemoryCache - Your standard NSCache backed Caching
- DictionaryCache - An old school approach to caching using NSMutableDictionary
All of the Caching providers have a similar API, with the only difference being the secret field used in the secure protocols.
BucketList makes it easy to save items into cache. Below is an example on how to do this using the 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)")
You can easily get items from cache. Below is an example on how to do this using the 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")
You can easily check if items are already in cache using the exist function. Below is an example on how to do this using the 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)")
You can also remove items from cache. Below is an example on how to do this using the 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)")
You can empty all of your cache items easily. Below is an example on how to do this using the 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)")
- Lifecycle: By default all of the cache providers empty their cache items on deinit.
- Durability: All of the providers use cache objects that can be drained by iOS if your app starts to run low on memory or diskspace. Don't assume a cache item will be there, always verify!!!
- All of the encrypted providers use RNCryptor.
Ben Bahrenburg, @bencoding
LockedBucket is available under the MIT license. See the LICENSE file for more details.