An open API service indexing awesome lists of open source software.

https://github.com/bih/swiftcache

A wonderfully lightweight cache for Swift
https://github.com/bih/swiftcache

Last synced: 8 months ago
JSON representation

A wonderfully lightweight cache for Swift

Awesome Lists containing this project

README

          

![](SwiftCache.png)

A beautifully lightweight cache written for **Swift**. It has been tested on iOS and OS X. I wrote it over an hour because I couldn't find something good enough and I think you'll like it.

Tested with XCode 6.3.1 with Swift 1.2. It requires [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) but that also means it's the only cache I know that works with SwiftyJSON and [Alamofire](https://github.com/alamofire/alamofire) and reliably so!

Give it a spin.

### Installation (Cocoapods)

You can now have SwiftCache on your Swift project using [Cocoapods](http://cocoapods.org)

Add this to your `Podfile`

```
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

pod 'SwiftCache'
```

### Installation (Manually)

Just copy the contents of `SwiftCache.swift` and insert it into your XCode. Nothing else necessary.

### Example

```swift

// The above will save it for 60 seconds before expiring the cache
var names = SwiftCache(name: "friendsNames", expiresIn: 60)

// Want to clear the cache? Run this.
// names.expireCache()

names.respond { (data : [String]) in
if names.isCached() {
println("Saved in cache: \(data)")
println("Expires from cache in \(names.secondsLeftInCache()) seconds")
} else {
println("Written to cache: \(data)")
}
}.request { in
// Save to cache (it'll accept any datatype)
// In this case, it's a String Array (which is denoted in Swift as [String])
names.saveToCache(["Mike", "Jon", "Aziz", "Tim", "Joe", "Syeef", "Hamer", "Li", "Gregor"])
}

```

### Another Example

```swift

// Let's remember where a user said they're from (setting expiresIn to -1 means forever)
var currentUserCity = SwiftCache(name: "currentUserCity", expiresIn: -1)

// Want to clear the cache? Run this.
// currentUserCity.expireCache()

names.respond { (currentCity : String) in
// This will always be ran, but may be from the cache or directly from the block in .request
println("Hi there stranger from the wonderful city of \(currentCity)")
}.request { in
// Here, if the cache is empty or expired, you can show a UIView to ask for their location
let aLocationTheUserSelected : String = "Manchester, United Kingdom"
names.saveToCache(aLocationTheUserSelected)
}

```

Released under the [MIT License](http://bih.mit-license.org)