Coder Social home page Coder Social logo

justinmfischer / swiftlylru Goto Github PK

View Code? Open in Web Editor NEW
57.0 3.0 12.0 28 KB

SwiftlyLRU is a pure Swift Least Recently Used "LRU" Cache. Older items that have not been recently used are discarded from the cache. This is the ideal pattern for large lists and infinite scrolling. The time, space complexity is O(1) and thanks to generics any values can be stored. Simply drag SwiftlyLRU.swift into your project to get started!

License: MIT License

Swift 100.00%

swiftlylru's Introduction

SwiftlyLRU Swift 4.0 Xcode 9

//
// Example
//

import Foundation

//...

//Create cache with capacity
var cache = SwiftlyLRU<String, Float>(capacity: 7)

    //Add Key, Value pairs
    cache["AAPL"] = 114.63
    cache["GOOG"] = 533.75
    cache["YHOO"] = 50.67
    cache["TWTR"] = 38.91
    cache["BABA"] = 109.89
    cache["YELP"] = 55.17
    cache["BABA"] = 109.80
    cache["TSLA"] = 231.43
    cache["AAPL"] = 113.41
    cache["GOOG"] = 533.60
    cache["AAPL"] = 113.01
    
    //Retrieve
    if let item = cache["AAPL"] {
        println("Key: AAPL Value: \(item)")
    } else {
        println("Item not found.")
    }
    
/* OUTPUT    
    Key: AAPL Value: 113.01
*/
    
    //Describe
    println(cache)

/* OUTPUT
    SwiftlyLRU Cache(7) 
    Key: AAPL Value: Optional(113.01) 
    Key: GOOG Value: Optional(533.6) 
    Key: TSLA Value: Optional(231.43) 
    Key: BABA Value: Optional(109.8) 
    Key: YELP Value: Optional(55.17) 
    Key: TWTR Value: Optional(38.91) 
    Key: YHOO Value: Optional(50.67) 
*/

//...

##Instillation: Simply drag SwiftlyLRU.swift file from the Source folder into your target project.

##Reference Notes: SwiftlyLRU uses generics and can store any value including nil as the value and keys that confirm to the Hashable protocol. Swift’s basic types (such as String, Int, Double, Float, and Bool) are hashable by default.

##Performance: Time: O(1), Space: O(1) assumes no collisions into the hashtable.

##NSCoding support: Sebastian Bub created a nice wrapper that is NSCoding-compliant via the NSKeyedArchiver. Please checkout his project to support this behavior. https://github.com/sebbu/SwiftlyLRU

##Legal: // // Copyright (c) 2014 Justin M Fischer // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Created by JUSTIN M FISCHER on 12/1/14. // Copyright (c) 2013 Justin M Fischer. All rights reserved. //

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.