Просмотр исходного кода

Add setting to network recorder to disable caching media response bodies.

i.e. image, video, audio. Most of the time, we’re not interested in looking at these response bodies, and they occupy lots space in the cache. Data responses (json, xml, etc.) tend to be more valuable to keep around.
Ryan Olson лет назад: 11
Родитель
Сommit
6d1db9535f
2 измененных файлов с 12 добавлено и 1 удалено
  1. 3 0
      Classes/Network/FLEXNetworkRecorder.h
  2. 9 1
      Classes/Network/FLEXNetworkRecorder.m

+ 3 - 0
Classes/Network/FLEXNetworkRecorder.h

@@ -23,6 +23,9 @@ extern NSString *const kFLEXNetworkRecorderUserInfoTransactionKey;
 /// Defaults to 25 MB if never set. Values set here are presisted across launches of the app.
 @property (nonatomic, assign) NSUInteger responseCacheByteLimit;
 
+/// If NO, the recorder not cache will not cache response for content types with an "image", "video", or "audio" prefix.
+@property (nonatomic, assign) BOOL shouldCacheMediaResponses;
+
 // Accessing recorded network activity
 
 /// Array of FLEXNetworkTransaction objects ordered by start time with the newest first.

+ 9 - 1
Classes/Network/FLEXNetworkRecorder.m

@@ -147,7 +147,15 @@ NSString *const kFLEXNetworkRecorderResponseCacheLimitDefaultsKey = @"com.flex.r
         transaction.transactionState = FLEXNetworkTransactionStateFinished;
         transaction.duration = -[transaction.startTime timeIntervalSinceNow];
 
-        if ([responseBody length] > 0) {
+        BOOL shouldCache = [responseBody length] > 0;
+        if (!self.shouldCacheMediaResponses) {
+            NSArray *ignoredMIMETypePrefixes = @[ @"audio", @"image", @"video" ];
+            for (NSString *ignoredPrefix in ignoredMIMETypePrefixes) {
+                shouldCache = shouldCache && ![transaction.response.MIMEType hasPrefix:ignoredPrefix];
+            }
+        }
+        
+        if (shouldCache) {
             [self.responseCache setObject:responseBody forKey:requestId cost:[responseBody length]];
         }