{"id":18000789,"url":"https://github.com/derrickreimer/mini_cache","last_synced_at":"2025-03-26T07:32:15.013Z","repository":{"id":56883826,"uuid":"5136341","full_name":"derrickreimer/mini_cache","owner":"derrickreimer","description":"A lightweight in-memory cache for Ruby objects","archived":false,"fork":false,"pushed_at":"2018-10-02T14:24:12.000Z","size":29,"stargazers_count":35,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T18:51:15.228Z","etag":null,"topics":["cache","ruby"],"latest_commit_sha":null,"homepage":"https://github.com/djreimer/mini_cache","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/derrickreimer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-07-21T19:52:34.000Z","updated_at":"2025-01-12T22:59:41.000Z","dependencies_parsed_at":"2022-08-21T00:20:15.991Z","dependency_job_id":null,"html_url":"https://github.com/derrickreimer/mini_cache","commit_stats":null,"previous_names":["djreimer/mini_cache"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derrickreimer%2Fmini_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derrickreimer%2Fmini_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derrickreimer%2Fmini_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derrickreimer%2Fmini_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derrickreimer","download_url":"https://codeload.github.com/derrickreimer/mini_cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245611904,"owners_count":20643924,"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":["cache","ruby"],"created_at":"2024-10-29T23:14:09.492Z","updated_at":"2025-03-26T07:32:14.608Z","avatar_url":"https://github.com/derrickreimer.png","language":"Ruby","readme":"# MiniCache\n\n[![Build Status](https://travis-ci.org/derrickreimer/mini_cache.svg?branch=master)](https://travis-ci.org/derrickreimer/mini_cache)\n\nMiniCache is a lightweight in-memory key-value store for Ruby objects.\nThis gem requires Ruby version 2.3.0 or higher.\n\n**Important Note:** This gem uses standard `Hash` under the covers to store data. By design, Ruby hashes are not thread-safe, so MiniCache is not thread-safe. If you need concurrent access across threads/processes, I recommend using a \"real\" cache store like Memcached or Redis.\n\n## Motivation\n\nIt is common practice to cache certain values on an object that are\ncomputationally expensive to obtain, such as a property that requires a\ndatabase query.\n\nThe simplest way to do this is by storing the value in an instance variable:\n\n```ruby\nclass Account\n  def calculate_balance\n    # Do something expensive.\n  end\n\n  def balance\n    @balance ||= self.calculate_balance\n  end\nend\n```\n\nWhile this method works in many scenarios, it fails when the value you\nneed to cache is:\n\n- Either `nil` or `false`\n- Dependent on a particular argument passed to the method\n\nHere's a demonstration of how MiniCache solves this problem:\n\n```ruby\nclass Account\n  def lookup_role(user)\n    # Execute a database query to find the user's role.\n  end\n\n  def role(user)\n    # Perform the lookup once and cache the value. We can't use\n    #\n    #   @role ||= lookup_user(user)\n    #\n    # because the value depends on the user argument. Also, the\n    # value could be nil if the user does not actually have a role.\n    # You can probably see how the solution could get pretty ugly.\n    # This is where MiniCache comes into play.\n    self.cache.get_or_set(\"role-#{user.id}\") do\n      self.lookup_role(user)\n    end\n  end\n\n  def cache\n    @cache ||= MiniCache::Store.new\n  end\nend\n```\n\nThe `#get_or_set` method works similarly to the `||=` operator, except it\nknows how to handle `false` and `nil` values and it's keyed off of a unique string ID.\nProblem solved!\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'mini_cache'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install mini_cache\n\n## Usage\n\nTo create a new MiniCache store object, just initialize it:\n\n```ruby\nstore = MiniCache::Store.new\n\n# Optionally pass in a Hash of data\nstore = MiniCache::Store.new(name: 'Derrick', occupation: 'Developer')\n```\n\nSet and retrieve data using `#get` and `#set`:\n\n```ruby\n# Pass in the value as an argument or block\nstore.set('age', 24)\nstore.set('birth_year') { 1988 }\n\nstore.get('age')\n# =\u003e 24\n\nstore.get('birth_year')\n# =\u003e 1988\n\n# Sets an expiration time to cache (in seconds)\nstore.set('age', 24, expires_in: 60)\nstore.set('day', expires_in: 60) { 12 }\nstore.set('birth_year') { MiniCache::Data.new(1988, 60) }\n\nstore.get('age')\n# =\u003e 24\n\nstore.get('day')\n# =\u003e 12\n\nstore.get('birth_year')\n# =\u003e 1988\n\nsleep(60)\n\nstore.get('age')\n# =\u003e nil\n\nstore.get('day')\n# =\u003e nil\n\nstore.get('birth_year')\n#=\u003e nil\n```\n\nUse the `#get_or_set` method to either set the value if it hasn't already been\nset, or get the value that was already set.\n\n```ruby\nstore.set('birth_year') { 1988 }\n#=\u003e 1988\n\nstore.get_or_set('birth_year') { 1964 }\n#=\u003e 1988  # Did not overwrite previously set value\n\n# You may also set an expiration time (in seconds):\n\nstore.get_or_set('age', expires_in: 60) { 24 }\n#=\u003e 24\n\nstore.get_or_set('birth_year') do\n  MiniCache::Data.new(1988, expires_in: 60)\nend\n#=\u003e 1988\n\nsleep(60)\n\nstore.get_or_set('age', expires_in: 60) { 28 }\n#=\u003e 28\n\nstore.get_or_set('birth_year') do\n  MiniCache::Data.new(1964, expires_in: 60)\nend\n#=\u003e 1964\n```\n\nOther convenience methods:\n\n- `#set?(key)`: Checks to see if a value has been set for a given key\n- `#unset(key)`: Removes a key-value pair for a given key.\n- `#reset`: Clears the cache.\n- `#load(hash)`: Loads a hash of data into the cache (appended to existing data).\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderrickreimer%2Fmini_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderrickreimer%2Fmini_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderrickreimer%2Fmini_cache/lists"}