ActiveMemoize
ActiveMemoize provides an API caching and memoizing local expensive calculations including those with parameters.
The flexible API allows you to memoize results using class or instance level cache.
Installation
Add this line to your application's Gemfile:
gem 'active_memoize'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_memoize
Table of Contents
Klass
Class level memoization is the quickest way to get up and running using your cache, but provides the least amount of flexibility. You can only cache results without access to any information about your cache.
class Movies
extend ActiveMemoize::Klass
def random
HTTP.get('http://movies.com/any')
end
memoize :random
def search(title)
HTTP.get("http://movies.com?title=#{title}")
end
memoize :search, as: :find
end
Instance
Instance level memoization is a more involved way to setup your cache, but provides the most amount of flexibility. You can access almost all methods
in the instance.rb
file.
class Movies
def cache
@cache ||= ActiveMemoize::Instance.new
end
def all
cache.memoize { HTTP.get("http://movies.com/all") }
end
def random
cache['random'] ||= HTTP.get('http://movies.com/any')
end
def search(title)
cache.memoize(as: :find, refresh: !cache.empty?) do
HTTP.get("http://movies.com?title=#{title}")
end
end
end
Contributing
Your contribution is welcome.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request