{"id":17942642,"url":"https://github.com/petercamilleri/counted_cache","last_synced_at":"2025-04-03T13:27:40.352Z","repository":{"id":59152422,"uuid":"172358165","full_name":"PeterCamilleri/counted_cache","owner":"PeterCamilleri","description":"A cache for mostly read data with a high cost of retrieval.","archived":false,"fork":false,"pushed_at":"2021-05-19T15:22:42.000Z","size":32,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T16:45:31.553Z","etag":null,"topics":["ruby","rubygem"],"latest_commit_sha":null,"homepage":null,"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/PeterCamilleri.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2019-02-24T16:00:25.000Z","updated_at":"2023-03-05T04:20:47.000Z","dependencies_parsed_at":"2022-09-13T11:00:49.459Z","dependency_job_id":null,"html_url":"https://github.com/PeterCamilleri/counted_cache","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fcounted_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fcounted_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fcounted_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fcounted_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PeterCamilleri","download_url":"https://codeload.github.com/PeterCamilleri/counted_cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247008791,"owners_count":20868424,"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":["ruby","rubygem"],"created_at":"2024-10-29T03:06:35.425Z","updated_at":"2025-04-03T13:27:40.335Z","avatar_url":"https://github.com/PeterCamilleri.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CountedCache\n\nThe counted_cache gem. A cache for use in cases where data is mostly only\nread and where the cost of rebuilding that data is high, and yet, the size\nof the data is too large to keep all of it in RAM all of the time.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'counted_cache'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install counted_cache\n\n## Usage\n\nCreating a counted cache is as simple as:\n\n```ruby\ncache_on_hand = CountedCache.new(10) {|key| retrieve_data_for(key) }\n```\n\nWhere:\n\n* The argument is the optional cache depth, which is the number of data\nitems kept in the cache.\n* The required block is used to fetch the data associated with the key when\nthat data is needed, but not currently in the cache.\n\nSo how do we use a counted cache to get at our data? That is really simple:\n\n```ruby\ndata = cache_on_hand[key]\n```\n\nThat's it! And that is the complete essentials guide. There are a very few\nextras to determine how well the cache is working. The hits and misses\nproperties record how many times the data was found in the cache (a hit) and\nhow many times it had to be retrieved (a miss).\n\n### Removing data from the cache.\n\nAt some point, data items will need to be removed from the cache to make room\nfor newer data requests.\n\nNormally when this happens, the old data can go away and no further action\nneeds to be taken. In some cases however, modified data will need to be written\nout to save any changes that may have been made to that data. This is done by\ngiving the cache a block that does the work of saving the data into wherever\nis identified by the key.\n\n```ruby\ncache_on_hand.set_save_block do |key, data|\n  # Code to save data at key goes here.\nend\n```\n\n### Example\n\nConsider the case of an application that uses embedded ruby (erb) files. This\ncould look like:\n\n```ruby\nerb_cache = CountedCache.new {|name| ERB.new(IO.read(name))}\n\n# Other code omitted.\n\nview_text = erb_cache[\"home_page/my_file.html.erb\"].result(a_binding)\n```\n\nThe benchmark in the demo folder examines just such a scenario.\n\n## Contributing\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\nOR...\n\n* Make a suggestion by raising an\n [issue](https://github.com/PeterCamilleri/counted_cache/issues)\n. All ideas and comments are welcome.\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](./LICENSE.txt).\n\n## Code of Conduct\n\nEveryone interacting in the counted_cache project’s codebases, issue trackers,\nchat rooms and mailing lists is expected to follow the\n[code of conduct](./CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercamilleri%2Fcounted_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetercamilleri%2Fcounted_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercamilleri%2Fcounted_cache/lists"}