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