-
Notifications
You must be signed in to change notification settings - Fork 162
Improvements in the code #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3319b20
Improved readability
Vkt0r 2306500
Improve readability in code
Vkt0r d606398
Remove force unwrapping
Vkt0r 68d78da
Remove do-catch statement
Vkt0r 151bef4
Remove do-catch and refactor regular expression matching
Vkt0r b202f7f
Change name in typealias to start to capittal letter
Vkt0r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -22,6 +22,10 @@ public class Cache<T: NSCoding> { | |
| private let fileManager = NSFileManager() | ||
| private let diskWriteQueue: dispatch_queue_t = dispatch_queue_create("com.aschuch.cache.diskWriteQueue", DISPATCH_QUEUE_SERIAL) | ||
| private let diskReadQueue: dispatch_queue_t = dispatch_queue_create("com.aschuch.cache.diskReadQueue", DISPATCH_QUEUE_SERIAL) | ||
|
|
||
| /// Typealias to define the reusability in declaration of the closures. | ||
| public typealias CacheBlockClosure = (T, CacheExpiry) -> Void | ||
| public typealias ErrorClosure = (NSError?) -> Void | ||
|
|
||
|
|
||
| // MARK: Initializers | ||
|
|
@@ -74,16 +78,17 @@ public class Cache<T: NSCoding> { | |
| /// The supplied success or failure blocks must be called upon completion. | ||
| /// If the error block is called, the object is not cached and the completion block is invoked with this error. | ||
| /// - parameter completion: Called as soon as a cached object is available to use. The second parameter is true if the object was already cached. | ||
| public func setObjectForKey(key: String, cacheBlock: ((T, CacheExpiry) -> (), (NSError?) -> ()) -> (), completion: (T?, Bool, NSError?) -> ()) { | ||
| public func setObjectForKey(key: String, cacheBlock: (CacheBlockClosure, ErrorClosure) -> Void, completion: (T?, Bool, NSError?) -> Void) { | ||
|
|
||
| if let object = objectForKey(key) { | ||
| completion(object, true, nil) | ||
| } else { | ||
| let successBlock: (T, CacheExpiry) -> () = { (obj, expires) in | ||
| let successBlock: CacheBlockClosure = { (obj, expires) in | ||
| self.setObject(obj, forKey: key, expires: expires) | ||
| completion(obj, false, nil) | ||
| } | ||
|
|
||
| let failureBlock: (NSError?) -> () = { (error) in | ||
| let failureBlock: ErrorClosure = { (error) in | ||
| completion(nil, false, error) | ||
| } | ||
|
|
||
|
|
@@ -101,20 +106,18 @@ public class Cache<T: NSCoding> { | |
| /// | ||
| /// - returns: The cached object for the given name, or nil | ||
| public func objectForKey(key: String) -> T? { | ||
| var possibleObject: CacheObject? | ||
|
|
||
| // Check if object exists in local cache | ||
| possibleObject = cache.objectForKey(key) as? CacheObject | ||
|
|
||
| if possibleObject == nil { | ||
| // Try to load object from disk (synchronously) | ||
| dispatch_sync(diskReadQueue) { | ||
| let path = self.urlForKey(key).path! | ||
| if self.fileManager.fileExistsAtPath(path) { | ||
| possibleObject = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? CacheObject | ||
| } | ||
| } | ||
| } | ||
| // Check if object exists in local cache | ||
| var possibleObject = cache.objectForKey(key) as? CacheObject | ||
|
|
||
| if possibleObject == nil { | ||
| // Try to load object from disk (synchronously) | ||
| dispatch_sync(diskReadQueue) { | ||
| if let path = self.urlForKey(key).path where self.fileManager.fileExistsAtPath(path) { | ||
| possibleObject = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? CacheObject | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check if object is not already expired and return | ||
| // Delete object if expired | ||
|
|
@@ -143,7 +146,7 @@ public class Cache<T: NSCoding> { | |
| } | ||
|
|
||
| /// For internal testing only, might add this to the public API if needed | ||
| internal func setObject(object: T, forKey key: String, expires: CacheExpiry = .Never, completion: () -> ()) { | ||
| internal func setObject(object: T, forKey key: String, expires: CacheExpiry = .Never, completion: () -> Void) { | ||
| let expiryDate = expiryDateForCacheExpiry(expires) | ||
| let cacheObject = CacheObject(value: object, expiryDate: expiryDate) | ||
|
|
||
|
|
@@ -152,8 +155,9 @@ public class Cache<T: NSCoding> { | |
|
|
||
| // Write object to disk (asyncronously) | ||
| dispatch_async(diskWriteQueue) { | ||
| let path = self.urlForKey(key).path! | ||
| NSKeyedArchiver.archiveRootObject(cacheObject, toFile: path) | ||
| if let path = self.urlForKey(key).path { | ||
| NSKeyedArchiver.archiveRootObject(cacheObject, toFile: path) | ||
| } | ||
| completion() | ||
| } | ||
| } | ||
|
|
@@ -169,9 +173,7 @@ public class Cache<T: NSCoding> { | |
|
|
||
| dispatch_async(diskWriteQueue) { | ||
| let url = self.urlForKey(key) | ||
| do { | ||
| try self.fileManager.removeItemAtURL(url) | ||
| } catch _ {} | ||
| let _ = try? self.fileManager.removeItemAtURL(url) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -186,11 +188,9 @@ public class Cache<T: NSCoding> { | |
|
|
||
| for key in keys { | ||
| let url = self.urlForKey(key) | ||
| do { | ||
| try self.fileManager.removeItemAtURL(url) | ||
| } catch _ {} | ||
| let _ = try? self.fileManager.removeItemAtURL(url) | ||
| } | ||
|
|
||
| dispatch_async(dispatch_get_main_queue()) { | ||
| completion?() | ||
| } | ||
|
|
@@ -244,9 +244,7 @@ public class Cache<T: NSCoding> { | |
| } | ||
|
|
||
| private func sanitizedKey(key: String) -> String { | ||
| let regex = try! NSRegularExpression(pattern: "[^a-zA-Z0-9_]+", options: NSRegularExpressionOptions()) | ||
| let range = NSRange(location: 0, length: key.characters.count) | ||
| return regex.stringByReplacingMatchesInString(key, options: NSMatchingOptions(), range: range, withTemplate: "-") | ||
| return key.stringByReplacingOccurrencesOfString("[^a-zA-Z0-9_]+", withString: "-", options: .RegularExpressionSearch, range: nil) | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
|
|
||
| private func expiryDateForCacheExpiry(expiry: CacheExpiry) -> NSDate { | ||
|
|
@@ -259,5 +257,4 @@ public class Cache<T: NSCoding> { | |
| return date | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should better start with a capital letter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, agree 👍