Skip to content

Instantly share code, notes, and snippets.

@Ptico
Created November 5, 2013 13:47
Show Gist options
  • Save Ptico/7319237 to your computer and use it in GitHub Desktop.
Save Ptico/7319237 to your computer and use it in GitHub Desktop.
class Cache
attr_reader :limit, :store, :queue
def [](key)
heat(key) if result = store[key]
result
end
def []=(key, value)
store[key] = value
heat(key)
pop if length > limit
value
end
def heat(key)
queue.delete(key)
queue.unshift(key)
end
def pop
store.delete(queue.pop)
end
def length
store.length
end
private
def initialize(limit=100)
@limit = limit
@store = {}
@queue = []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment