Browse Source

add auto checkpoint to limit `sqlite-wal` file's size

ibireme 9 years ago
parent
commit
bc30ca438d
2 changed files with 27 additions and 6 deletions
  1. 2 2
      YYWebImage/Cache/YYDiskCache.h
  2. 25 4
      YYWebImage/Cache/YYKVStorage.m

+ 2 - 2
YYWebImage/Cache/YYDiskCache.h

@@ -51,7 +51,7 @@
 @property (readonly) NSUInteger inlineThreshold;
 
 /**
- If this block in not nil, then the block will be used to archive object instead
+ If this block is not nil, then the block will be used to archive object instead
  of NSKeyedArchiver. You can use this block to support the objects which do not
  conform to the `NSCoding` protocol.
  
@@ -60,7 +60,7 @@
 @property (copy) NSData *(^customArchiveBlock)(id object);
 
 /**
- If this block in not nil, then the block will be used to unarchive object instead
+ If this block is not nil, then the block will be used to unarchive object instead
  of NSKeyedUnarchiver. You can use this block to support the objects which do not
  conform to the `NSCoding` protocol.
  

+ 25 - 4
YYWebImage/Cache/YYKVStorage.m

@@ -135,6 +135,12 @@ static NSString *const kTrashDirectoryName = @"trash";
     return [self _dbExecute:sql];
 }
 
+- (void)_dbCheckpoint {
+    if (![self _dbIsReady]) return;
+    // Cause a checkpoint to occur, merge `sqlite-wal` file to `sqlite` file.
+    sqlite3_wal_checkpoint(_db, NULL);
+}
+
 - (BOOL)_dbExecute:(NSString *)sql {
     if (sql.length == 0) return NO;
     if (![self _dbIsReady]) return NO;
@@ -777,7 +783,10 @@ static NSString *const kTrashDirectoryName = @"trash";
     
     switch (_type) {
         case YYKVStorageTypeSQLite: {
-            return [self _dbDeleteItemsWithSizeLargerThan:size];
+            if ([self _dbDeleteItemsWithSizeLargerThan:size]) {
+                [self _dbCheckpoint];
+                return YES;
+            }
         } break;
         case YYKVStorageTypeFile:
         case YYKVStorageTypeMixed: {
@@ -785,7 +794,10 @@ static NSString *const kTrashDirectoryName = @"trash";
             for (NSString *name in filenames) {
                 [self _fileDeleteWithName:name];
             }
-            return [self _dbDeleteItemsWithSizeLargerThan:size];
+            if ([self _dbDeleteItemsWithSizeLargerThan:size]) {
+                [self _dbCheckpoint];
+                return YES;
+            }
         } break;
     }
     return NO;
@@ -797,7 +809,10 @@ static NSString *const kTrashDirectoryName = @"trash";
     
     switch (_type) {
         case YYKVStorageTypeSQLite: {
-            return [self _dbDeleteItemsWithTimeEarlierThan:time];
+            if ([self _dbDeleteItemsWithTimeEarlierThan:time]) {
+                [self _dbCheckpoint];
+                return YES;
+            }
         } break;
         case YYKVStorageTypeFile:
         case YYKVStorageTypeMixed: {
@@ -805,7 +820,10 @@ static NSString *const kTrashDirectoryName = @"trash";
             for (NSString *name in filenames) {
                 [self _fileDeleteWithName:name];
             }
-            return [self _dbDeleteItemsWithTimeEarlierThan:time];
+            if ([self _dbDeleteItemsWithTimeEarlierThan:time]) {
+                [self _dbCheckpoint];
+                return NO;
+            }
         } break;
     }
     return NO;
@@ -837,6 +855,7 @@ static NSString *const kTrashDirectoryName = @"trash";
             if (!suc) break;
         }
     } while (total > maxSize && items.count > 0 && suc);
+    if (suc) [self _dbCheckpoint];
     return suc;
 }
 
@@ -866,6 +885,7 @@ static NSString *const kTrashDirectoryName = @"trash";
             if (!suc) break;
         }
     } while (total > maxCount && items.count > 0 && suc);
+    if (suc) [self _dbCheckpoint];
     return suc;
 }
 
@@ -904,6 +924,7 @@ static NSString *const kTrashDirectoryName = @"trash";
             }
             if (progress) progress(total - left, total);
         } while (left > 0 && items.count > 0 && suc);
+        if (suc) [self _dbCheckpoint];
         if (end) end(!suc);
     }
 }