From c9a27b45435995d5eb6994b42753cc3b162c85ea Mon Sep 17 00:00:00 2001 From: ayb-huangweihua Date: Tue, 15 Dec 2015 16:23:36 +0800 Subject: [PATCH 1/4] * add calculateDiskCacheSizeWithCompletionHandler methed * add test methed --- AwesomeCache/Cache.swift | 46 +++++++++++++++++++++++++++-- Example/Base.lproj/LaunchScreen.xib | 5 ++-- Example/Base.lproj/Main.storyboard | 26 +++++++++++++--- Example/ViewController.swift | 9 ++++++ 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/AwesomeCache/Cache.swift b/AwesomeCache/Cache.swift index 5531235..fd6b6b7 100644 --- a/AwesomeCache/Cache.swift +++ b/AwesomeCache/Cache.swift @@ -211,7 +211,48 @@ public class Cache { } } } - + //MARK: -- Calculate Disk Cache Size + /** + Calculate disk cache size + + - parameter completionHandler: Calculate complete then callback + */ + public func calculateDiskCacheSizeWithCompletionHandler(completionHandler: ((size: UInt) -> ())?) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in + let diskCacheURL = self.cacheDirectory + + let resourceKeys = [NSURLIsDirectoryKey, NSURLTotalFileAllocatedSizeKey] + var diskCacheSize: UInt = 0 + + if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL, includingPropertiesForKeys: resourceKeys, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler: nil), + urls = fileEnumerator.allObjects as? [NSURL] { + for fileURL in urls { + do { + let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys) + // If it is a Directory. Continue to next file URL. + if let isDirectory = resourceValues[NSURLIsDirectoryKey]?.boolValue { + if isDirectory { + continue + } + } + + if let fileSize = resourceValues[NSURLTotalFileAllocatedSizeKey] as? NSNumber { + diskCacheSize += fileSize.unsignedLongValue + } + } catch _ { + } + + } + } + + dispatch_async(dispatch_get_main_queue(), { () -> Void in + if let completionHandler = completionHandler { + completionHandler(size: diskCacheSize) + } + }) + }) + } + // MARK: Subscripting @@ -259,5 +300,6 @@ public class Cache { return date } } - + + } diff --git a/Example/Base.lproj/LaunchScreen.xib b/Example/Base.lproj/LaunchScreen.xib index a3e3147..65f0836 100644 --- a/Example/Base.lproj/LaunchScreen.xib +++ b/Example/Base.lproj/LaunchScreen.xib @@ -1,7 +1,8 @@ - + - + + diff --git a/Example/Base.lproj/Main.storyboard b/Example/Base.lproj/Main.storyboard index 2e092ec..12215ba 100644 --- a/Example/Base.lproj/Main.storyboard +++ b/Example/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -29,7 +29,7 @@ + + + + + + diff --git a/Example/ViewController.swift b/Example/ViewController.swift index eabeaa5..b57a2eb 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -27,5 +27,14 @@ class ViewController: UIViewController { @IBAction func saveInCache(sender: AnyObject?) { cache["myText"] = textView.text } + @IBAction func calculateCacheSize(sender: AnyObject?) { + cache.calculateDiskCacheSizeWithCompletionHandler { (size) -> () in + let kSize = String(format: "%d", size) + print(kSize) + } + } + @IBAction func removeCache(sender: AnyObject) { + cache.removeAllObjects() + } } From 1879d6b12afebd659a080134f7f43c1c172a74e0 Mon Sep 17 00:00:00 2001 From: ayb-huangweihua Date: Tue, 15 Dec 2015 17:02:52 +0800 Subject: [PATCH 2/4] modify README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 409bafe..b9a4b13 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,14 @@ cache.setObject("Alex", forKey: "name", expires: .Date(NSDate(timeIntervalSince1 If an object is accessed after its expiry date, it is automatically removed from the cache and deleted from disk. However, you are responsible to delete expired objects regularly by calling `removeExpiredObjects` (e.g. on app launch). +### Calculate cache size +Asynchronous calculate cache size , callback size on main thread. +```swift +cache.calculateDiskCacheSizeWithCompletionHandler { (size) -> () in + let kSize = String(format: "%d", size) + print(kSize) +} +``` ### Awesome API Caching From 28dcd235dbe1cf867761d8b8f1792a68f2254e13 Mon Sep 17 00:00:00 2001 From: huang1988519 Date: Tue, 15 Dec 2015 17:10:42 +0800 Subject: [PATCH 3/4] change user name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b9a4b13..3c07aa6 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ cache.calculateDiskCacheSizeWithCompletionHandler { (size) -> () in ### Awesome API Caching API responses are usually cached for a specific period of time. AwesomeCache provides an easy method to cache a block of asynchronous tasks. - + ```swift cache.setObjectForKey("name", cacheBlock: { success, failure in // Perform tasks, e.g. call an API From 936775a8a25f98807406c25c1810a29d1df8d709 Mon Sep 17 00:00:00 2001 From: Wally Huang Date: Tue, 22 Dec 2015 11:47:21 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20cocoapod=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AwesomeCache.podspec | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/AwesomeCache.podspec b/AwesomeCache.podspec index 87c180b..666b424 100644 --- a/AwesomeCache.podspec +++ b/AwesomeCache.podspec @@ -1,15 +1,15 @@ Pod::Spec.new do |s| - s.name = "AwesomeCache" + s.name = "AwesomeCache-hwh" s.version = "2.0" - s.summary = "Delightful on-disk cache (written in Swift)" - s.description = "Delightful on-disk cache (written in Swift). Backed by NSCache for maximum performance and support for expiry of single objects." - s.homepage = "https://github.com/aschuch/AwesomeCache" + s.summary = "轻量级本地缓存swift 库" + s.description = "轻量级本地缓存swift 库. Backed by NSCache for maximum performance and support for expiry of single objects." + s.homepage = "https://github.com/huang1988519/" s.license = { :type => "MIT", :file => "LICENSE" } - s.author = { "Alexander Schuch" => "alexander@schuch.me" } - s.social_media_url = "http://twitter.com/schuchalexander" + s.author = { "huangwh" => "huang1988519@126.com" } + s.social_media_url = "http://huang1988519.github.io/" s.platform = :ios s.ios.deployment_target = "8.0" - s.source = { :git => "https://github.com/aschuch/AwesomeCache.git", :tag => s.version } + s.source = { :git => "https://github.com/huang1988519/AwesomeCache.git", :tag => s.version } s.requires_arc = true s.source_files = "AwesomeCache/Cache.swift", "AwesomeCache/CacheObject.swift" end