Skip to content

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

  1. Implement time-based expiration
  2. Use version-based invalidation
  3. Handle network state changes
  4. Implement partial cache invalidation
  5. Monitor cache hit rates

Created by Eren Demir.