Project

lru_cache

0.0
No commit activity in last 3 years
No release in over 3 years
its performant / constant o(1)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
 Project Readme

LRUCache

It's a simple in-memory cache that expires the least recently used item in the cache when the count of items in the cache reaches a fixed limit.

Installation

$ gem install lru_cache

Usage

require "lru_cache"

# create a new cache with a 1,000 item limit
cache = LRUCache.new(1_000)

# get or set item in cache
cache["some key"] ||= expensive_operation

By default the LRUCache is not thread-safe. If you intend to access the cache in multi-threaded code, use LRUCache::ThreadSafe.

require "lru_cache/thread_safe"

# create a new thread-safe cache with a 1,000 item limit
cache = LRUCache::ThreadSafe.new(1_000)

# get or set item in cache
cache["some key"] ||= expensive_operation

Public Instance Methods

  • #clear empties the cache
  • #count returns the number of items currently in cache
  • #delete(key) removes an item from the cache
  • #get(key) returns the cached value or nil
  • #limit returns the cache limit (defaults to 1)
  • #limit=value sets the cache limit to value
  • #set(key, value) stores the value in the cache
  • #[key] alias for #get(key)
  • #[key]=value alias for #set(key, value)

License

This code is released under the MIT license, and is copyright 2012 by Brendan Baldwin. Please see the accompanying LICENSE file for the full text of the license.