Skip to content

Add support for contentAccessDate-based expiry and cleanup using custom expiry period #345

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Source/Shared/Storage/DiskStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,64 @@ extension DiskStorage: StorageAware {
try removeResourceObjects(resourceObjects, totalSize: totalSize)
}

public func removeExpiredObjects(expiryPeriod: TimeInterval? = nil) throws {
let storageURL = URL(fileURLWithPath: path)
let resourceKeys: [URLResourceKey] = [
.isDirectoryKey,
.contentModificationDateKey,
.contentAccessDateKey,
.totalFileAllocatedSizeKey
]

var resourceObjects = [ResourceObject]()
var filesToDelete = [URL]()
var totalSize: UInt = 0

let fileEnumerator = fileManager.enumerator(
at: storageURL,
includingPropertiesForKeys: resourceKeys,
options: .skipsHiddenFiles,
errorHandler: nil
)

guard let urlArray = fileEnumerator?.allObjects as? [URL] else {
throw Error.fileEnumeratorFailed
}

for url in urlArray {
let resourceValues = try url.resourceValues(forKeys: Set(resourceKeys))
guard resourceValues.isDirectory != true else {
continue
}

if let expiryPeriod = expiryPeriod,
let accessDate = resourceValues.contentAccessDate,
accessDate.addingTimeInterval(expiryPeriod) < Date() {
filesToDelete.append(url)
continue
} else if expiryPeriod == nil,
let expiryDate = resourceValues.contentModificationDate,
expiryDate.inThePast {
filesToDelete.append(url)
continue
}

if let fileSize = resourceValues.totalFileAllocatedSize {
totalSize += UInt(fileSize)
resourceObjects.append((url: url, resourceValues: resourceValues))
}
}

// Remove expired
for url in filesToDelete {
try fileManager.removeItem(at: url)
onRemove?(url.path)
}

// Enforce size limits
try removeResourceObjects(resourceObjects, totalSize: totalSize)
}

public func removeInMemoryObject(forKey key: Key) throws { }
}

Expand Down
7 changes: 7 additions & 0 deletions Source/Shared/Storage/HybridStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ extension HybridStorage: StorageAware {

notifyStorageObservers(about: .removeExpired)
}

public func removeExpiredObjects(expiryPeriod: TimeInterval? = nil) throws {
memoryStorage.removeExpiredObjects()
try diskStorage.removeExpiredObjects(expiryPeriod: expiryPeriod)

notifyStorageObservers(about: .removeExpired)
}
}

public extension HybridStorage {
Expand Down
4 changes: 4 additions & 0 deletions Source/Shared/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ extension Storage: StorageAware {
public func removeExpiredObjects() throws {
try self.syncStorage.removeExpiredObjects()
}

public func removeExpiredObjects(expiryPeriod: TimeInterval? = nil) throws {
try self.syncStorage.removeExpiredObjects(expiryPeriod: expiryPeriod)
}
}

public extension Storage {
Expand Down
6 changes: 6 additions & 0 deletions Source/Shared/Storage/SyncStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ extension SyncStorage: StorageAware {
}
}

public func removeExpiredObjects(expiryPeriod: TimeInterval? = nil) throws {
try serialQueue.sync {
try innerStorage.removeExpiredObjects(expiryPeriod: expiryPeriod)
}
}

public func removeInMemoryObject(forKey key: Key) throws {
try serialQueue.sync {
try self.innerStorage.removeInMemoryObject(forKey: key)
Expand Down