https://github.com/bsm/timed_lru
Simple, thread-safe LRU with (optional) TTLs and constant time operations
https://github.com/bsm/timed_lru
Last synced: about 1 year ago
JSON representation
Simple, thread-safe LRU with (optional) TTLs and constant time operations
- Host: GitHub
- URL: https://github.com/bsm/timed_lru
- Owner: bsm
- License: other
- Created: 2013-03-04T14:06:05.000Z (over 13 years ago)
- Default Branch: main
- Last Pushed: 2024-08-01T23:44:48.000Z (almost 2 years ago)
- Last Synced: 2025-03-18T22:46:27.123Z (about 1 year ago)
- Language: Ruby
- Size: 24.4 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Timed LRU
[](https://github.com/bsm/timed_lru/actions/workflows/ruby.yml)
[](https://gemnasium.com/bsm/timed_lru)
My implementation of a simple, thread-safe LRU with (optional) TTLs
and constant time operations. There are many LRUs for Ruby available but
I was unable to find one that matches all three requirements.
## Install
Install it via `gem`:
```ruby
gem install timed_lru
```
Or just bundle it with your project.
## Usage Example
```ruby
# Initialize with a max size (default: 100) and a TTL (default: none)
lru = TimedLRU.new max_size: 3, ttl: 5
# Add values
lru["a"] = "value 1"
lru["b"] = "value 2"
lru["c"] = "value 3"
lru.keys # => ["a", "b"]
# Wait a second
sleep(1)
# Add more values
lru["d"] = "value 4"
lru.keys # => ["b", "c", "d"]
# Sleep a little longer
sleep(4)
lru["c"] # => "value 3"
lru.keys # => ["c", "d"]
```