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;
 @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
  of NSKeyedArchiver. You can use this block to support the objects which do not
  conform to the `NSCoding` protocol.
  conform to the `NSCoding` protocol.
  
  
@@ -60,7 +60,7 @@
 @property (copy) NSData *(^customArchiveBlock)(id object);
 @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
  of NSKeyedUnarchiver. You can use this block to support the objects which do not
  conform to the `NSCoding` protocol.
  conform to the `NSCoding` protocol.
  
  

+ 25 - 4
YYWebImage/Cache/YYKVStorage.m

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