     }
     
     public func setObject(object: NSData?, forKey: String) {
-        if let _ = object {
-            
+        if let data = object {
+            let now = NSDate()
+            let task = DiskBackgroundTask.start()
+            
+            lock()
+            guard let fileURL = encodedFileURLForKey(forKey) else { return }
+            
+            if data.writeToURL(fileURL, atomically: true) {
+                setFileModificationDate(now, forURL: fileURL)
+                
+                let values = try? fileURL.resourceValuesForKeys([ NSURLTotalFileAllocatedSizeKey ])
+                if let diskFileSize = values?[NSURLTotalFileAllocatedSizeKey] as? Double {
+                    state.sizes[forKey] = diskFileSize
+                    state.byteCount += diskFileSize
+                }
+                
+                if let limit = state.maximumSizeInBytes where state.byteCount > limit {
+                    trimToSizeByDate(limit, completionHandler: nil)
+                }
+            }
+            
+            unlock()
+            
+            task.end()
         }
         else {
             removeObjectForKey(forKey)

    var cacheURL: NSURL!
}

private struct DiskBackgroundTask {
    #if os(ios) || os(watchos)
    var taskIdentifier: UIBackgroundTaskIdentifier
    #endif
}

    }
    
    public func objectForKey(key: String) -> NSData? {
        let now = NSDate()
        let fileManager = NSFileManager.defaultManager()
        var object: NSData?
        lock()
        if let fileURL = encodedFileURLForKey(key), let path = fileURL.path where fileManager.fileExistsAtPath(path) {
            do {
                object = try NSData(contentsOfURL: fileURL, options: .DataReadingMappedIfSafe)
            }
            catch {
                let _ = try? fileManager.removeItemAtURL(fileURL)
            }
            
            setFileModificationDate(now, forURL: fileURL)
        }
        unlock()
        
        return object
    }
    
    public func removeObjectForKey(key: String) {
    }
    
    public func trimBeforeDate(date: NSDate) {
    }
    
    public func trimToSizeByDate(size: Double) {
    }
    
    public func removeAllObjects() {
