Clever memoization helper that uses Ruby internals instead of meta-programming.
Requiring the gem will add a memo
method to the Object
class so that you can just use it like so:
def load_stuff_from_the_web
memo do
# some expensive operation like HTTP request
# the return value of the block will be memoized
HTTPClient.get('https://github.com/phoet/memo-it')
end
end
Per default, the memoization will be done on an instance level, so every instance of an object will have it's own memoization namespace:
class Loader
def initialize(url)
@url = url
end
def content
memo do
# fetch the url content from the web
end
end
end
issues_loader = Loader.new('https://github.com/phoet/memo-it/issues')
pulls_loader = Loader.new('https://github.com/phoet/memo-it/pulls')
issues_loader.content # load & memoize the issues
pulls_loader.content # load & memoize the pulls
But you can also memoize on a global/class scope. This is needed if you want to use Memo::It in dynamically instanciated objects like Rails helpers:
module WebHelper
def content(url)
Memo::It.memo do
# fetch the url content from the web
end
end
end
In case you want to memoize something that has parameters, Memo::It will just use all local variables in scope to determine the memoization:
def load_repo(name = 'memo-it')
memo do
# in this case the result will be memoized per name
HTTPClient.get("https://github.com/phoet/#{name}")
end
end
If, on the other hand, you want to memoize parameters but ignore one of them,
you can do this by adding it to the :except
list:
def load_repo(name = 'memo-it', time = Time.now)
memo(except: :time) do
# in this case the result will be memoized per name
HTTPClient.get("https://github.com/phoet/#{name}?time=#{time}")
end
end
Or provide a list of parameters to except:
def load_repo(name = 'memo-it', time = Time.now, other = 'irrelevant')
memo(except: [:time, :other]) do
# in this case the result will be memoized per name
HTTPClient.get("https://github.com/phoet/#{name}?time=#{time}&other=#{other}")
end
end
To be symmetric, it's also possible to define one or more parameters through the :only
key:
def load_repo(name = 'memo-it', time = Time.now, format = 'json')
memo(only: [:name, :format]) do
# in this case the result will be memoized per name & format
HTTPClient.get("https://github.com/phoet/#{name}?time=#{time}&format=#{format}")
end
end
Provide your own memoization keys through the :provided
key:
def load_repo(name = 'memo-it')
memo(provided: Date.today.day_of_week) do
# in this case the result will be memoized per name and day_of_week
HTTPClient.get("https://github.com/phoet/#{name}")
end
end
In case you would like to disable memoization (ie. for testing) you can disable Memo::It:
# enabled is default
Memo.enabled? # => true
# disable memoization globally
Memo.disable
Memo.enabled? # => false
# re-enable memoization
Memo.enable
Memo.enabled? # => true
If you want to call memo
twice within the same line of code, you would need provide a custom key through the :provided
argument.
This is not recommended through. A better alternative would be to put both calls into their own sub-methods and call those instead.
Compared to other memoization frameworks, the memo
method requires more computation and is slower by size of a magnitude: #6
Do not use this library to optimize hot code-paths! Use it to cache really slow things such as network-requests or generation of large strings like JSON objects etc.
Whatever you do, benchmark your code in order to see if the memoization strategy you use is a good fit for your use-case.
Add this line to your application's Gemfile:
gem 'memo-it'
And then execute:
$ bundle
Or install it yourself as:
$ gem install memo-it
If you don't want to include yet another Gem, just run this in your shell:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/phoet/memo-it/master/bin/install)"
See CHANGELOG.md.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/phoet/memo-it. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.