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

Overview

CachyKit

codebeat badge

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 is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CachyKit'

then run

$ pod repo update
$ pod install

Features

  • Asynchronous data downloading and caching.
  • Asynchronous image downloading, caching and showing.
  • Expiry date/time for all the object individually.
  • Multiple-layer hybrid cache for both memory and disk.
  • Fine control on cache behavior. Customizable expiration date and size limit.
  • Force refresh if needed.
  • Independent components. Use the Cachy or CachyLoader system separately as you need.
  • Can save JSON, UIImage, ZIP or AnyObject.
  • View extensions for UIImageView.
  • Indicator while loading images.

Usage

import CachyKit

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

 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

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

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

CachyLoaderManager.shared.clear()

CachyLoader has also UIImageView extension.

//(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()

// 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

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:

//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 LinkedIn. If you find an issue, just open a ticket. 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.

Comments
  • Release pod new version with new tag off master

    Release pod new version with new tag off master

    Thanks, @Sadmansamee 🙌 for merging in my contribution. I believe those changes are quite crucial to the make CachyKit fully usable with func configure and all the improvements. Can you please push a new release with these changes? This way - users won't need to specify the :branch => master in Podfile.

    opened by d4r1091 2
  • Various improvements

    Various improvements

    CachyKit will now support:

    • URLRequest with additional headers and other settings. Previously was only referring to a new URLRequest created inside the load method getting its url even though the load method was accepting a urlRequest as a parameter.
    • Archiving now works correctly after supporting optional secure coding for disk saving
    • configure method is now accessible outside of the CachyKit pod and is now matching the documentation
    • Code documentation improved
    • Readme updated
    opened by d4r1091 2
  • Cache emptied after termination of app

    Cache emptied after termination of app

    Hi. Is there any way that objects are alive even after i terminate the app and open it again? They are alive while my app is active or in background, but if i kill it and open again the objects are not there anymore.

    opened by SlavcoPetkovski 2
  • cannot use CachyLoaderManager.shared.configure()

    cannot use CachyLoaderManager.shared.configure()

    great library, thanks.

    I'm trying to use:

    CachyLoaderManager.shared.configure( memoryCapacity: 1020, diskCapacity: 2024, maxConcurrentOperationCount: 10, timeoutIntervalForRequest: 3, diskPath: "temp", expiryDate: .weekly, isOnlyInMemory: true )

    But I get this error:

    'shared' is inaccessible due to 'internal' protection level

    I cannot find a way around this, any ideas?

    Swift 5.1, ios 13.

    bug resolved 
    opened by workingDog 2
  • [ImgBot] Optimize images

    [ImgBot] Optimize images

    Beep boop. Your images are optimized!

    Your image file size has been reduced by 52% 🎉

    Details

    | File | Before | After | Percent reduction | |:--|:--|:--|:--| | /cachy.png | 87.69kb | 38.29kb | 56.34% | | /Example/CachyDemo/Assets.xcassets/spinner.imageset/spinner-of-dots.png | 9.25kb | 7.94kb | 14.17% | | | | | | | Total : | 96.94kb | 46.22kb | 52.32% |


    📝docs | :octocat: repo | 🙋issues | 🏅swag | 🏪marketplace

    opened by imgbot[bot] 0
Releases(1.0.14)
Owner
Sadman Samee
A Mobile dev, knows iOS, Android native, and Flutter development.
Sadman Samee
Cache - Nothing but Cache.

Cache doesn't claim to be unique in this area, but it's not another monster library that gives you a god's power. It does nothing but caching, but it does it well. It offers a good public API with out-of-box implementations and great customization possibilities. Cache utilizes Codable in Swift 4 to perform serialization.

HyperRedink 2.7k Dec 28, 2022
Apple Asset Cache (Content Cache) Tools

AssetCacheTool A library and tool for interacting with both the local and remote asset caches. This is based on research I did a few years ago on the

Kenneth Endfinger 21 Jan 5, 2023
Swift caching library

