@@ -22,6 +22,10 @@ public class Cache<T: NSCoding> {
2222 private let fileManager = NSFileManager ( )
2323 private let diskWriteQueue : dispatch_queue_t = dispatch_queue_create ( " com.aschuch.cache.diskWriteQueue " , DISPATCH_QUEUE_SERIAL)
2424 private let diskReadQueue : dispatch_queue_t = dispatch_queue_create ( " com.aschuch.cache.diskReadQueue " , DISPATCH_QUEUE_SERIAL)
25+
26+ /// Typealias to define the reusability in declaration of the closures.
27+ public typealias CacheBlockClosure = ( T , CacheExpiry ) -> Void
28+ public typealias ErrorClosure = ( NSError ? ) -> Void
2529
2630
2731 // MARK: Initializers
@@ -74,16 +78,17 @@ public class Cache<T: NSCoding> {
7478 /// The supplied success or failure blocks must be called upon completion.
7579 /// If the error block is called, the object is not cached and the completion block is invoked with this error.
7680 /// - parameter completion: Called as soon as a cached object is available to use. The second parameter is true if the object was already cached.
77- public func setObjectForKey( key: String , cacheBlock: ( ( T , CacheExpiry ) -> ( ) , ( NSError ? ) -> ( ) ) -> ( ) , completion: ( T ? , Bool , NSError ? ) -> ( ) ) {
81+ public func setObjectForKey( key: String , cacheBlock: ( CacheBlockClosure , ErrorClosure ) -> Void , completion: ( T ? , Bool , NSError ? ) -> Void ) {
82+
7883 if let object = objectForKey ( key) {
7984 completion ( object, true , nil )
8085 } else {
81- let successBlock : ( T , CacheExpiry ) -> ( ) = { ( obj, expires) in
86+ let successBlock : CacheBlockClosure = { ( obj, expires) in
8287 self . setObject ( obj, forKey: key, expires: expires)
8388 completion ( obj, false , nil )
8489 }
8590
86- let failureBlock : ( NSError ? ) -> ( ) = { ( error) in
91+ let failureBlock : ErrorClosure = { ( error) in
8792 completion ( nil , false , error)
8893 }
8994
@@ -101,20 +106,18 @@ public class Cache<T: NSCoding> {
101106 ///
102107 /// - returns: The cached object for the given name, or nil
103108 public func objectForKey( key: String ) -> T ? {
104- var possibleObject : CacheObject ?
105-
106- // Check if object exists in local cache
107- possibleObject = cache. objectForKey ( key) as? CacheObject
108109
109- if possibleObject == nil {
110- // Try to load object from disk (synchronously)
111- dispatch_sync ( diskReadQueue) {
112- let path = self . urlForKey ( key) . path!
113- if self . fileManager. fileExistsAtPath ( path) {
114- possibleObject = NSKeyedUnarchiver . unarchiveObjectWithFile ( path) as? CacheObject
115- }
116- }
117- }
110+ // Check if object exists in local cache
111+ var possibleObject = cache. objectForKey ( key) as? CacheObject
112+
113+ if possibleObject == nil {
114+ // Try to load object from disk (synchronously)
115+ dispatch_sync ( diskReadQueue) {
116+ if let path = self . urlForKey ( key) . path where self . fileManager. fileExistsAtPath ( path) {
117+ possibleObject = NSKeyedUnarchiver . unarchiveObjectWithFile ( path) as? CacheObject
118+ }
119+ }
120+ }
118121
119122 // Check if object is not already expired and return
120123 // Delete object if expired
@@ -143,7 +146,7 @@ public class Cache<T: NSCoding> {
143146 }
144147
145148 /// For internal testing only, might add this to the public API if needed
146- internal func setObject( object: T , forKey key: String , expires: CacheExpiry = . Never, completion: ( ) -> ( ) ) {
149+ internal func setObject( object: T , forKey key: String , expires: CacheExpiry = . Never, completion: ( ) -> Void ) {
147150 let expiryDate = expiryDateForCacheExpiry ( expires)
148151 let cacheObject = CacheObject ( value: object, expiryDate: expiryDate)
149152
@@ -152,8 +155,9 @@ public class Cache<T: NSCoding> {
152155
153156 // Write object to disk (asyncronously)
154157 dispatch_async ( diskWriteQueue) {
155- let path = self . urlForKey ( key) . path!
156- NSKeyedArchiver . archiveRootObject ( cacheObject, toFile: path)
158+ if let path = self . urlForKey ( key) . path {
159+ NSKeyedArchiver . archiveRootObject ( cacheObject, toFile: path)
160+ }
157161 completion ( )
158162 }
159163 }
@@ -169,9 +173,7 @@ public class Cache<T: NSCoding> {
169173
170174 dispatch_async ( diskWriteQueue) {
171175 let url = self . urlForKey ( key)
172- do {
173- try self . fileManager. removeItemAtURL ( url)
174- } catch _ { }
176+ let _ = try ? self . fileManager. removeItemAtURL ( url)
175177 }
176178 }
177179
@@ -186,11 +188,9 @@ public class Cache<T: NSCoding> {
186188
187189 for key in keys {
188190 let url = self . urlForKey ( key)
189- do {
190- try self . fileManager. removeItemAtURL ( url)
191- } catch _ { }
191+ let _ = try ? self . fileManager. removeItemAtURL ( url)
192192 }
193-
193+
194194 dispatch_async ( dispatch_get_main_queue ( ) ) {
195195 completion ? ( )
196196 }
@@ -244,9 +244,7 @@ public class Cache<T: NSCoding> {
244244 }
245245
246246 private func sanitizedKey( key: String ) -> String {
247- let regex = try ! NSRegularExpression ( pattern: " [^a-zA-Z0-9_]+ " , options: NSRegularExpressionOptions ( ) )
248- let range = NSRange ( location: 0 , length: key. characters. count)
249- return regex. stringByReplacingMatchesInString ( key, options: NSMatchingOptions ( ) , range: range, withTemplate: " - " )
247+ return key. stringByReplacingOccurrencesOfString ( " [^a-zA-Z0-9_]+ " , withString: " - " , options: . RegularExpressionSearch, range: nil )
250248 }
251249
252250 private func expiryDateForCacheExpiry( expiry: CacheExpiry ) -> NSDate {
@@ -259,5 +257,4 @@ public class Cache<T: NSCoding> {
259257 return date
260258 }
261259 }
262-
263260}
0 commit comments