Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/Sadmansamee/CachyKit

A Caching Library is written in Swift that can cache JSON, Image, Zip or AnyObject with expiry date/TTYL and force refresh.
https://github.com/Sadmansamee/CachyKit

cache ios nscache swift

Last synced: about 2 months ago
JSON representation

A Caching Library is written in Swift that can cache JSON, Image, Zip or AnyObject with expiry date/TTYL and force refresh.

Awesome Lists containing this project

README

        

# CachyKit
[![codebeat badge](https://codebeat.co/badges/de5bf12e-9682-4d1e-85ce-fbc89a738f3d)](https://codebeat.co/projects/github-com-sadmansamee-cachykit-master) ![](https://img.shields.io/cocoapods/v/CachyKit.svg?style=flat)



## Description

Nice threadsafe expirable cache management that can cache any object. Supports fetching from server, single object expire date, UIImageView loading etc.

## Installation

### Cocoapods

[**CachyKit**](http://cocoapods.org/pods/CachyKit) is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod 'CachyKit'
```
then run

```
$ pod repo update
```
```
$ pod install
```

## Features

- [x] Asynchronous data downloading and caching.
- [x] Asynchronous image downloading, caching and showing.
- [x] Expiry date/time for all the object individually.
- [x] Multiple-layer hybrid cache for both memory and disk.
- [x] Fine control on cache behavior. Customizable expiration date and size limit.
- [x] Force refresh if needed.
- [x] Independent components. Use the [Cachy](https://github.com/Sadmansamee/CachyKit/blob/master/Sources/Cachy/Cachy.swift) or [CachyLoader](https://github.com/Sadmansamee/CachyKit/blob/master/Sources/Cachy/CachyLoader.swift) system separately as you need.
- [x] Can save JSON, UIImage, ZIP or AnyObject.
- [x] View extensions for `UIImageView`.
- [x] Indicator while loading images.

## Usage

```swift
import CachyKit
```

If you want to download and cache a file(JSON, ZIP, UIImage or any type) then simply call with a url

```swift
let cachy = CachyLoader()

cachy.loadWithURL(URL(string: "http://your_url_here")!) { [weak self] data, _ in
// Do whatever you need with the data object
}
```

You can also cache with **URLRequest**

```swift
let cachy = CachyLoader()

let request = URLRequest(url: URL(string: "http://your_url_here")!)
cachy.loadWithURLRequest(request) { [weak self] data, _ in
// Do whatever you need with the data object
}
```

if you want set expiry date for each object

```swift
let cachy = CachyLoader()

//(optional) if isRefresh = true it will forcefully refresh data from remote server
//(optional) you can set **expirationDate** according to your need

cachy.loadWithURL(URL(string: "http://your_url_here")!,isRefresh = false,expirationDate = ExpiryDate.everyDay.expiryDate()) { [weak self] data, _ in
// Do whatever you need with the data object
}
```

**Clear** all cache

```swift
CachyLoaderManager.shared.clear()
```

**CachyLoader** has also UIImageView extension.

```swift
//(optional) if isShowLoading is true it will show a loading indicator
imageView.cachyImageLoad("your URL", isShowLoading: true, completionBlock: { _, _ in })
```

It will download, cache and load UIImage into your UIImageView
**CachyLoader** is also configurable, by calling function ***CachyLoaderManager.shared.configure()***

```swift
// All the parametre is optional
// Here if you want set how much much memory/disk should use set memoryCapacity, diskCapacity
// To cache only on memory set isOnlyInMemory which is true by default
// You may set expiry date for all the cache object by setting expiryDate
// Your objects may not be conforming to `NSSecureCoding`, set this variable to `false`

CachyLoaderManager.shared.configure(
memoryCapacity: 1020,
maxConcurrentOperationCount: 10,
timeoutIntervalForRequest: 3,
expiryDate: ExpiryDate.everyWeek,
isOnlyInMemory: true,
isSupportingSecureCodingSaving: false
)
```

**expiryDate** parametre takes

1. **.never** to never expire the cache object
2. **.everyDay** to expire at the end of the day
3. **.everyWeek** to expiry after a week
4. **.everyMonth** to set the expiry date each month
5. **.seconds** to set the expiry after some seconds

## Without CachyLoader

To cache using **CachyLoader** is the easiest way to do, but if you want manage your caching, sycning and threading **CachyKit** also supports that.

### Configuration

Here is how you can setup some configuration options

```swift
Cachy.countLimit = 1000 // setup total count of elements saved into the cache

Cachy.totalCostLimit = 1024 * 1024 // setup the cost limit of the cache

Cachy.shared.expiration = .everyDay // setup expiration date of each object in the cache

Cachy.shared.isOnlyInMemory = false // will be cached on Memory only or both

```

### Adding/Fetching objects

If you want to add or fetch an object you just follow these simple steps:

```swift
//1. Create a CachyObject
let object = CachyObject(value: "HEllo, Worlds", key: "key")

// A given expiry date will be applied to the item
let object2 = CachyObject(value: "HEllo, Worlds", key: "key",expirationDate: ExpiryDate.everyDay.expiryDate())

//2. Add it to the cache
Cachy.shared.add(object: object)

//3. Fetch an object from the cache
let string: String? = Cachy.shared.get(forKey: "key")
```

## Contact

Follow and contact me on [Twitter](https://twitter.com/SameeSadman) or [LinkedIn](https://www.linkedin.com/in/sadmansamee/). If you find an issue, just [open a ticket](https://github.com/sadmansamee/Cachy/issues/new). Pull requests are warmly welcome as well.

## Contribution

If you want fix anything or improve or add any new feature you are very much welcome.

### License

Cachy is released under the MIT license. See LICENSE for details.