Cache A generic caching library for Swift. Cache depends on Foundation. This is still very much a work in progress. Usage Cache provides a simple Cach

Sam Soffes 210 Sep 9, 2022
A simple cache that can hold anything, including Swift items

CacheIsKing CacheIsKing is a simple cache that allows you to store any item, including objects, pure Swift structs, enums (with associated values), et

Christopher Luu 13 Jan 22, 2018
MrCode is a simple GitHub iPhone App that can cache Markdown content (include images in HTML) for read it later.

MrCode is a simple GitHub iPhone App that can cache Markdown content (include images in HTML) for read it later.

hao 448 Dec 19, 2022
A caching and consistency solution for immutable models.

?? Data Rocket Data is a model management system with persistence for immutable models. Motivation Immutability has many benefits, but keeping models

Peter Livesey 652 Nov 11, 2022
Melodic Caching for Swift

Johnny is a generic caching library written for Swift 4. Features Johnny can cache any model object that conforms to the Cachable protocol. Out-of-the

Zoltán Matók 37 Nov 21, 2022
Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift.

Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift. There already exists plenty of cache solutions, so why creat

Norsk rikskringkasting (NRK) 124 Nov 24, 2022
Carlos - A simple but flexible cache, written in Swift for iOS 13+ and WatchOS 6 apps.

Carlos A simple but flexible cache, written in Swift for iOS 13+ and WatchOS 6 apps. Breaking Changes Carlos 1.0.0 has been migrated from PiedPiper de

National Media & Tech 628 Dec 3, 2022
SwiftyCache is a dynamic and auto-managed cache written in Swift

SwiftyCache is a dynamic and auto-managed cache written in Swift. Unlike a simple cache system, it allows you to keep some data even in different executions. Guaranteeing persistence, when desired, without increasing the time required to save or recover data.

Antonio Guerra 3 Aug 28, 2022
iOS Offline Caching for Web Content

Mattress A Swift framework for storing entire web pages into a disk cache distinct from, but interoperable with, the standard NSURLCache layer. This i

BuzzFeed 522 Oct 25, 2022
A lightweight generic cache for iOS written in Swift with extra love for images.

Haneke is a lightweight generic cache for iOS and tvOS written in Swift 4. It's designed to be super-simple to use. Here's how you would initalize a J

Haneke 5.2k Dec 29, 2022
Cache library for videos for React Native

@lowkey/react-native-cache Cache everything Installation npm install @lowkey/react-native-cache Usage import ReactNativeCache from "@lowkey/react-nati

Max Prokopenko 1 Oct 1, 2021
Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU.

Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU. Features Thread safe: Implement by dispatch_sema

Cheer 268 Nov 21, 2022
Everyone tries to implement a cache at some point in their iOS app’s lifecycle, and this is ours.

Everyone tries to implement a cache at some point in their app’s lifecycle, and this is ours. This is a library that allows people to cache NSData wit

Spotify 1.2k Dec 28, 2022
Fast, non-deadlocking parallel object cache for iOS, tvOS and OS X

PINCache Fast, non-deadlocking parallel object cache for iOS and OS X. PINCache is a fork of TMCache re-architected to fix issues with deadlocking cau

Pinterest 2.6k Dec 28, 2022
🏈 Cache CocoaPods for faster rebuild and indexing Xcode project.

Motivation Working on a project with a huge amount of pods I had some troubles: - Slow and unnecessary indexing of pods targets, which implementation

Vyacheslav Khorkov 487 Jan 5, 2023
MemoryCache - type-safe, thread-safe memory cache class in Swift

MemoryCache is a memory cache class in swift. The MemoryCache class incorporates LRU policies, which ensure that a cache doesn’t

Yusuke Morishita 74 Nov 24, 2022
High performance cache framework for iOS.

YYCache High performance cache framework for iOS. (It's a component of YYKit) Performance You may download and compile the latest version of sqlite an

null 2.3k Dec 16, 2022