Getting Started
To begin using SPTPersistentCache, follow the steps below:
Step 1 – Installation
First, install the SPTPersistentCache Cocoapod:
- Open your terminal
- Navigate to your Xcode project directory
- Run the command:
pod init
- Open the generated Podfile using a text editor
- Add the following line under the target section:
pod 'SPTPersistentCache'
- Save and close the Podfile
- Run the command:
pod install
- Open the generated .xcworkspace file
Step 2 – Importing the Framework
In the source file where you want to use SPTPersistentCache, add the following import statement:
import SPTPersistentCache
Step 3 – Initializing the Cache
In order to start using the cache, you need to initialize an instance of SPTPersistentCache. You can do this by following the code example below:
// Define the cache directory path
let cachePath = /* Provide the path to your cache directory */
// Initialize the cache object
let cache = SPTPersistentCache(cacheDirectory: cachePath)
Optional Configuration Settings
Cache Size Limit
You can specify the maximum size that the cache can reach before it starts to evict items. Use the maxCacheSize
property to set the cache size limit:
cache.maxCacheSize = /* Your desired cache size in bytes */
Eviction Policy
You can set the eviction policy to be used when the cache reaches its size limit. Use the cacheEvictionPolicy
property to choose between different eviction policies such as .fifo
(First In, First Out) or .lru
(Least Recently Used):
cache.cacheEvictionPolicy = /* Your desired eviction policy */
Cache Encryption
If you want to encrypt the cache data for added security, you can enable encryption by setting the isDataCacheEncrypted
property to true
:
cache.isDataCacheEncrypted = true
Cache Shredding
If you want to securely delete the cache data when the cache object is deallocated, you can enable cache shredding by setting the isDataCacheShreddable
property to true
:
cache.isDataCacheShreddable = true
Usage Guide
Storing Data
Use the storeItem(_:forKey:completionHandler:)
method to store data in the cache:
// Load your data into a Data object
let data = /* Your data */
// Store the data in the cache
cache.storeItem(data, forKey: "your-cache-key") { (cache, key, error) in
if let error = error {
// Error occurred while storing data
print("Error while storing data: \(error.localizedDescription)")
} else {
// Data stored successfully
print("Data stored successfully")
}
}
Reading Data
Use the getItem(forKey:completionHandler:)
method to read data from the cache:
// Read data from the cache
cache.getItem(forKey: "your-cache-key") { (cache, key, data, error) in
if let error = error {
// Error occurred while reading data
print("Error while reading data: \(error.localizedDescription)")
} else if let data = data {
// Data read successfully
print("Data read successfully")
// Use the data
} else {
// Data not found in the cache
print("Data not found")
}
}
Removing Data
Use the removeItem(forKey:completionHandler:)
method to remove data from the cache:
// Remove data from the cache
cache.removeItem(forKey: "your-cache-key") { (cache, key, error) in
if let error = error {
// Error occurred while removing data
print("Error while removing data: \(error.localizedDescription)")
} else {
// Data removed successfully
print("Data removed successfully")
}
}
Clearing the Cache
Use the clearCache(completionHandler:)
method to clear the entire cache:
// Clear the cache
cache.clearCache { (cache, error) in
if let error = error {
// Error occurred while clearing the cache
print("Error while clearing the cache: \(error.localizedDescription)")
} else {
// Cache cleared successfully
print("Cache cleared successfully")
}
}
Sample Project
You can find a sample project demonstrating the usage of SPTPersistentCache in the Spotify iOS SDK repository.
Conclusion
With SPTPersistentCache, you can easily manage and store data in a persistent cache, enabling faster access to frequently used data in your app. The cache provides customizable configuration options to suit your specific requirements and supports encryption and secure data shredding for enhanced security.