{"id":15033139,"url":"https://github.com/phoet/memo-it","last_synced_at":"2025-04-09T21:24:00.176Z","repository":{"id":56883293,"uuid":"78198800","full_name":"phoet/memo-it","owner":"phoet","description":":inbox_tray: :outbox_tray: simple yet clever memoization helper with parameter support","archived":false,"fork":false,"pushed_at":"2019-01-27T20:38:20.000Z","size":31,"stargazers_count":30,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-23T23:23:01.462Z","etag":null,"topics":["memoization","memoize","ruby"],"latest_commit_sha":null,"homepage":"https://github.com/phoet/memo-it#readme","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phoet.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-06T10:49:36.000Z","updated_at":"2024-05-11T09:39:12.000Z","dependencies_parsed_at":"2022-08-20T22:31:02.490Z","dependency_job_id":null,"html_url":"https://github.com/phoet/memo-it","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phoet%2Fmemo-it","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phoet%2Fmemo-it/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phoet%2Fmemo-it/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phoet%2Fmemo-it/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phoet","download_url":"https://codeload.github.com/phoet/memo-it/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248113219,"owners_count":21049806,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["memoization","memoize","ruby"],"created_at":"2024-09-24T20:20:12.933Z","updated_at":"2025-04-09T21:24:00.149Z","avatar_url":"https://github.com/phoet.png","language":"Ruby","readme":"# Memo::It\n\nClever memoization helper that uses Ruby internals instead of meta-programming. [![Build Status](https://travis-ci.org/phoet/memo-it.svg?branch=master)](https://travis-ci.org/phoet/memo-it)\n\n## Using Memo::It\n\n### Basic Usage\n\nRequiring the gem will add a `memo` method to the `Object` class so that you can just use it like so:\n\n```ruby\n  def load_stuff_from_the_web\n    memo do\n      # some expensive operation like HTTP request\n      # the return value of the block will be memoized\n      HTTPClient.get('https://github.com/phoet/memo-it')\n    end\n  end\n```\n\n### Instance vs Class memoization\n\nPer default, the memoization will be done on an instance level,\nso every instance of an object will have it's own memoization namespace:\n\n```ruby\n  class Loader\n    def initialize(url)\n      @url = url\n    end\n\n    def content\n      memo do\n        # fetch the url content from the web\n      end\n    end\n  end\n\n  issues_loader = Loader.new('https://github.com/phoet/memo-it/issues')\n  pulls_loader  = Loader.new('https://github.com/phoet/memo-it/pulls')\n\n  issues_loader.content # load \u0026 memoize the issues\n  pulls_loader.content # load \u0026 memoize the pulls\n```\n\nBut you can also memoize on a global/class scope.\nThis is needed if you want to use Memo::It in dynamically instanciated objects like Rails helpers:\n\n```ruby\n  module WebHelper\n    def content(url)\n      Memo::It.memo do\n        # fetch the url content from the web\n      end\n    end\n  end\n```\n\n### Parameters as scopes\n\nIn case you want to memoize something that has parameters, Memo::It will just use all local variables in scope to determine the memoization:\n\n```ruby\n  def load_repo(name = 'memo-it')\n    memo do\n      # in this case the result will be memoized per name\n      HTTPClient.get(\"https://github.com/phoet/#{name}\")\n    end\n  end\n```\n\nIf, on the other hand, you want to memoize parameters but ignore one of them,\nyou can do this by adding it to the `:except` list:\n\n```ruby\n  def load_repo(name = 'memo-it', time = Time.now)\n    memo(except: :time) do\n      # in this case the result will be memoized per name\n      HTTPClient.get(\"https://github.com/phoet/#{name}?time=#{time}\")\n    end\n  end\n```\n\nOr provide a list of parameters to except:\n\n```ruby\n  def load_repo(name = 'memo-it', time = Time.now, other = 'irrelevant')\n    memo(except: [:time, :other]) do\n      # in this case the result will be memoized per name\n      HTTPClient.get(\"https://github.com/phoet/#{name}?time=#{time}\u0026other=#{other}\")\n    end\n  end\n```\n\nTo be symmetric, it's also possible to define one or more parameters through the `:only` key:\n\n```ruby\n  def load_repo(name = 'memo-it', time = Time.now, format = 'json')\n    memo(only: [:name, :format]) do\n      # in this case the result will be memoized per name \u0026 format\n      HTTPClient.get(\"https://github.com/phoet/#{name}?time=#{time}\u0026format=#{format}\")\n    end\n  end\n```\n\nProvide your own memoization keys through the `:provided` key:\n\n```ruby\n  def load_repo(name = 'memo-it')\n    memo(provided: Date.today.day_of_week) do\n      # in this case the result will be memoized per name and day_of_week\n      HTTPClient.get(\"https://github.com/phoet/#{name}\")\n    end\n  end\n```\n\n### Turning it on and off\n\nIn case you would like to disable memoization (ie. for testing) you can disable Memo::It:\n\n```ruby\n  # enabled is default\n  Memo.enabled? # =\u003e true\n\n  # disable memoization globally\n  Memo.disable\n  Memo.enabled? # =\u003e false\n\n  # re-enable memoization\n  Memo.enable\n  Memo.enabled? # =\u003e true\n```\n\n## Caveats\n\n### Multiple calls to memo on the same line of code\n\nIf you want to call `memo` twice within the same line of code, you would need provide a custom key through the `:provided` argument.\nThis is not recommended through. A better alternative would be to put both calls into their own sub-methods and call those instead.\n\n### Runtime-Speed\n\nCompared to other memoization frameworks, the `memo` method requires more computation and is slower by size of a magnitude: https://github.com/phoet/memo-it/issues/6\nDo 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.\nWhatever you do, benchmark your code in order to see if the memoization strategy you use is a good fit for your use-case.\n\n## Installation\n\n### As a Gem\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'memo-it'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install memo-it\n\n### Copy \u0026 Paste\n\nIf you don't want to include yet another Gem, just run this in your shell:\n\n    $ /usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/phoet/memo-it/master/bin/install)\"\n\n## Changelog\n\nSee [CHANGELOG.md](https://github.com/phoet/memo-it/blob/master/CHANGELOG.md).\n\n## Development\n\nAfter 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.\n\nTo 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](https://rubygems.org).\n\n## Contributing\n\nBug 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](http://contributor-covenant.org) code of conduct.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphoet%2Fmemo-it","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphoet%2Fmemo-it","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphoet%2Fmemo-it/lists"}