Introduction
Welcome to the documentation for EvURLCache, a flexible and powerful caching library for iOS.
Getting Started
To use EvURLCache in your iOS project, follow the steps below:
- Install EvURLCache using CocoaPods:
pod 'EvURLCache'
- Import the framework in your source file:
import EvURLCache
- Initialize and set up the cache in your AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
EvURLCache.shared.initialize()
return true
}
Usage
1. Basic Caching
// Create a URL request
guard let url = URL(string: "https://example.com") else { return }
let request = URLRequest(url: url)
// Perform the request
urlCache.perform(request) { data, response, error in
if let error = error {
print("Error: \(error.localizedDescription)")
} else if let data = data {
print("Data received: \(data)")
}
}
2. Customizing Cache Behavior
You can customize the behavior of the cache by configuring various properties:
// Set maximum cache size (in MB)
EvURLCache.shared.maxCacheSizeInMB = 50
// Set maximum age of cached responses (in seconds)
EvURLCache.shared.maxCacheAgeInSeconds = 3600
// Enable or disable memory caching
EvURLCache.shared.shouldCacheInMemory = true
// Enable or disable disk caching
EvURLCache.shared.shouldCacheOnDisk = true
3. Clearing the Cache
// Clear all cached responses
EvURLCache.shared.clearCache()
// Remove a specific URL from cache
guard let url = URL(string: "https://example.com") else { return }
EvURLCache.shared.removeCachedResponse(for: URLRequest(url: url))
Advanced Topics
1. Conditional Requests
// Check if a cached response is still valid
guard let url = URL(string: "https://example.com") else { return }
let request = URLRequest(url: url)
if let cachedResponse = EvURLCache.shared.cachedResponse(for: request) {
if cachedResponse.isValid {
// Response is still valid, use it
} else {
// Response expired, perform a new request
EvURLCache.shared.perform(request) { data, response, error in
// Handle the response
}
}
}
2. Cache Analytics
EvURLCache provides a handy way to gather cache analytics:
// Get cache statistics
let statistics = EvURLCache.shared.statistics
print("Cache Statistics:")
print("Total Cached Responses: \(statistics.totalCachedResponses)")
print("Total Cached Data Size: \(statistics.totalCachedSize) bytes")
print("Cache Hits: \(statistics.hits)")
print("Cache Misses: \(statistics.misses)")
// Reset cache statistics
EvURLCache.shared.resetStatistics()
3. Accessing Cached Responses
// Access cached response directly
guard let url = URL(string: "https://example.com") else { return }
let request = URLRequest(url: url)
if let cachedResponse = EvURLCache.shared.cachedResponse(for: request) {
print("Cached Data Size: \(cachedResponse.data.count) bytes")
print("Cache Expiration Date: \(cachedResponse.expirationDate)")
}
Conclusion
That’s it! You should now be able to effectively use EvURLCache in your iOS project for efficient caching. For more details and additional functionality, please refer to the provided documentation.