Code Monkey home page Code Monkey logo

cachekit's Introduction

CacheKit

[CI Status](https://travis-ci.org/David Beck/CacheKit) Version License Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

You can access a cache similarly to NSMutableDictionary or NSCache:

[cache setObject:@1 forKey:@"A"];
[cache objectForKey:@"A"]; // returns @1 if it still exists

In addition, you can specify an expiration date:

[cache setObject:@1 forKey:@"A" expiresIn:30.0];
[cache objectForKey:@"A"]; // returns @1
// 31 seconds later...
[cache objectForKey:@"A"]; // returns nil

There are several reasons why -objectForKey: would return nil. The object could have expired, deleted in a background thread, or in the case of an in memory cache, the object could have been evicted during a memory warning. The best way to use an CKCache is to provide a content block in the case of a cache miss.

[cache objectForKey:@"A" withContent:^{
    return @1;
}]; // returns @1 if the object for @"A" doesn't exist or has expired

You can use shared caches if you just want to quickly throw data into something. Just make sure you use globally unique keys. For instance, you could prefix keys with a class name.

[[CKSQLiteCache sharedCache] objectForKey:@"MyViewController.A" withContent:^{
    return @1;
}];

There are several different subclasses of CKCache for different purposes:

CKMemoryCache

This is a wrapper around NSCache. It's objects are not persisted between launches.

CKFileCache

This is a persistent cache that stores it's objects as files on disk. Use this cache if the objects you are storeing are extremely large. Each object is it's own file, encoded using NSCoding and stored in the caches directory under the name of the cache and key. Make sure that the keys you use are valid file names and the objects conform to NSCoding.

The cache also has an internal NSCache for qucker access.

CKSQLiteCache

This is a persistent cache that uses an SQLite database to store it's objects. The database is saved to the cache directory by the name of the cache. Each object is encoded using NSCoding and stored as a BLOB in the database. Make sure that the objects conform to NSCoding. Use this type of cache for most persistent caching. The only reason you might want to use CKFileCache instead is if your objects were very large.

FMDB is used internally for the interface to SQLite.

The cache also has an internal NSCache for qucker access.

CKNullCache

A useless cache that doesn't store anything. Use this if you want to test your app without caching and you want to quickly swap out another cache type. Make sure to provide fallback content blocks when fetching data since the bare -objectForKey: will always return nil no matter what.

Requirements

CacheKit requires iOS 6 or 10.8.

Installation

CacheKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "CacheKit"

License

CacheKit is available under the MIT license. See the LICENSE file for more info.

cachekit's People

Contributors

davbeck avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.