Instagram.mp4
VideoCacheManager is a lightweight Swift utility for caching videos locally in an iOS application. It helps efficiently store and retrieve downloaded video files, reducing redundant network requests and improving performance.
- β Singleton instance for easy access
- β Stores cached videos in the app's cache directory
- β Retrieves cached videos using a unique key
- β Downloads and caches videos asynchronously
- β Handles errors gracefully
if let cachedURL = VideoCacheManager.shared.getCachedVideoURL(for: "videoKey") {
// Use the cached video
}let videoURL = URL(string: "https://example.com/video.mp4")!
VideoCacheManager.shared.cacheVideo(from: videoURL, key: "videoKey") { cachedURL in
if let cachedURL = cachedURL {
print("Video cached at:", cachedURL)
} else {
print("Failed to cache video")
}
}- Uses FileManager to create a VideoCache directory inside the appβs cache folder.
- Checks if a video is already cached before downloading.
- Downloads the video asynchronously using URLSession.downloadTask.
- Moves the downloaded file to the cache directory for persistent storage.