Swiftpack.co - lukacs-m/SafeCache as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by lukacs-m.
lukacs-m/SafeCache 0.1.2
Thread Safe generic caching for swift projects
⭐️ 0
🕓 1 week ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/lukacs-m/SafeCache.git", from: "0.1.2")

SafeCache

This is package purpose is to offer a thread safe cache service for swift projects. It is based on the article of John Sundell on how to implement a Cache service in swift while leveraging the power of NSCache. Caching By Sundell The implementation contains a actor wrapper around NSCache to make it compatible with the new Async/Await concurrency of Swift. It also offers a way to persist data on disk in some cases.

What

  • ☑ Caching
  • ☑ Uses of Apple's new structured concurrency (Async/Await)
  • ☑ Disk Persistency
  • ☑ Use the power of Generics
  • ☑ Tested

Getting Started

Installation

SafeCache is installed via the official Swift Package Manager.

Select Xcode>File> Swift Packages>Add Package Dependency...
and add https://github.com/lukacs-m/SafeCache.

Create a Cache

To create an instance of SafeCache it is very easy. You just need to instanciate it with a Key/Value type and voila you can now start a caching festival. The following implementation has a type String for the keys and CachedObject as the value type saved in this instance of cache.

// Default
struct CachedObject {
 let text = "I'm the cached object"
}

let cacheClient = SafeCache<String, CachedObject>() 

Of course the key and value type combo could be completly different.

Actions

Add element to cache

    await cacheClient.insert(ElementToAdd, forKey: key)

Get element for key from cache

    let value = await cacheClient.value(forKey: key)

Remove one element of cahche

    await cacheClient.removeValue(forKey: key)

Remove all element of cache

    await cacheClient.removeAll()

GitHub

link
Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Removed date provider from init
4 weeks ago

Removing a date provider from the init of safecache

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics