Skip to content

Commit

Permalink
added SwiftSafe dependency and InMemoryURLCache
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 committed Jan 20, 2016
1 parent 2d6f4ec commit 019461a
Show file tree
Hide file tree
Showing 7 changed files with 1,332 additions and 464 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ DerivedData
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Pods/
Pods/

# Carthage
#
Expand Down
8 changes: 7 additions & 1 deletion BuildaUtils.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "BuildaUtils"
s.version = "0.2.6"
s.version = "0.2.7"
s.summary = "Shared utilities for the Buildasaur and XcodeServerSDK projects."

s.description = <<-DESC
Expand All @@ -21,4 +21,10 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/czechboy0/BuildaUtils.git", :tag => "v#{s.version}" }
s.source_files = "Source/*.{swift}"

# load the dependencies from the podfile for target ekgclient
podfile_deps = Podfile.from_file(Dir["Podfile"].first).target_definitions["BuildaUtils"].dependencies
podfile_deps.each do |dep|
s.dependency dep.name, dep.requirement.to_s
end

end
1,681 changes: 1,219 additions & 462 deletions BuildaUtils.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions BuildaUtils.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Podfile
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

10 changes: 10 additions & 0 deletions Podfile.lock
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
74 changes: 74 additions & 0 deletions Source/SimpleURLCache.swift
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)"
}
}

0 comments on commit 019461a

Please sign in to comment.