Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU.

Related tags

Cache Track
Overview

Language Pod Version Pod Platform Carthage Compatible License

Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU.

Features

  • Thread safe: Implement by dispatch_semaphore_t lock and DISPATCH_QUEUE_CONCURRENT. Cache methods are thread safe and no deadlock.

  • LRU: Implement by linkedlist, it`s fast. You can manage a cache through functions to limit size, age of entries and memory usage to eliminate least recently used object.

  • Support async and sync operation.

  • Cache implement SequenceType Generator, support subscrip for ... in map flapmap filter...

Use

Base use

Support Sync and Async Set, Get, RemoveObject, RemoveAll and Subscript.

let track = Cache.shareInstance

track.set(object: "object", forKey: "key")

track.object(forKey: "key")

track.removeObject(forKey: "key") { (cache, key, object) in }

track.removeAllObjects { (cache, key, object) in }

track["key"] = "object"

print(track["key"])

Other use

MemoryCache and DiskCache has feature of LRU, so they can eliminate least recently used object according countLimit, costLimit and ageLimit.

let diskcache = DiskCache.shareInstance

diskcache.countLimit = 20

diskcache.costLimit = 1024 * 10

let memorycache = MemoryCache.shareInstance

memorycache.trim(toAge: 1000) { (cache, key, object) in }

memorycache.trim(toCount: 10) { (cache, key, object) in }

New features: SequenceType Generator

Cache support thread safe for ... in map forEache...

let cache: Cache = Cache.shareInstance

for i in 1 ... 5 {
    cache.set(object: "\(i)", forKey: "\(i)")
}

for object in cache {
    print(object)
}
output: ("5", 5) ("4", 4) ("3", 3) ("2", 2) ("1", 1)
cache.forEach {
    print($0)
}
output: ("1", 1) ("2", 2) ("3", 3) ("4", 4) ("5", 5)
let values = cache.map { return $0 }

print(values)
output: [("5", 5), ("4", 4), ("3", 3), ("2", 2), ("1", 1)]

Installation

CocoaPods

Support Swift 5.0

pod 'Track', :git => 'https://github.com/maquannene/Track.git', :branch => 'master'

Manually

  1. Download and drop /Track folder in your project.
  2. Congratulations!

Thanks

Thanks YYCache,PINCache very much. Some ideas from them.

License

Track is released under the MIT license.

如果来自天朝点击查看更多实现细节文章

You might also like...
🏈 Cache CocoaPods for faster rebuild and indexing Xcode project.
🏈 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

A lightweight generic cache for iOS written in Swift with extra love for images.
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

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

High performance cache framework for iOS.
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

UITableView cell cache that cures scroll-lags on cell instantiating

UITableView + Cache https://github.com/Kilograpp/UITableView-Cache UITableView cell cache that cures scroll-lags on a cell instantiating. Introduction

💾 Simple memory & disk cache

Cache 💾 Simple memory & disk cache Usage 🧑‍💻 Default let cache = CacheString() try memory.save("MyValue", forKey: "MyKey") let cached = try cac

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

CachedAsyncImage is the simplest way to add cache to your AsyncImage.

CachedAsyncImage 🗃️ CachedAsyncImage is AsyncImage, but with cache capabilities. Usage CachedAsyncImage has the exact same API and behavior as AsyncI

XCRemoteCache is a remote cache tool for Xcode projects.
XCRemoteCache is a remote cache tool for Xcode projects.

XCRemoteCache is a remote cache tool for Xcode projects. It reuses target artifacts generated on a remote machine, served from a simple REST server. H

Comments
  • Can't make it work

    Can't make it work

    Hello, i'm trying cache images with DiskCache, but when i try to read nothing is returned diskCache.set(object: cache, forKey: "\(step)") { (cache, key, object) in debugPrint(self.diskCache["\(self.step)"] ?? "") } console prints ""

    opened by vadimvitvickiy 2
  • Manual Installation

    Manual Installation

    CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

    It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

    opened by lfarah 2
  • 两块不同的缓存,数据粘连

    两块不同的缓存,数据粘连

            let cache: Cache = Cache.shareInstance
            cache.set(object: "2222" as NSCoding, forKey: "2223")
            let cache2 = Cache(name: "cache2")
    
            cache2?.set(object: "1111" as NSCoding, forKey: "1111")
            print(cache.object(forKey: "1111") ?? "none")
    

    输入结果为:1111

    opened by hanchenhao 1
Owner
Cheer
iOS Developer, Cooker
Cheer
Awesome Cache Delightful on-disk cache (written in Swift). Backed by NSCache for maximum performance

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

Alexander Schuch 1.3k Dec 29, 2022
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
Thread -safe access to a lazily retrieved value, with optional validity checking

SerialUpdatingValue Thread-safe access to a lazily retrieved value, with optional validity checking Motivation Swift's Structured Concurrency provides

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

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

Sadman Samee 122 Dec 28, 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
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