-
Notifications
You must be signed in to change notification settings - Fork 334
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
Automatically clear expired objects #294
Open
007-Heroa
wants to merge
2
commits into
hyperoslo:master
Choose a base branch
from
007-Heroa:Support-Expiry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
import Foundation | ||
|
||
public struct MemoryConfig { | ||
/// Expiry date that will be applied by default for every added object | ||
/// if it's not overridden in the add(key: object: expiry: completion:) method | ||
public let expiry: Expiry | ||
/// The maximum number of objects in memory the cache should hold. | ||
/// If 0, there is no count limit. The default value is 0. | ||
public let countLimit: UInt | ||
|
||
/// The maximum total cost that the cache can hold before it starts evicting objects. | ||
/// If 0, there is no total cost limit. The default value is 0 | ||
public let totalCostLimit: UInt | ||
|
||
public init(expiry: Expiry = .never, countLimit: UInt = 0, totalCostLimit: UInt = 0) { | ||
self.expiry = expiry | ||
self.countLimit = countLimit | ||
self.totalCostLimit = totalCostLimit | ||
} | ||
/// Expiry date that will be applied by default for every added object | ||
/// if it's not overridden in the add(key: object: expiry: completion:) method | ||
public let expiry: Expiry | ||
|
||
/// ExpirationMode that will be applied for every added object | ||
public let expirationMode: ExpirationMode | ||
|
||
/// The maximum number of objects in memory the cache should hold. | ||
/// If 0, there is no count limit. The default value is 0. | ||
public let countLimit: UInt | ||
|
||
/// The maximum total cost that the cache can hold before it starts evicting objects. | ||
/// If 0, there is no total cost limit. The default value is 0 | ||
public let totalCostLimit: UInt | ||
|
||
public init(expiry: Expiry = .never, expirationMode: ExpirationMode = .auto, countLimit: UInt = 0, totalCostLimit: UInt = 0) { | ||
self.expiry = expiry | ||
self.expirationMode = expirationMode | ||
|
||
self.countLimit = countLimit | ||
self.totalCostLimit = totalCostLimit | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,8 @@ | ||||||
import Foundation | ||||||
#if canImport(UIKit) | ||||||
import UIKit | ||||||
#elseif canImport(AppKit) && !targetEnvironment(macCatalyst) | ||||||
import AppKit | ||||||
#endif | ||||||
|
||||||
/// Save objects to file on disk | ||||||
final public class DiskStorage<Key: Hashable, Value> { | ||||||
|
@@ -17,6 +21,9 @@ final public class DiskStorage<Key: Hashable, Value> { | |||||
|
||||||
private let transformer: Transformer<Value> | ||||||
private let hasher = Hasher.constantAccrossExecutions() | ||||||
|
||||||
private var didEnterBackgroundObserver: NSObjectProtocol? | ||||||
|
||||||
|
||||||
// MARK: - Initialization | ||||||
public convenience init(config: DiskConfig, fileManager: FileManager = FileManager.default, transformer: Transformer<Value>) throws { | ||||||
|
@@ -54,6 +61,29 @@ final public class DiskStorage<Key: Hashable, Value> { | |||||
self.fileManager = fileManager | ||||||
self.path = path | ||||||
self.transformer = transformer | ||||||
applyExpiratonMode(self.config.expirationMode) | ||||||
} | ||||||
|
||||||
public func applyExpiratonMode(_ expirationMode: ExpirationMode) { | ||||||
if let didEnterBackgroundObserver = didEnterBackgroundObserver { | ||||||
NotificationCenter.default.removeObserver(didEnterBackgroundObserver) | ||||||
} | ||||||
|
||||||
if expirationMode == .auto { | ||||||
didEnterBackgroundObserver = | ||||||
NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, | ||||||
object: nil, | ||||||
queue: nil) { [weak self] _ in | ||||||
guard let `self` = self else { return } | ||||||
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.
Suggested change
|
||||||
try? self.removeExpiredObjects() | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
deinit { | ||||||
if let didEnterBackgroundObserver = didEnterBackgroundObserver { | ||||||
NotificationCenter.default.removeObserver(didEnterBackgroundObserver) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,8 @@ | ||||||
import Foundation | ||||||
#if canImport(UIKit) | ||||||
import UIKit | ||||||
#elseif canImport(AppKit) && !targetEnvironment(macCatalyst) | ||||||
import AppKit | ||||||
#endif | ||||||
|
||||||
public class MemoryStorage<Key: Hashable, Value>: StorageAware { | ||||||
final class WrappedKey: NSObject { | ||||||
|
@@ -22,11 +26,39 @@ public class MemoryStorage<Key: Hashable, Value>: StorageAware { | |||||
fileprivate var keys = Set<Key>() | ||||||
/// Configuration | ||||||
fileprivate let config: MemoryConfig | ||||||
|
||||||
/// The closure to be called when the key has been removed | ||||||
public var onRemove: ((Key) -> Void)? | ||||||
|
||||||
public var didEnterBackgroundObserver: NSObjectProtocol? | ||||||
|
||||||
public init(config: MemoryConfig) { | ||||||
self.config = config | ||||||
self.cache.countLimit = Int(config.countLimit) | ||||||
self.cache.totalCostLimit = Int(config.totalCostLimit) | ||||||
applyExpiratonMode(self.config.expirationMode) | ||||||
} | ||||||
|
||||||
public func applyExpiratonMode(_ expirationMode: ExpirationMode) { | ||||||
if let didEnterBackgroundObserver = didEnterBackgroundObserver { | ||||||
NotificationCenter.default.removeObserver(didEnterBackgroundObserver) | ||||||
} | ||||||
if expirationMode == .auto { | ||||||
didEnterBackgroundObserver = | ||||||
NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, | ||||||
object: nil, | ||||||
queue: nil) | ||||||
{ [weak self] _ in | ||||||
guard let `self` = self else { return } | ||||||
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.
Suggested change
|
||||||
self.removeExpiredObjects() | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
deinit { | ||||||
if let didEnterBackgroundObserver = didEnterBackgroundObserver { | ||||||
NotificationCenter.default.removeObserver(didEnterBackgroundObserver) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -41,7 +73,10 @@ extension MemoryStorage { | |||||
|
||||||
public func setObject(_ object: Value, forKey key: Key, expiry: Expiry? = nil) { | ||||||
let capsule = MemoryCapsule(value: object, expiry: .date(expiry?.date ?? config.expiry.date)) | ||||||
cache.setObject(capsule, forKey: WrappedKey(key)) | ||||||
|
||||||
/// MemoryLayout.size(ofValue:) return the contiguous memory footprint of the given instance , so cost is always MemoryCapsule size (8 bytes) | ||||||
let cost = MemoryLayout.size(ofValue: capsule) | ||||||
cache.setObject(capsule, forKey: WrappedKey(key), cost: cost) | ||||||
keys.insert(key) | ||||||
} | ||||||
|
||||||
|
@@ -66,6 +101,7 @@ extension MemoryStorage { | |||||
public func removeObject(forKey key: Key) { | ||||||
cache.removeObject(forKey: WrappedKey(key)) | ||||||
keys.remove(key) | ||||||
onRemove?(key) | ||||||
} | ||||||
|
||||||
public func entry(forKey key: Key) throws -> Entry<Value> { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -123,3 +123,9 @@ public extension Storage { | |||||
return self.hybridStorage.diskStorage.totalSize | ||||||
} | ||||||
} | ||||||
|
||||||
public extension Storage { | ||||||
func applyExpiratonMode(_ expirationMode: ExpirationMode) { | ||||||
self.hybridStorage.applyExpiratonMode(expirationMode) | ||||||
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.
Suggested change
|
||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.