GithubHelp home page GithubHelp logo

pony001 / lrucache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gn0meavp/lrucache

0.0 1.0 0.0 15 KB

Objective-C Implementation of the LRU Cache

License: MIT License

Objective-C 97.35% Ruby 2.65%

lrucache's Introduction

LRUCache

Objective-C Implementation of the LRU Cache. http://en.wikipedia.org/wiki/Cache_algorithms

Description

Provide easy access to the most recently used objects by key (used NSDictionary and Linked List inside).

Automatically remove least recently used objects if the cache is out of space.

Thread-safe

Append and receive elements from cache is thread-safe (used dispatch_queue inside).

Complexity

Getting an element: O(1)

Adding an element: O(1)

Sample of usage

// initial state of cache with capacity 3
//      -,-,-
LRUCache *cache = [[LRUCache alloc] initWithCapacity:3];

// add 1
//      1,-,-
[cache setObject:@"1" forKey:@"1"];

// add 2
//      2,1,-
[cache setObject:@"2" forKey:@"2"];

// add 3
//      3,2,1
[cache setObject:@"3" forKey:@"3"];

// add 4
//      4,3,2       (1 removed as least recently used)
[cache setObject:@"4" forKey:@"4"];

// request 2
//      2,4,3       (2 goes to the top as most recently used)
NSLog(@"%@", [cache objectForKey:@"2"]);

// add 5
//      5,2,4       (3 removed as least recently used)
[cache setObject:@"5" forKey:@"5"];

NSCoding

Supports NSCoding protocol, so the cache can be archived/unarchived with all stored values (which also should support NSCoding protocol in that way).

lrucache's People

Contributors

gn0meavp avatar pony001 avatar

Watchers

 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.