-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: cache clean up #15
Changes from 8 commits
7aa5b39
c32de92
f958ec9
3e03498
9cb3993
366f796
a9f8fba
c03e22f
2c1458f
86e7091
cee8c3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
public class HybridCache { | ||
|
||
|
@@ -16,6 +16,15 @@ public class HybridCache { | |
|
||
frontStorage = StorageFactory.resolve(name, kind: config.frontKind, maxSize: config.maxSize) | ||
backStorage = StorageFactory.resolve(name, kind: config.backKind, maxSize: config.maxSize) | ||
|
||
let notificationCenter = NSNotificationCenter.defaultCenter() | ||
|
||
notificationCenter.addObserver(self, selector: "applicationDidReceiveMemoryWarning", | ||
name: UIApplicationDidReceiveMemoryWarningNotification, object: nil) | ||
notificationCenter.addObserver(self, selector: "applicationWillTerminate", | ||
name: UIApplicationWillTerminateNotification, object: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💀 |
||
notificationCenter.addObserver(self, selector: "applicationDidEnterBackground", | ||
name: UIApplicationDidEnterBackgroundNotification, object: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to remove these notifications at any time? |
||
} | ||
|
||
// MARK: - Caching | ||
|
@@ -78,4 +87,38 @@ public class HybridCache { | |
} | ||
} | ||
} | ||
|
||
// MARK: - Notifications | ||
|
||
public func applicationDidReceiveMemoryWarning() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these be |
||
frontStorage.clearExpired(nil) | ||
} | ||
|
||
public func applicationWillTerminate() { | ||
backStorage.clearExpired(nil) | ||
} | ||
|
||
public func applicationDidEnterBackground() { | ||
let application = UIApplication.sharedApplication() | ||
var backgroundTask: UIBackgroundTaskIdentifier? | ||
|
||
backgroundTask = application.beginBackgroundTaskWithExpirationHandler { [weak self] in | ||
guard let weakSelf = self, var backgroundTask = backgroundTask else { return } | ||
|
||
weakSelf.endBackgroundTask(&backgroundTask) | ||
} | ||
|
||
backStorage.clearExpired { [weak self] in | ||
guard let weakSelf = self, var backgroundTask = backgroundTask else { return } | ||
|
||
dispatch_async(dispatch_get_main_queue()) { | ||
weakSelf.endBackgroundTask(&backgroundTask) | ||
} | ||
} | ||
} | ||
|
||
func endBackgroundTask(inout task: UIBackgroundTaskIdentifier) { | ||
UIApplication.sharedApplication().endBackgroundTask(task) | ||
task = UIBackgroundTaskInvalid | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ public class DiskStorage: StorageAware { | |
do { | ||
try weakSelf.fileManager.createDirectoryAtPath(weakSelf.path, | ||
withIntermediateDirectories: true, attributes: nil) | ||
} catch _ {} | ||
} catch {} | ||
} | ||
|
||
do { | ||
|
@@ -52,7 +52,7 @@ public class DiskStorage: StorageAware { | |
try weakSelf.fileManager.setAttributes( | ||
[NSFileModificationDate : expiry.date], | ||
ofItemAtPath: filePath) | ||
} catch _ {} | ||
} catch {} | ||
|
||
completion?() | ||
} | ||
|
@@ -85,7 +85,7 @@ public class DiskStorage: StorageAware { | |
|
||
do { | ||
try weakSelf.fileManager.removeItemAtPath(weakSelf.filePath(key)) | ||
} catch _ {} | ||
} catch {} | ||
|
||
completion?() | ||
} | ||
|
@@ -106,7 +106,7 @@ public class DiskStorage: StorageAware { | |
where expiryDate.inThePast { | ||
try weakSelf.fileManager.removeItemAtPath(weakSelf.filePath(key)) | ||
} | ||
} catch _ {} | ||
} catch {} | ||
|
||
completion?() | ||
} | ||
|
@@ -121,7 +121,81 @@ public class DiskStorage: StorageAware { | |
|
||
do { | ||
try weakSelf.fileManager.removeItemAtPath(weakSelf.path) | ||
} catch _ {} | ||
} catch {} | ||
|
||
completion?() | ||
} | ||
} | ||
|
||
public func clearExpired(completion: (() -> Void)? = nil) { | ||
dispatch_async(writeQueue) { [weak self] in | ||
guard let weakSelf = self else { | ||
completion?() | ||
return | ||
} | ||
|
||
let URL = NSURL(fileURLWithPath: weakSelf.path) | ||
let resourceKeys = [NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey] | ||
var objects = [(URL: NSURL, resourceValues: [NSObject: AnyObject])]() | ||
var URLsToDelete = [NSURL]() | ||
var totalSize: UInt = 0 | ||
|
||
guard let fileEnumerator = weakSelf.fileManager.enumeratorAtURL(URL, includingPropertiesForKeys: resourceKeys, | ||
options: .SkipsHiddenFiles, errorHandler: nil), URLs = fileEnumerator.allObjects as? [NSURL] else { | ||
completion?() | ||
return | ||
} | ||
|
||
for fileURL in URLs { | ||
do { | ||
let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys) | ||
|
||
guard (resourceValues[NSURLIsDirectoryKey] as? NSNumber)?.boolValue == false else { | ||
continue | ||
} | ||
|
||
if let expiryDate = resourceValues[NSURLContentModificationDateKey] as? NSDate | ||
where expiryDate.inThePast { | ||
URLsToDelete.append(fileURL) | ||
continue | ||
} | ||
|
||
if let fileSize = resourceValues[NSURLTotalFileAllocatedSizeKey] as? NSNumber { | ||
totalSize += fileSize.unsignedLongValue | ||
objects.append((URL: fileURL, resourceValues: resourceValues)) | ||
} | ||
} catch {} | ||
} | ||
|
||
for fileURL in URLsToDelete { | ||
do { | ||
try weakSelf.fileManager.removeItemAtURL(fileURL) | ||
} catch {} | ||
} | ||
|
||
if weakSelf.maxSize > 0 && totalSize > weakSelf.maxSize { | ||
let targetSize = weakSelf.maxSize / 2 | ||
|
||
let sortedFiles = objects.sort({ | ||
let time1 = ($0.resourceValues[NSURLContentModificationDateKey] as? NSDate)?.timeIntervalSince1970 | ||
let time2 = ($1.resourceValues[NSURLContentModificationDateKey] as? NSDate)?.timeIntervalSince1970 | ||
return time1 > time2 | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can remove |
||
|
||
for file in sortedFiles { | ||
do { | ||
try weakSelf.fileManager.removeItemAtURL(file.URL) | ||
} catch {} | ||
|
||
if let fileSize = file.resourceValues[NSURLTotalFileAllocatedSizeKey] as? NSNumber { | ||
totalSize -= fileSize.unsignedLongValue | ||
} | ||
|
||
if totalSize < targetSize { | ||
break | ||
} | ||
} | ||
} | ||
|
||
completion?() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.