Cache Invalidation
Cache invalidation is the process of removing or updating cached data when it becomes stale or invalid.
Overview
Effective cache invalidation strategies are crucial for maintaining data consistency and ensuring users always see up-to-date information.
Implementation
swift
// Example implementation in Swift
class CacheInvalidator {
private var cacheItems: [String: CacheItem] = [:]
func setCacheItem(key: String, data: Data, expirationTime: TimeInterval) {
let expirationDate = Date().addingTimeInterval(expirationTime)
cacheItems[key] = CacheItem(data: data, expirationDate: expirationDate)
}
func getCacheItem(key: String) -> Data? {
guard let item = cacheItems[key] else {
return nil
}
// Check if item is expired
if Date() > item.expirationDate {
cacheItems.removeValue(forKey: key)
return nil
}
return item.data
}
func invalidateCache(key: String) {
cacheItems.removeValue(forKey: key)
}
func invalidateAllCache() {
cacheItems.removeAll()
}
func invalidateExpiredCache() {
let now = Date()
cacheItems = cacheItems.filter { _, item in
item.expirationDate > now
}
}
}
struct CacheItem {
let data: Data
let expirationDate: Date
}
Best Practices
- Implement time-based expiration
- Use version-based invalidation
- Handle network state changes
- Implement partial cache invalidation
- Monitor cache hit rates