{"id":13483121,"url":"https://github.com/rails/actionpack-action_caching","last_synced_at":"2025-04-07T23:06:31.340Z","repository":{"id":4919784,"uuid":"6076206","full_name":"rails/actionpack-action_caching","owner":"rails","description":"Action caching for Action Pack (removed from core in Rails 4.0)","archived":false,"fork":false,"pushed_at":"2022-09-11T21:29:03.000Z","size":101,"stargazers_count":262,"open_issues_count":24,"forks_count":96,"subscribers_count":27,"default_branch":"master","last_synced_at":"2024-10-29T14:14:59.326Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"gnprice/pg-hstore","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rails.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-04T14:05:39.000Z","updated_at":"2024-10-19T23:13:37.000Z","dependencies_parsed_at":"2022-09-09T04:02:11.337Z","dependency_job_id":null,"html_url":"https://github.com/rails/actionpack-action_caching","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rails%2Factionpack-action_caching","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rails%2Factionpack-action_caching/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rails%2Factionpack-action_caching/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rails%2Factionpack-action_caching/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rails","download_url":"https://codeload.github.com/rails/actionpack-action_caching/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744333,"owners_count":20988783,"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":[],"created_at":"2024-07-31T17:01:08.437Z","updated_at":"2025-04-07T23:06:31.322Z","avatar_url":"https://github.com/rails.png","language":"Ruby","readme":"actionpack-action_caching\n=========================\n\nAction caching for Action Pack (removed from core in Rails 4.0).\n\nInstallation\n------------\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'actionpack-action_caching'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install actionpack-action_caching\n\nUsage\n-----\n\nAction caching is similar to page caching by the fact that the entire\noutput of the response is cached, but unlike page caching, every\nrequest still goes through Action Pack. The key benefit of this is\nthat filters run before the cache is served, which allows for\nauthentication and other restrictions on whether someone is allowed\nto execute such action.\n\n```ruby\nclass ListsController \u003c ApplicationController\n  before_action :authenticate, except: :public\n\n  caches_page   :public\n  caches_action :index, :show\nend\n```\n\nIn this example, the `public` action doesn't require authentication\nso it's possible to use the faster page caching. On the other hand\n`index` and `show` require authentication. They can still be cached,\nbut we need action caching for them.\n\nAction caching uses fragment caching internally and an around\nfilter to do the job. The fragment cache is named according to\nthe host and path of the request. A page that is accessed at\n`http://david.example.com/lists/show/1` will result in a fragment named\n`david.example.com/lists/show/1`. This allows the cacher to\ndifferentiate between `david.example.com/lists/` and\n`jamis.example.com/lists/` -- which is a helpful way of assisting\nthe subdomain-as-account-key pattern.\n\nDifferent representations of the same resource, e.g.\n`http://david.example.com/lists` and\n`http://david.example.com/lists.xml`\nare treated like separate requests and so are cached separately.\nKeep in mind when expiring an action cache that\n`action: \"lists\"` is not the same as\n`action: \"list\", format: :xml`.\n\nYou can modify the default action cache path by passing a\n`:cache_path` option. This will be passed directly to\n`ActionCachePath.new`. This is handy for actions with\nmultiple possible routes that should be cached differently. If a\nproc (or an object that responds to `to_proc`) is given, it is\ncalled with the current controller instance.\n\nAnd you can also use `:if` (or `:unless`) to control when the action\nshould be cached, similar to how you use them with `before_action`.\n\nAs of Rails 3.0, you can also pass `:expires_in` with a time\ninterval (in seconds) to schedule expiration of the cached item.\n\nThe following example depicts some of the points made above:\n\n```ruby\nclass ListsController \u003c ApplicationController\n  before_action :authenticate, except: :public\n\n  # simple fragment cache\n  caches_action :current\n\n  # expire cache after an hour\n  caches_action :archived, expires_in: 1.hour\n\n  # cache unless it's a JSON request\n  caches_action :index, unless: -\u003e { request.format.json? }\n\n  # custom cache path\n  caches_action :show, cache_path: { project: 1 }\n\n  # custom cache path with a proc\n  caches_action :history, cache_path: -\u003e { request.domain }\n\n  # custom cache path with a symbol\n  caches_action :feed, cache_path: :user_cache_path\n\n  protected\n    def user_cache_path\n      if params[:user_id]\n        user_list_url(params[:user_id], params[:id])\n      else\n        list_url(params[:id])\n      end\n    end\nend\n```\n\nIf you pass `layout: false`, it will only cache your action\ncontent. That's useful when your layout has dynamic information.\n\nNote: Both the `:format` param and the `Accept` header are taken\ninto account when caching the fragment with the `:format` having\nprecedence. For backwards compatibility when the `Accept` header\nindicates a HTML request the fragment is stored without the\nextension but if an explicit `\"html\"` is passed in `:format` then\nthat _is_ used for storing the fragment.\n\nContributing\n------------\n\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 a new Pull Request.\n\nCode Status\n-----------\n\n* [![Build Status](https://github.com/rails/actionpack-action_caching/actions/workflows/ci.yml/badge.svg)](https://github.com/rails/actionpack-action_caching/actions/workflows/ci.yml)\n","funding_links":[],"categories":["Caching"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frails%2Factionpack-action_caching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frails%2Factionpack-action_caching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frails%2Factionpack-action_caching/lists"}