@@ -151,12 +151,14 @@ open class URLCache : NSObject {
151
151
if let cache = sharedCache {
152
152
return cache
153
153
} else {
154
+ guard var cacheDirectoryUrl = FileManager . default. urls ( for: . cachesDirectory, in: . userDomainMask) . first else {
155
+ fatalError ( " Unable to find cache directory " )
156
+ }
157
+
154
158
let fourMegaByte = 4 * 1024 * 1024
155
159
let twentyMegaByte = 20 * 1024 * 1024
156
- let bundleIdentifier = Bundle . main. bundleIdentifier ?? UUID ( ) . uuidString
157
- let cacheDirectoryPath = FileManager . default. urls ( for: . cachesDirectory, in: . userDomainMask) . first? . path ?? " \( NSHomeDirectory ( ) ) /Library/Caches "
158
- let diskPath = cacheDirectoryPath + " / " + bundleIdentifier
159
- let cache = URLCache ( memoryCapacity: fourMegaByte, diskCapacity: twentyMegaByte, diskPath: diskPath)
160
+ cacheDirectoryUrl. appendPathComponent ( ProcessInfo . processInfo. processName)
161
+ let cache = URLCache ( memoryCapacity: fourMegaByte, diskCapacity: twentyMegaByte, diskPath: cacheDirectoryUrl. path)
160
162
sharedCache = cache
161
163
cache. persistence? . setupCacheDirectory ( )
162
164
return cache
@@ -167,8 +169,6 @@ open class URLCache : NSObject {
167
169
guard newValue !== sharedCache else { return }
168
170
169
171
sharedSyncQ. sync {
170
- // Remove previous data before resetting new URLCache instance
171
- URLCache . sharedCache? . persistence? . deleteAllCaches ( )
172
172
sharedCache = newValue
173
173
newValue. persistence? . setupCacheDirectory ( )
174
174
}
@@ -195,7 +195,7 @@ open class URLCache : NSObject {
195
195
self . memoryCapacity = memoryCapacity
196
196
self . diskCapacity = diskCapacity
197
197
198
- if let _path = path {
198
+ if diskCapacity > 0 , let _path = path {
199
199
self . persistence = _CachePersistence ( path: _path)
200
200
}
201
201
@@ -284,7 +284,7 @@ open class URLCache : NSObject {
284
284
@result the current usage of the on-disk cache of the receiver.
285
285
*/
286
286
open var currentDiskUsage : Int {
287
- return self . syncQ. sync { self . persistence? . cacheSizeInDesk ?? 0 }
287
+ return self . syncQ. sync { self . persistence? . cacheSizeInDisk ?? 0 }
288
288
}
289
289
290
290
}
@@ -299,7 +299,9 @@ fileprivate struct _CachePersistence {
299
299
300
300
let path : String
301
301
302
- var cacheSizeInDesk : Int {
302
+ // FIXME: Create a stored property
303
+ // Update this value as the cache added and evicted
304
+ var cacheSizeInDisk : Int {
303
305
do {
304
306
let subFiles = try FileManager . default. subpathsOfDirectory ( atPath: path)
305
307
var total : Int = 0
@@ -315,10 +317,6 @@ fileprivate struct _CachePersistence {
315
317
316
318
func setupCacheDirectory( ) {
317
319
do {
318
- if FileManager . default. fileExists ( atPath: path) {
319
- try FileManager . default. removeItem ( atPath: path)
320
- }
321
-
322
320
try FileManager . default. createDirectory ( atPath: path, withIntermediateDirectories: true )
323
321
} catch {
324
322
fatalError ( " Unable to create directories for URLCache: \( error. localizedDescription) " )
@@ -328,15 +326,21 @@ fileprivate struct _CachePersistence {
328
326
func saveCachedResponse( _ response: CachedURLResponse , for request: URLRequest ) {
329
327
let archivedData = NSKeyedArchiver . archivedData ( withRootObject: response)
330
328
do {
331
- let cacheFileURL = urlForFileIdentifier ( request. cacheFileIdentifier)
332
- try archivedData. write ( to: cacheFileURL)
329
+ if let fileIdentifier = request. cacheFileIdentifier {
330
+ let cacheFileURL = urlForFileIdentifier ( fileIdentifier)
331
+ try archivedData. write ( to: cacheFileURL)
332
+ }
333
333
} catch {
334
334
fatalError ( " Unable to save cache data: \( error. localizedDescription) " )
335
335
}
336
336
}
337
337
338
338
func cachedResponse( for request: URLRequest ) -> CachedURLResponse ? {
339
- let url = urlForFileIdentifier ( request. cacheFileIdentifier)
339
+ guard let fileIdentifier = request. cacheFileIdentifier else {
340
+ return nil
341
+ }
342
+
343
+ let url = urlForFileIdentifier ( fileIdentifier)
340
344
guard let data = try ? Data ( contentsOf: url) ,
341
345
let response = NSKeyedUnarchiver . unarchiveObject ( with: data) as? CachedURLResponse else {
342
346
return nil
@@ -345,29 +349,35 @@ fileprivate struct _CachePersistence {
345
349
return response
346
350
}
347
351
348
- func deleteAllCaches( ) {
349
- do {
350
- try FileManager . default. removeItem ( atPath: path)
351
- } catch {
352
- fatalError ( " Unable to flush database for URLCache: \( error. localizedDescription) " )
353
- }
354
- }
355
-
356
352
private func urlForFileIdentifier( _ identifier: String ) -> URL {
357
- return URL ( fileURLWithPath: path + " / " + identifier)
353
+ return URL ( fileURLWithPath: path) . appendingPathComponent ( identifier)
358
354
}
359
355
360
356
}
361
357
362
- fileprivate extension URLRequest {
358
+ extension URLRequest {
359
+
360
+ fileprivate var cacheFileIdentifier : String ? {
361
+ guard let scheme = self . url? . scheme, scheme == " http " || scheme == " https " ,
362
+ let method = httpMethod, !method. isEmpty,
363
+ let urlString = url? . absoluteString else {
364
+ return nil
365
+ }
366
+
367
+ var hashString = " \( scheme) _ \( method) _ \( urlString) "
368
+ if let userAgent = self . allHTTPHeaderFields ? [ " User-Agent " ] , !userAgent. isEmpty {
369
+ hashString. append ( contentsOf: " _ \( userAgent) " )
370
+ }
371
+
372
+ if let acceptLanguage = self . allHTTPHeaderFields ? [ " Accept-Language " ] , !acceptLanguage. isEmpty {
373
+ hashString. append ( contentsOf: " _ \( acceptLanguage) " )
374
+ }
363
375
364
- var cacheFileIdentifier : String {
365
- guard let urlString = url? . absoluteString else {
366
- fatalError ( " Unable to create cache identifier for request: \( self ) " )
376
+ if let range = self . allHTTPHeaderFields ? [ " Range " ] , !range. isEmpty {
377
+ hashString. append ( contentsOf: " _ \( range) " )
367
378
}
368
379
369
- let method = httpMethod ?? " GET "
370
- return urlString + method
380
+ return String ( format: " %X " , hashString. hashValue)
371
381
}
372
382
373
383
}
0 commit comments