All Projects → aschuch → Awesomecache

aschuch / Awesomecache

Licence: mit
Delightful on-disk cache (written in Swift)

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Awesomecache

Kirby3 Autoid
Automatic unique ID for Pages, Files and Structures including performant helpers to retrieve them. Bonus: Tiny-URL.
Stars: ✭ 58 (-95.26%)
Mutual labels:  cache
Cashew
A simple and elegant yet powerful HTTP client cache for .NET
Stars: ✭ 70 (-94.28%)
Mutual labels:  cache
Realpath cache tuner
Simple script that helps tuning PHP realpath cache
Stars: ✭ 78 (-93.62%)
Mutual labels:  cache
Django Qsessions
Extended session backends for Django (Sessions store IP, User Agent, and foreign key to User)
Stars: ✭ 64 (-94.77%)
Mutual labels:  cache
Kong Plugin Response Cache
A Kong plugin that will cache responses in redis
Stars: ✭ 66 (-94.6%)
Mutual labels:  cache
Postgresql Provider
PostgreSQL Provider for the Vapor web framework.
Stars: ✭ 71 (-94.19%)
Mutual labels:  cache
Apollo Invalidation Policies
An extension of the Apollo 3 cache with support for type-based invalidation policies.
Stars: ✭ 55 (-95.5%)
Mutual labels:  cache
Memorystore
express-session full featured MemoryStore layer without leaks!
Stars: ✭ 79 (-93.54%)
Mutual labels:  cache
Twig Cache Extension
Stars: ✭ 67 (-94.52%)
Mutual labels:  cache
Keshi
A better in-memory cache for Node and the browser
Stars: ✭ 75 (-93.87%)
Mutual labels:  cache
Jsonapi React
A minimal JSON:API client and React hooks for fetching, updating, and caching remote data.
Stars: ✭ 65 (-94.69%)
Mutual labels:  cache
Apollo Cache Invalidation
Experimental cache invalidation tools for Apollo.
Stars: ✭ 66 (-94.6%)
Mutual labels:  cache
Terraform Aws Elasticache Redis
Terraform module to provision an ElastiCache Redis Cluster
Stars: ✭ 73 (-94.03%)
Mutual labels:  cache
Cache Macro
A procedural attribute macro to automatically cache the results of a function call with given args.
Stars: ✭ 59 (-95.18%)
Mutual labels:  cache
Comet Cache
An advanced WordPress® caching plugin inspired by simplicity.
Stars: ✭ 78 (-93.62%)
Mutual labels:  cache
Oss.common
oss基础类库,主要涉及基础实体,加密算法,xml序列化,以及其他扩展方法等
Stars: ✭ 56 (-95.42%)
Mutual labels:  cache
Zio Tls Http
100% non-blocking, Java NIO only( inspired by zio-nio) , JSON HTTP server based on Scala ZIO library. Everything including TLS encryption modeled as ZIO effects, convenient route DSL similar to https4s, up to 30K TPS local JSON transaction with 25 threads on 6 cores(i7) with ZIO fibers.
Stars: ✭ 71 (-94.19%)
Mutual labels:  cache
Minicache
📦 Python memory caching utilities
Stars: ✭ 79 (-93.54%)
Mutual labels:  cache
Gitcache
When clone from github.com, build mirror cache to improve clone speed
Stars: ✭ 77 (-93.7%)
Mutual labels:  cache
Cachemanage
🔥android缓存管理器,分为内存缓存和文件缓存两种 先取内存数据,没有再从文件缓存中取
Stars: ✭ 73 (-94.03%)
Mutual labels:  cache

Awesome Cache

Build Status CocoaPods Compatible Carthage compatible Swift 3.0 Platform

Delightful on-disk cache (written in Swift). Backed by NSCache for maximum performance and support for expiry of single objects.

Usage

do {
    let cache = try Cache<NSString>(name: "awesomeCache")

    cache["name"] = "Alex"
    let name = cache["name"]
    cache["name"] = nil
} catch _ {
    print("Something went wrong :(")
}

Sync by design

AwesomeCache >= 3.0 is designed to have a sync API, making it easy to reason about the actual contents of the cache. This decision has been made based on feedback from the community, to keep the API of AwesomeCache small and easy to use.

The internals of the cache use a concurrent dispatch queue, that syncs reads and writes for thread safety. In case a particular caching operation blocks your main thread for too long, consider offloading the read and write operations to a different thread.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
	cache["name"] = "Alex"
}

Cache expiry

Objects can also be cached for a certain period of time.

cache.setObject("Alex", forKey: "name", expires: .never) // same as cache["name"] = "Alex"
cache.setObject("Alex", forKey: "name", expires: .seconds(2)) // name expires in 2 seconds
cache.setObject("Alex", forKey: "name", expires: .date(Date(timeIntervalSince1970: 1428364800))) // name expires on 4th of July 2015

If an object is accessed after its expiry date, it is automatically removed from the cache and deleted from disk. However, you are responsible to delete expired objects regularly by calling removeExpiredObjects (e.g. on app launch).

Awesome API Caching

API responses are usually cached for a specific period of time. AwesomeCache provides an easy method to cache a block of asynchronous tasks.

cache.setObject(forKey: "name", cacheBlock: { success, failure in
  // Perform tasks, e.g. call an API
  let response = ...

  success(response, .seconds(300)) // Cache response for 5 minutes
  // ... or failure(error)
}, completion: { object, isLoadedFromCache, error in
	if object {
	 	// object is now cached
	}
})

If the cache already contains an object, the completion block is called with the cached object immediately.

If no object is found or the cached object is already expired, the cacheBlock is called. You may perform any tasks (e.g. network calls) within this block. Upon completion of these tasks, make sure to call the success or failure block that is passed to the cacheBlock. The cacheBlock will not be re-evaluated until the object is expired or manually deleted.

The completion block is invoked as soon as the cacheBlock is finished and the object is cached.

Version Compatibility

Current Swift compatibility breakdown:

Swift Version Framework Version
3.0 5.x
2.3 4.x
2.2 3.x

Installation

Carthage

Add the following line to your Cartfile.

github "aschuch/AwesomeCache"

Then run carthage update.

CocoaPods

Add the following line to your Podfile.

pod "AwesomeCache", "~> 3.0"

Then run pod install with CocoaPods 0.36 or newer.

Manually

Just drag and drop the two .swift files as well as the NSKeyedUnarchiverWrapper.h/.m files in the AwesomeCache folder into your project. If you are adding AwesomeCache to a Swift project, you also need to add an import for NSKeyedUnarchiverWrapper.h to your bridging header.

Tests

Open the Xcode project and press ⌘-U to run the tests.

Alternatively, all tests can be run in the terminal using xctool.

xctool -scheme AwesomeCacheTests -sdk iphonesimulator test

Contributing

  • Create something awesome, make the code better, add some functionality, whatever (this is the hardest part).
  • Fork it
  • Create new branch to make your changes
  • Commit all your changes to your branch
  • Submit a pull request

Contact

Feel free to get in touch.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].