This project contains a Swift class that acts as a memory cache (singleton). It supports optionally namespacing for the cache identifier. It also supports a TTL to invalidate a cashed data object after a certain time.
The purpose of this class is to provide data across the whole code structure with an easy access pattern. You can extract interesting data into the mem cache and access it via a shared instance from anywhere in your code. SwiftMemCache DOES NOT write data to disk!
SwiftMemCache is completely unit tested, to prove that everything works as expected!
Save the current cache to disk. Entries with an expired TTL will be deleted before the cache gets persisted.
CTMemCache.sharedInstance.saveToDisk()
CTMemCache.sharedInstance.restoreFromDisk()
Adds or changes a specified entry in the mem cache. Default TTL: -1 (object will always live in memory)
CTMemCache.sharedInstance.set("foo", data:<YourObject>, namespace:"bar", ttl:3600)
CTMemCache.sharedInstance.set("foo", data:<YourObject>)
Return an entry if exists, otherwise returns nil
CTMemCache.sharedInstance.get("foo", namespace:"bar")
CTMemCache.sharedInstance.get("foo")
Deletes an entry by its key (namespace)
CTMemCache.sharedInstance.delete("foo", namespace:"bar")
CTMemCache.sharedInstance.delete("foo")
Returns true if an entry with the given key (optional namespace) exists
CTMemCache.sharedInstance.exists("foo", namespace:"bar")
Returns the amount of the entries in the mem cache
CTMemCache.sharedInstance.size()
Deletes all entries of the given namespace from the mem cache
CTMemCache.sharedInstance.cleanNamespace("foo")
Checks if an given entry is expired. Returns true/false
CTMemCache.sharedInstance.isExpired("foo", namespace:"bar")
Cleans all entries in the mem cache where the TTL is expire
CTMemCache.sharedInstance.deleteOutdated()
This wipes all the data from the mem cache:
CTMemCache.sharedInstance.reset()
For feature requests or bugs open an issue or provide a unit tested Pull Request :-) Find me on Twitter: @ctews