SwiftCacher is a Swift package that provides a simple key-value caching mechanism with persistence. It allows you to store objects in a cache directory on disk, making them easily retrievable for future use.
- Create a cache directory on disk if it doesn't exist.
- Save objects to the cache using secure coding and serialization.
- Retrieve objects from the cache.
- Remove specific objects from the cache.
- Remove all objects from the cache.
- Remove expired objects from the cache.
- iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+
- Xcode 11.0+
- Swift 5.0+
You can integrate SwiftCacher into your Swift project using Swift Package Manager.
- Open your project in Xcode.
- Select your project in the Project Navigator.
- Select the "Swift Packages" tab.
- Click the "+" button to add a package dependency.
- Enter the URL of this repository: https://github.com/stremovskyy/SwiftCacher.git
- Choose the desired version or branch.
- Click "Next" and follow the Xcode instructions to complete the installation.
import SwiftCacher
// Create an instance of the cache
let cache = try SwiftCacher()
// Store an object in the cache
let objectToCache = MyObject(name: "John Doe")
try cache.setObject(objectToCache, forKey: "user")
// Retrieve the object from the cache
do {
if let cachedObject: MyObject = try cache.getObject(forKey: "user") {
print("Cached object: \(cachedObject)")
} else {
print("Object not found in the cache.")
}
} catch {
print("Failed to retrieve cached object: \(error)")
}
// Remove the object from the cache
do {
try cache.removeObject(forKey: "user")
} catch {
print("Failed to remove object from cache: \(error)")
}
// Remove all objects from the cache
do {
try cache.removeAllObjects()
} catch {
print("Failed to remove all objects from cache: \(error)")
}
// Remove expired objects from the cache
do {
try cache.removeExpired()
} catch {
print("Failed to remove expired objects from cache: \(error)")
}
- SwiftCacher uses the
NSKeyedArchiver
andNSKeyedUnarchiver
classes to serialize and deserialize objects.
This means that all objects that you want to store in the cache must conform to the
NSSecureCoding
protocol. For more information, see Archives and Serializations Programming Guide and NSSecureCoding.
In other words, you need to implement the following methods in your object:
class MyObject: NSObject, NSSecureCoding {
static var supportsSecureCoding: Bool = true
func encode(with coder: NSCoder) {
// Encode the object
}
required init?(coder: NSCoder) {
// Decode the object
}
}
- SwiftCacher uses the
FileManager
class to create a cache directory on disk.
This means that you need to add the
NSFileProtectionKey
key to your app'sInfo.plist
file to protect the cache directory with data protection. For more information, see Data Protection.
- Do not use
coder.decodeObject(forKey:)
to decode objects from the cache. Instead, usecache.getObject(forKey:)
to retrieve objects from the cache.
This is because
coder.decodeObject(forKey:)
returns an optional object, which can benil
. If you try to decode anil
object, you will get anil
object. This is not what you want. Instead, you should usecache.getObject(forKey:)
to retrieve objects from the cache. This method returns an optional object, which can benil
. If you try to retrieve anil
object, you will get anil
object. This is what you want.
SwiftCacher is released under the MIT license. See LICENSE for details.
Contributions are welcome! Please see CONTRIBUTING for details. If you have any ideas, suggestions, or bug reports, please open an issue or submit a pull request.
SwiftCacher is inspired by the need for a simple and efficient caching mechanism in Swift projects. It aims to provide an easy-to-use solution for storing and retrieving objects with persistence.