-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added SwiftSafe dependency and InMemoryURLCache
- Loading branch information
Showing
7 changed files
with
1,332 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
use_frameworks! | ||
|
||
target 'BuildaUtils' do | ||
pod 'SwiftSafe', '~> 0.1' | ||
end | ||
|
||
target 'BuildaUtilsTests' do | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
PODS: | ||
- SwiftSafe (0.1) | ||
|
||
DEPENDENCIES: | ||
- SwiftSafe (~> 0.1) | ||
|
||
SPEC CHECKSUMS: | ||
SwiftSafe: 77ffd12b02678790bec1ef56a2d14ec5036f1fd6 | ||
|
||
COCOAPODS: 0.39.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// SimpleURLCache.swift | ||
// Buildasaur | ||
// | ||
// Created by Honza Dvorsky on 1/19/16. | ||
// Copyright © 2016 Honza Dvorsky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftSafe | ||
|
||
public class ResponseInfo: AnyObject { | ||
public let response: NSHTTPURLResponse | ||
public let body: AnyObject? | ||
|
||
public init(response: NSHTTPURLResponse, body: AnyObject?) { | ||
self.response = response | ||
self.body = body | ||
} | ||
} | ||
|
||
public struct CachedInfo { | ||
public let responseInfo: ResponseInfo? | ||
public let update: (ResponseInfo) -> () | ||
|
||
public var etag: String? { | ||
guard let responseInfo = self.responseInfo else { return nil } | ||
return responseInfo.response.allHeaderFields["ETag"] as? String | ||
} | ||
} | ||
|
||
public protocol URLCache { | ||
func getCachedInfoForRequest(request: NSURLRequest) -> CachedInfo | ||
} | ||
|
||
/** | ||
* Stores responses in memory only and only if resp code was in range 200...299 | ||
* This is optimized for APIs that return ETag for every response, thus | ||
* a repeated request can send over ETag in a header, allowing for not | ||
* downloading data again. In the case of GitHub, such request doesn't count | ||
* towards the rate limit. | ||
*/ | ||
public class InMemoryURLCache: URLCache { | ||
|
||
private let storage = NSCache() | ||
private let safe: Safe = CREW() | ||
|
||
public init(countLimit: Int = 1000) { | ||
self.storage.countLimit = countLimit //just to not grow indefinitely | ||
} | ||
|
||
public func getCachedInfoForRequest(request: NSURLRequest) -> CachedInfo { | ||
|
||
var responseInfo: ResponseInfo? | ||
let key = request.cacheableKey() | ||
self.safe.read { | ||
responseInfo = self.storage.objectForKey(key) as? ResponseInfo | ||
} | ||
|
||
let info = CachedInfo(responseInfo: responseInfo) { (responseInfo) -> () in | ||
self.safe.write { | ||
self.storage.setObject(responseInfo, forKey: key) | ||
} | ||
} | ||
return info | ||
} | ||
} | ||
|
||
extension NSURLRequest { | ||
public func cacheableKey() -> String { | ||
return "\(self.HTTPMethod!)-\(self.URL!.absoluteString)" | ||
} | ||
} | ||
|