Skip to content

Instantly share code, notes, and snippets.

@raulriera
Created December 7, 2016 14:40
Show Gist options
  • Save raulriera/5e5f2b9b1a63e655f80c15bdb64a0335 to your computer and use it in GitHub Desktop.
Save raulriera/5e5f2b9b1a63e655f80c15bdb64a0335 to your computer and use it in GitHub Desktop.
Code sample for Medium article about Caching and Protocols
public protocol Cachable {
var fileName: String { get }
func transform() -> Data
}
final public class Cacher {
let destination: URL
private let queue = OperationQueue()
public enum CacheDestination {
case temporary
case atFolder(String)
}
// MARK: Initialization
public init(destination: CacheDestination) {
// Create the URL for the location of the cache resources
switch destination {
case .temporary:
self.destination = URL(fileURLWithPath: NSTemporaryDirectory())
case .atFolder(let folder):
let documentFolder = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
self.destination = URL(fileURLWithPath: documentFolder).appendingPathComponent(folder, isDirectory: true)
}
let fileManager = FileManager.default
do {
try fileManager.createDirectory(at: self.destination, withIntermediateDirectories: true, attributes: nil)
}
catch {
fatalError("Unable to create cache URL: \(error)")
}
}
// MARK
public func persist(item: Cachable, completion: @escaping (_ url: URL) -> Void) {
let url = destination.appendingPathComponent(item.fileName, isDirectory: false)
// Create an operation to process the request.
let operation = BlockOperation {
do {
try item.transform().write(to: url, options: [.atomicWrite])
} catch {
fatalError("Failed to write item to cache: \(error)")
}
}
// Set the operation's completion block to call the request's completion handler.
operation.completionBlock = {
completion(url)
}
// Add the operation to the queue to start the work.
queue.addOperation(operation)
}
}
@raulriera
Copy link
Author

Checkout the Swift 4 and multi platform (iOS, tvOS, macOS, watchOS) at https://github.com/raulriera/Cacher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment