{"id":13835618,"url":"https://github.com/mhgbrown/cached_resource","last_synced_at":"2025-07-10T10:30:40.003Z","repository":{"id":1739282,"uuid":"2564732","full_name":"mhgbrown/cached_resource","owner":"mhgbrown","description":"Caching for ActiveResource","archived":false,"fork":false,"pushed_at":"2024-10-28T11:12:19.000Z","size":217,"stargazers_count":80,"open_issues_count":3,"forks_count":28,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-16T02:39:18.632Z","etag":null,"topics":["activeresource","caching","rails","ruby"],"latest_commit_sha":null,"homepage":"","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/mhgbrown.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-10-12T19:21:59.000Z","updated_at":"2024-07-19T12:30:34.000Z","dependencies_parsed_at":"2024-05-05T17:26:37.435Z","dependency_job_id":"7d360c86-ffba-45cb-a959-abf6bcbb900d","html_url":"https://github.com/mhgbrown/cached_resource","commit_stats":{"total_commits":215,"total_committers":23,"mean_commits":9.347826086956522,"dds":0.5627906976744186,"last_synced_commit":"964895e34abd7758bcf552386beee2135fed688f"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhgbrown%2Fcached_resource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhgbrown%2Fcached_resource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhgbrown%2Fcached_resource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhgbrown%2Fcached_resource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mhgbrown","download_url":"https://codeload.github.com/mhgbrown/cached_resource/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225632929,"owners_count":17499889,"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":["activeresource","caching","rails","ruby"],"created_at":"2024-08-04T14:01:06.474Z","updated_at":"2024-11-20T21:31:11.275Z","avatar_url":"https://github.com/mhgbrown.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# CachedResource ![Tests](https://github.com/mhgbrown/cached_resource/actions/workflows/ruby.yml/badge.svg)\n\nCachedResource is a Ruby gem designed to enhance the performance of web service interactions through ActiveResource by caching responses based on request parameters. By reducing the need for repeated network requests, it minimizes latency and optimizes the efficiency of your application's data retrieval processes.\n\n## Installation\n\n```ruby\ngem install cached_resource\n```\n\n## Compatibility\n\nCachedResource is designed to be framework agnostic, but will hook into Rails for caching and logging if available. CachedResource supports the following ActiveSupport/Rails (right) and Ruby (down) version combinations:\n\n|          | 🛤️ 6.1 | 🛤️ 7.0 | 🛤️ 7.1 |\n|----------|:------:|:------:|:------:|\n| 💎 3.0   |   ✅   |   ✅   |   ✅   |\n| 💎 3.1   |   ✅   |   ✅   |   ✅   |\n| 💎 3.2   |   ✅   |   ✅   |   ✅   |\n\n## Configuration\n\n**Set up CachedResource across all ActiveResources:**\n\n```ruby\nclass ActiveResource::Base\n  cached_resource(options)\nend\n```\n\nOr set up CachedResource for a single class:\n\n```ruby\nclass MyActiveResource \u003c ActiveResource::Base\n  cached_resource(options)\nend\n```\n\n### Options\nCachedResource accepts the following options as a hash:\n\n| Option                     | Description                                                                                                                                                         | Default                                                                                                    |\n|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|\n| `:enabled`                 | Enables or disables caching.                                                                                                                                        | `true`                                                                                                     |\n| `:cache_collections`       | Set to false to always remake a request for collections.                                                                                                            | `true`                                                                                                     |\n| `:cache`                   | The cache store that CacheResource should use.                                                                                                                      | The `Rails.cache` if available, or an `ActiveSupport::Cache::MemoryStore`                                  |\n| `:cache_key_prefix`        | A prefix to be added to the cache keys.                                                                                                                              | `nil`                                                                                                      |\n| `:collection_arguments`    | The arguments that identify the principal collection request.                                                                                                        | `[:all]`                                                                                                    |\n| `:collection_synchronize`  | Use collections to generate cache entries for individuals. Update the existing cached principal collection when retrieving subsets of the principal collection or individuals. | `false`                                                                                                    |\n| `:logger`                  | The logger to which CachedResource messages should be written.                                                                                                       | The `Rails.logger` if available, or an `ActiveSupport::Logger`                                             |\n| `:race_condition_ttl`      | The race condition ttl, to prevent [dog pile effect](https://en.wikipedia.org/wiki/Cache_stampede) or [cache stampede](https://en.wikipedia.org/wiki/Cache_stampede). | `86400`                                                                                                    |\n| `:ttl_randomization_scale` | A Range from which a random value will be selected to scale the ttl.                                                                                                | `1..2`                                                                                                     |\n| `:ttl_randomization`       | Enable ttl randomization.                                                                                                                                           | `false`                                                                                                    |\n| `:ttl`                     | The time in seconds until the cache should expire.                                                                                                                   | `604800`                                                                                                   |\n\nFor example:\n```ruby\ncached_resource :cache =\u003e MyCacheStore.new, :ttl =\u003e 60, :collection_synchronize =\u003e true, :logger =\u003e MyLogger.new\n```\n\n#### You can also change them dynamically. Simply:\n\n```ruby\n  MyActiveResource.cached_resource.option = option_value\n```\n\nAdditionally a couple of helper methods are available:\n\n```ruby\n  # Turn caching off\n  MyActiveResource.cached_resource.off!\n  # Turn caching on\n  MyActiveResource.cached_resource.on!\n```\n\n### Caveats\nIf you set up CachedResource across all ActiveResources or any subclass of ActiveResource that will be inherited by other classes and you want some of those others to have independent CachedResource configurations, then check out the example below:\n\n```ruby\nclass ActiveResource::Base\n  cached_resource\nend\n```\n\n```ruby\nclass MyActiveResource \u003c ActiveResource::Base\n  self.cached_resource = CachedResource::Configuration.new(:collection_synchronize: true)\nend\n```\n## Usage\nSit back and relax! If you need to reload a particular request you can pass `:reload =\u003e true` into the options hash like this:\n\n```ruby\nMyActiveResource.find(:all, :reload =\u003e true)\n```\nIf you need to clear the entire cache just do the following:\n\n```ruby\nMyActiveResource.clear_cache\n```\n---\nSometimes you might have a case the resource pathing is non-unique per call. This can create a situation where your caching the same result for multiple calls:\n\n```ruby\nMyActiveResource.find(:one, from: \"/admin/shop.json\")\n```\n\nSince resources are cached with an argument based key, you may pass in extra data to be appended to the cache key:\n\n```ruby\nMyActiveResource.find(:one, from: \"/admin/shop.json\", uid: \"unique value\")\n```\n\n## Contribution\n### Testing\n#### Locally\nYou can set `TEST_RAILS_VERSION` to a value of the supported Rails versions in the [Compatability Matrix](#compatibility), or bundler will automatically select a version.\n\nExamples:\n```sh\nTEST_RAILS_VERSION=7.1 bundle exec rspec # runs test suite + linter\nbundle exec rspec                        # Runs test suite\n```\n\n#### With Docker\n```sh\ndocker build -t cached_resource_test -f Dockerfile.test .\ndocker run --rm -v  ${PWD}/coverage:/app/coverage cached_resource_test\n\n# Coverage report can be found in coverage/index.html\n# RSpec reports can be found in coverage/spec_results/${ruby-version}-${rails-version}.index.html\n# Linter reports can be found in coverage/linter-results.index.html\n```\n\n### Code Coverage\nCoverage can be found found within `coverage/index.html` after every test run.\n\n### Linter\nWe use unconfigurable linting rules from [Standard](https://github.com/standardrb/standard)\n\nTo lint run: `bundle exec rake standard`\nTo automatically apply linter fixes: `bundle exec rake standard:fix`\n\n\n## Credit/Inspiration\n* quamen and [this gist](http://gist.github.com/947734)\n* latimes and [this plugin](http://github.com/latimes/cached_resource)\n\n## Feedback/Problems\nFeedback is greatly appreciated! Check out this project's [issue tracker](https://github.com/Ahsizara/cached_resource/issues) if you've got anything to say.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhgbrown%2Fcached_resource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmhgbrown%2Fcached_resource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhgbrown%2Fcached_resource/lists"}