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 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() + } } diff --git a/README.md b/README.md index 409bafe..3c07aa6 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,19 @@ 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 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