Browse Source

更新缓存归档方式,去掉多线程操作

gaosboy 12 năm trước cách đây
mục cha
commit
fe6ddca353
4 tập tin đã thay đổi với 43 bổ sung14 xóa
  1. 2 0
      Kache/KHolder.h
  2. 35 10
      Kache/KHolder.m
  3. 3 2
      Kache/Kache.h
  4. 3 2
      Kache/Kache.m

+ 2 - 0
Kache/KHolder.h

@@ -6,6 +6,8 @@
 //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
 //
 
+#define Kache_Objects_Disk_Path         @"Caches/Kache_objects"
+
 #import <Foundation/Foundation.h>
 
 @class KQueue;

+ 35 - 10
Kache/KHolder.m

@@ -6,8 +6,6 @@
 //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
 //
 
-#define Kache_Objects_Disk_Path         @"Caches/Kache_objects"
-
 #import "KConfig.h"
 #import "KHolder.h"
 #import "KObject.h"
@@ -28,6 +26,7 @@
 
 // 把数据写到磁盘
 - (void)archiveData;
+- (void)archiveAllData;
 
 - (void)cleanExpiredObjects;
 
@@ -56,7 +55,7 @@
     if (self) {
         self.objects = [[NSMutableDictionary alloc] init];
         self.keys = [[NSMutableArray alloc] init];
-        self.size = 0.0f;
+        self.size = 0;
         
         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
         NSString *libDirectory = [paths objectAtIndex:0];
@@ -89,7 +88,7 @@
         NSMutableArray *copiedKeys = [self.keys mutableCopy];
         while (0 < [copiedKeys count]) {
             // 归档至阈值一半的数据
-            if ((ARCHIVING_THRESHOLD / 2) > self.size) {
+            if ((ARCHIVING_THRESHOLD / 2) >= self.size) {
                 break;
             }
             NSString *key = [copiedKeys lastObject];
@@ -106,10 +105,38 @@
 //    });
 }
 
+- (void)archiveAllData
+{
+    self.archiving = YES;
+//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
+        BOOL isDirectory = NO;
+        if (! [[NSFileManager defaultManager] fileExistsAtPath:self.path isDirectory:&isDirectory]) {
+            [self.fileManager createDirectoryAtPath:self.self.path
+                        withIntermediateDirectories:YES
+                                         attributes:nil
+                                              error:nil];
+        }
+        NSMutableArray *copiedKeys = [self.keys mutableCopy];
+        while (0 < [copiedKeys count]) {
+            NSString *key = [copiedKeys lastObject];
+            NSString *filePath = [self.path stringByAppendingPathComponent:key];
+            
+            NSData *data = [self.objects objectForKey:key];
+            [data writeToFile:filePath atomically:YES];
+            self.size -= data.length;
+            [copiedKeys removeLastObject];
+            [self.objects removeObjectForKey:key];
+        }
+        copiedKeys = nil;
+        self.archiving = NO;
+//    });
+    
+}
+
 - (void)cleanExpiredObjects
 {
     self.cleaning = YES;
-    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         if (self.keys && 0 < [self.keys count]) {
             for (int i = 0; i < [self.keys count] - 1; i ++) {
                 NSString *tmpKey = [self.keys objectAtIndex:i];
@@ -123,7 +150,7 @@
             }
         }
         self.cleaning = NO;
-    });
+//    });
 }
 
 #pragma mark - public
@@ -217,8 +244,8 @@
 // Convert object to NSDictionary.
 - (NSDictionary *)serialize
 {
+    [self archiveAllData];
     return [NSDictionary dictionaryWithObjectsAndKeys:
-            self.objects, @"objects",
             self.keys, @"keys",
             [NSString stringWithFormat:@"%d", self.size], @"size",
             nil];
@@ -227,10 +254,8 @@
 // Convert NSDictionary to object.
 - (void)unserializeFrom:(NSDictionary *)dict
 {
-    if ([[dict allKeys] containsObject:@"objects"]
-        && [[dict allKeys] containsObject:@"keys"]
+    if ([[dict allKeys] containsObject:@"keys"]
         && [[dict allKeys] containsObject:@"meta"]) {
-        self.objects = [dict objectForKey:@"objects"];
         self.keys = [dict objectForKey:@"keys"];
         self.size = [[dict objectForKey:@"size"] intValue];
     }

+ 3 - 2
Kache/Kache.h

@@ -44,7 +44,8 @@
 + (void)newQueueWithName:(NSString *)name size:(NSInteger)size;
 + (void)newPoolWithName:(NSString *)name size:(NSInteger)size;
 
-+ (void)saveToStorage;
-+ (void)loadFromStorage;
++ (void)save;
+// 静态load方法,每次启动会被自动调用
++ (void)load;
 
 @end

+ 3 - 2
Kache/Kache.m

@@ -83,11 +83,11 @@
     return [[Kache instance] newPoolWithName:name size:size];
 }
 
-+ (void)saveToStorage {
++ (void)save {
     return [[Kache instance] save];
 }
 
-+ (void)loadFromStorage {
++ (void)load {
     return [[Kache instance] load];
 }
 
@@ -231,6 +231,7 @@
 }
 
 - (void)load {
+    NSLog(@"zouni");
 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
 	NSString *libDirectory = [paths objectAtIndex:0];