Code Monkey home page Code Monkey logo

track's Introduction

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

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

Support Swift 3.0

pod 'Track'

Support Swift 2.3

pod 'Track', :git => 'https://github.com/maquannene/Track.git', :branch => 'feature/S-2.3'

If you want to use the new features of Track

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

Carthage

github "maquannene/Track"

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.

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

track's People

Contributors

maquannene avatar hons82 avatar lfarah avatar

Watchers

James Cloos avatar Lee 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.