New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rufus-lru

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rufus-lru

  • 1.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

rufus-lru

Build Status Gem Version

LruHash class, a Hash with a max size, controlled by a LRU mechanism.

getting it

gem install rufus-lru

or better, simply add to your Gemfile

gem 'rufus-lru'

usage

It's a regular hash, but you have to set a maxsize at instantiation.

Once the maxsize is reached, the hash will discard the element that was the least recently used (hence LRU).

require 'rufus-lru'

h = Rufus::Lru::Hash.new(3)

5.times { |i| h[i] = "a" * i }

puts h.inspect # >> {2=>"aa", 3=>"aaa", 4=>"aaaa"}

h[:newer] = 'b'

puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}

Rufus::Lru::Hash isn't thread-safe, if you need something that is, use Rufus::Lru::SynchronizedHash

require 'rufus-lru'

h = Rufus::Lru::SynchronizedHash.new(3)

# ...

It's possible to squeeze LruHash manually:

h = Rufus::Lru::Hash.new(33, :auto_squeeze => false)
  # or
#h = Rufus::Lru::Hash.new(33)
#h.auto_squeeze = false

# ... values keep accumulating ...

# when a squeeze is needed...
h.squeeze!

LruHash accepts on initialization a :on_removal option. It can be set to a Symbol, which is then used as the method name to call on the value just removed:

require 'rufus-lru'

class ObjectWithDestructor; def clear; puts 'Destructor called'; end; end

h = LruHash.new(1, :on_removal => :clear)

h[:one] = ObjectWithDestructor.new
h[:two] = nil # :one is being removed >> "Destructor called"

Or it can be set to a lambda:

require 'rufus-lru'

seen = []
h = Rufus::Lru::Hash.new(
  1,
  :on_removal => lambda { |val| seen << val.object_id })

h[:one] = 'abc'
h[:two] = 'xyz'

# seen ends up with the object_id of the 'abc' String instance...

The value of on_removal can be set later on.

h.on_removal = :destroy
h.on_removal = lambda { |val| bodycount += 1 if val.is_a?(Martian) }

auto_squeeze and on_removal were originally contributed by Gleb Kuzmenko.

dependencies

None.

mailing list

On the rufus-ruby list:

http://groups.google.com/group/rufus-ruby

issue tracker

http://github.com/jmettraux/rufus-lru/issues

irc

irc.freenode.net #ruote

source

http://github.com/jmettraux/rufus-lru

git clone git://github.com/jmettraux/rufus-lru.git

author

John Mettraux, jmettraux@gmail.com, http://lambda.io/jmettraux

contributors and help

see CREDITS.txt

license

MIT

FAQs

Package last updated on 09 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc