Created
October 29, 2020 18:16
-
-
Save the-spectator/d4218e355d2ad894feaf8ce66b7b6b9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LRUCache | |
attr_reader :lookup, :size | |
def initialize(size) | |
@lookup = {} | |
@size = size | |
end | |
def get(key) | |
return unless lookup[key] | |
lookup[key] = lookup.delete(key) | |
end | |
def set(key, value) | |
lookup.shift if lookup.size > size | |
lookup.delete(key) | |
lookup[key] = value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment