SieveCache is an open source replacement for NSCache
that behaves in a predictable, debuggable way. SieveCache is an implementation in Swift of the SIEVE cache explained here.
SieveCache is distributed as a Swift Package that you can import into your Xcode project or other Swift based code base.
Note: SieveCache requires Swift 5.9 (so Xcode 15+) to build, and runs on iOS 13+ and macOS 12+. It should workTM on other platforms but has not been tested. Do let me know if you can verify success on other platforms.
To install using Swift Package Manager, add this to the dependencies:
section in your Package.swift file:
.package(url: "https://github.com/GratefulGuru/SieveCache.git", from: "1.0.0"),
You can create an instance of SieveCache as follows:
let cache = SieveCache<String, Int>()
This would create a cache of default size (1000 objects), containing Int
values keyed by String
. To add an object to the cache, use:
cache.setObject(99, forKey: "foo")
To fetch a cached object, use:
let object = cache.object(forKey: "foo") // Returns nil if key not found
You can limit the cache size by count. This can be done at initialization time:
let cache = SieveCache<URL, Date>(countLimit: 100)
Or after the cache has been created:
cache.countLimit = 100 // Limit the cache to 100 elements
Objects will be removed from the cache automatically when the count limit is exceeded. You can also remove objects explicitly by using:
let object = cache.removeObject(forKey: "foo")
Or, if you don't need the object, by setting it to nil
:
cache.setObject(nil, forKey: "foo")
And you can remove all objects at once with:
cache.removeAllObjects()
On iOS and tvOS, the cache will be emptied automatically in the event of a memory warning.
Reading, writing and removing entries from the cache are performed in constant time. When the cache is full, insertion time degrades slightly due to the need to remove elements each time a new value is inserted. This should still be constant-time.
The SieveCache framework is primarily the work of Christian Wolf Johannsen.