Caching with Rails: An Overview
This guide is an introduction to speeding up your Rails application with caching.
After reading this guide, you will know:
- What caching is.
- The types of caching strategies.
- How to manage the caching dependencies.
- Solid Cache - a database-backed Active Support cache store.
- Other cache stores.
- Cache keys.
- Conditional GET support.
1. What is Caching?
Caching means storing content generated during the request-response cycle andreusing it when responding to similar requests. It's like keeping your favoritecoffee mug right on your desk instead of in the kitchen cabinet — it’s readywhen you need it, saving you time and effort.
Caching is one of the most effective ways to boost an application's performance.It allows websites running on modest infrastructure — a single server with asingle database — to sustain thousands of concurrent users.
Rails provides a set of caching features out of the box which allows you to notonly cache data, but also to tackle challenges like cache expiration, cachedependencies, and cache invalidation.
This guide will explore Rails' comprehensive caching strategies, from fragmentcaching to SQL caching. With these techniques, your Rails application can servemillions of views while keeping response times low and server bills manageable.
2. Types of Caching
This is an introduction to some of the common types of caching.
By default, Action Controller caching is only enabled in your production environment. You can playaround with caching locally by runningbin/rails dev:cache, or by settingconfig.action_controller.perform_caching totrue inconfig/environments/development.rb.
Changing the value ofconfig.action_controller.perform_caching willonly have an effect on the caching provided by Action Controller.For instance, it will not impact low-level caching, that we addressbelow.
2.1. Fragment Caching
Dynamic web applications usually build pages with a variety of components notall of which have the same caching characteristics. When different parts of thepage need to be cached and expired separately you can use Fragment Caching.
Fragment Caching allows a fragment of view logic to be wrapped in a cache block and served out of the cache store when the next request comes in.
For example, if you wanted to cache each product on a page, you could use thiscode:
<%@products.eachdo|product|%><%cacheproductdo%><%=renderproduct%><%end%><%end%>When your application receives its first request to this page, Rails will writea new cache entry with a unique key. A key looks something like this:
views/products/index:bea67108094918eeba42cd4a6e786901/products/1The string of characters in the middle is a template tree digest. It is a hashdigest computed based on the contents of the view fragment you are caching. Ifyou change the view fragment (e.g., the HTML changes), the digest will change,expiring the existing file.
A cache version, derived from the product record, is stored in the cache entry.When the product is touched, the cache version changes, and any cached fragmentsthat contain the previous version are ignored.
Cache stores like Memcached will automatically delete old cache files.
If you want to cache a fragment under certain conditions, you can usecache_if orcache_unless:
<%cache_ifadmin?,productdo%><%=renderproduct%><%end%>2.1.1. Collection Caching
Therender helper can also cache individual templates rendered for a collection.It can even one up the previous example witheach by reading all cachetemplates at once instead of one by one. This is done by passingcached: true when rendering the collection:
<%=renderpartial:'products/product',collection:@products,cached:true%>All cached templates from previous renders will be fetched at once with muchgreater speed. Additionally, the templates that haven't yet been cached will bewritten to cache and multi fetched on the next render.
The cache key can be configured. In the example below, it is prefixed with thecurrent locale to ensure that different localizations of the product pagedo not overwrite each other:
<%=renderpartial:'products/product',collection:@products,cached:->(product){[I18n.locale,product]}%>2.2. Russian Doll Caching
You may want to nest cached fragments inside other cached fragments. This iscalled Russian doll caching.
The advantage of Russian doll caching is that if a single product is updated,all the other inner fragments can be reused when regenerating the outerfragment.
As explained in the previous section, a cached file will expire if the value ofupdated_at changes for a record on which the cached file directly depends.However, this will not expire any cache the fragment is nested within.
For example, take the following view:
<%cacheproductdo%><%=renderproduct.games%><%end%>Which in turn renders this view:
<%cachegamedo%><%=rendergame%><%end%>If any attribute of game is changed, theupdated_at value will be set to thecurrent time, thereby expiring the cache. However, becauseupdated_atwill not be changed for the product object, that cache will not be expired andyour app will serve stale data. To fix this, we tie the models together withthetouch method:
classProduct<ApplicationRecordhas_many:gamesendclassGame<ApplicationRecordbelongs_to:product,touch:trueendWithtouch set totrue, any action which changesupdated_at for a gamerecord will also change it for the associated product, thereby expiring thecache.
2.3. Shared Partial Caching
It is possible to share partials and associated caching between files with different MIME types. For example shared partial caching allows template writers to share a partial between HTML and JavaScript files. When templates are collected in the template resolver file paths they only include the template language extension and not the MIME type. Because of this templates can be used for multiple MIME types. Both HTML and JavaScript requests will respond to the following code:
render(partial:"hotels/hotel",collection:@hotels,cached:true)Will load a file namedhotels/hotel.erb.
Another option is to include theformats attribute to the partial to render.
render(partial:"hotels/hotel",collection:@hotels,formats: :html,cached:true)Will load a file namedhotels/hotel.html.erb in any file MIME type, for example you could include this partial in a JavaScript file.
2.4. Low-Level Caching usingRails.cache
Sometimes you need to cache a particular value or query result instead of caching view fragments. Rails' caching mechanism works great for storing any serializable information.
An efficient way to implement low-level caching is using theRails.cache.fetch method. This method handles bothreading from andwriting to the cache. When called with a single argument, it fetches and returns the cached value for the given key. If a block is passed, the block is executed only on a cache miss. The block's return value is written to the cache under the given cache key and returned. In case of cache hit, the cached value is returned directly without executing the block.
Consider the following example. An application has aProduct model with an instance method that looks up the product's price on a competing website. The data returned by this method would be perfect for low-level caching:
classProduct<ApplicationRecorddefcompeting_priceRails.cache.fetch("#{cache_key_with_version}/competing_price",expires_in:12.hours)doCompetitor::API.find_price(id)endendendNotice that in this example we used thecache_key_with_version method, so the resulting cache key will be something likeproducts/233-20140225082222765838000/competing_price.cache_key_with_version generates a string based on the model's class name,id, andupdated_at attributes. This is a common convention and has the benefit of invalidating the cache whenever the product is updated. In general, when you use low-level caching, you need to generate a cache key.
Below are some more examples of how to use low-level caching:
# Store a value in the cacheRails.cache.write("greeting","Hello, world!")# Retrieve the value from the cachegreeting=Rails.cache.read("greeting")putsgreeting# Output: Hello, world!# Fetch a value with a block to set a default if it doesn’t existwelcome_message=Rails.cache.fetch("welcome_message"){"Welcome to Rails!"}putswelcome_message# Output: Welcome to Rails!# Delete a value from the cacheRails.cache.delete("greeting")2.4.1. Avoid Caching Instances of Active Record Objects
Consider this example, which stores a list of Active Record objects representing superusers in the cache:
# super_admins is an expensive SQL query, so don't run it too oftenRails.cache.fetch("super_admin_users",expires_in:12.hours)doUser.super_admins.to_aendYou shouldavoid this pattern. Why? Because the instance could change. In production, attributeson it could differ, or the record could be deleted. And in development, it works unreliably withcache stores that reload code when you make changes.
Instead, cache the ID or some other primitive data type. For example:
# super_admins is an expensive SQL query, so don't run it too oftenids=Rails.cache.fetch("super_admin_user_ids",expires_in:12.hours)doUser.super_admins.pluck(:id)endUser.where(id:ids).to_a2.5. SQL Caching
Query caching is a Rails feature that caches the result set returned by eachquery. If Rails encounters the same query again for that request, it will usethe cached result set as opposed to running the query against the databaseagain.
For example:
classProductsController<ApplicationControllerdefindex# Run a find query@products=Product.all# ...# Run the same query again@products=Product.allendendThe second time the same query is run against the database, it's not actually going to hit the database. The first time the result is returned from the query it is stored in the query cache (in memory) and the second time it's pulled from memory. However, each retrieval still instantiates new instances of the queried objects.
Query caches are created at the start of an action and destroyed at theend of that action and thus persist only for the duration of the action. Ifyou'd like to store query results in a more persistent fashion, you can withlow-level caching.
3. Managing Dependencies
In order to correctly invalidate the cache, you need to properly define thecaching dependencies. Rails is clever enough to handle common cases so you don'thave to specify anything. However, sometimes, when you're dealing with customhelpers for instance, you need to explicitly define them.
3.1. Implicit Dependencies
Most template dependencies can be derived from calls torender in the templateitself. Here are some examples of render calls thatActionView::Digestor knowshow to decode:
renderpartial:"comments/comment",collection:commentable.commentsrender"comments/comments"render("comments/comments")render"header"# translates to render("comments/header")render(@topic)# translates to render("topics/topic")render(topics)# translates to render("topics/topic")render(message.topics)# translates to render("topics/topic")On the other hand, some calls need to be changed to make caching work properly.For instance, if you're passing a custom collection, you'll need to change:
render@project.documents.where(published:true)to:
renderpartial:"documents/document",collection:@project.documents.where(published:true)3.2. Explicit Dependencies
Sometimes you'll have template dependencies that can't be derived at all. Thisis typically the case when rendering happens in helpers. Here's an example:
<%=render_sortable_todolists@project.todolists%>You'll need to use a special comment format to call those out:
<%# Template Dependency: todolists/todolist %><%=render_sortable_todolists@project.todolists%>In some cases, like a single table inheritance setup, you might have a bunch ofexplicit dependencies. Instead of writing every template out, you can use awildcard to match any template in a directory:
<%# Template Dependency: events/* %><%=render_categorizable_events@person.events%>As for collection caching, if the partial template doesn't start with a cleancache call, you can still benefit from collection caching by adding a specialcomment format anywhere in the template, like:
<%# Template Collection: notification %><%my_helper_that_calls_cache(some_arg,notification)do%><%=notification.name%><%end%>3.3. External Dependencies
If you use a helper method, for example, inside a cached block and you then updatethat helper, you'll have to bump the cache as well. It doesn't really matter howyou do it, but the MD5 of the template file must change. One recommendation is tosimply be explicit in a comment, like:
<%# Helper Dependency Updated: Jul 28, 2015 at 7pm %><%=some_helper_method(person)%>4. Solid Cache
Solid Cache is a database-backed Active Support cache store. It leverages thespeed of modernSSDs (SolidState Drives) to offer cost-effective caching with larger storage capacity andsimplified infrastructure. While SSDs are slightly slower than RAM, thedifference is minimal for most applications. SSDs compensate for this by notneeding to be invalidated as frequently, since they can store much more data. Asa result, there are fewer cache misses on average, leading to fast responsetimes.
Solid Cache uses a FIFO (First In, First Out) caching strategy, where the firstitem added to the cache is the first one to be removed when the cache reachesits limit. This approach is simpler but less efficient compared to an LRU (LeastRecently Used) cache, which removes the least recently accessed items first,better optimizing for frequently used data. However, Solid Cache compensates forthe lower efficiency of FIFO by allowing the cache to live longer, reducing thefrequency of invalidations.
Solid Cache is enabled by default from Rails version 8.0 and onward. However, ifyou'd prefer not to utilize it, you can skip Solid Cache:
rails new app_name --skip-solidUsing the--skip-solid flag skips all parts of the SolidTrifecta (Solid Cache, Solid Queue, and Solid Cable).If you stillwant to use some of them, you can install them separately. Forexample, if you want to use Solid Queue and Solid Cable but notSolid Cache, you can follow the installation guides forSolidQueue andSolid Cable.
4.1. Configuring the Database
To use Solid Cache, you can configure the database connection in yourconfig/database.yml file. Here's an example configuration for a SQLitedatabase:
production:primary:<<:*defaultdatabase:storage/production.sqlite3cache:<<:*defaultdatabase:storage/production_cache.sqlite3migrations_paths:db/cache_migrateIn this configuration, thecache database is used to store cached data. Youcan also specify a different database adapter, like MySQL or PostgreSQL, if youprefer.
production:primary:&primary_production<<:*defaultdatabase:app_productionusername:apppassword:<%= ENV["APP_DATABASE_PASSWORD"] %>cache:<<:*primary_productiondatabase:app_production_cachemigrations_paths:db/cache_migrateIfdatabase ordatabases is not specified in thecache configuration, Solid Cache will use the ActiveRecord::Base connectionpool. This means that cache reads and writes will be part of any wrappingdatabase transaction.
In production, the cache store is configured to use the Solid Cache store bydefault:
# config/environments/production.rbconfig.cache_store = :solid_cache_storeYou canaccess the cache by callingRails.cache
4.2. Customizing the Cache Store
Solid Cache can be customized through the config/cache.yml file:
default:&defaultstore_options:# Cap age of oldest cache entry to fulfill retention policiesmax_age:<%= 60.days.to_i %>max_size:<%= 256.megabytes %>namespace:<%= Rails.env %>For the full list of keys for store_options seeCacheconfiguration.
Here, you can adjust themax_age andmax_size options to control the age andsize of the cache entries.
4.3. Handling Cache Expiration
Solid Cache tracks cache writes by incrementing a counter with each write. Whenthe counter reaches 50% of theexpiry_batch_size from theCacheconfiguration, abackground task is triggered to handle cache expiry. This approach ensures cacherecords expire faster than they are written when the cache needs to shrink.
The background task only runs when there are writes, so the process stays idlewhen the cache is not being updated. If you prefer to run the expiry process ina background job instead of a thread, setexpiry_method from theCacheconfiguration to:job.
4.4. Sharding the Cache
If you need more scalability, Solid Cache supports sharding — splitting thecache across multiple databases. This spreads the load, making your cache evenmore powerful. To enable sharding, add multiple cache databases to yourdatabase.yml:
# config/database.ymlproduction:cache_shard1:database:cache1_productionhost:cache1-dbcache_shard2:database:cache2_productionhost:cache2-dbcache_shard3:database:cache3_productionhost:cache3-dbAdditionally, you must specify the shards in the cache configuration:
# config/cache.ymlproduction:databases:[cache_shard1,cache_shard2,cache_shard3]4.5. Encryption
Solid Cache supports encryption to protect sensitive data. To enable encryption,set theencrypt value in your cache configuration:
# config/cache.ymlproduction:encrypt:trueYou will need to set up your application to useActive RecordEncryption.
4.6. Caching in Development
By default, caching isenabled in development mode with:memory_store. This doesn't apply toAction Controller caching, which is disabled by default.
To enable Action Controller caching Rails provides thebin/rails dev:cachecommand.
$bin/railsdev:cacheDevelopment mode is now being cached.$bin/railsdev:cacheDevelopment mode is no longer being cached.If you want to use Solid Cache in development, set thecache_storeconfiguration inconfig/environments/development.rb:
config.cache_store=:solid_cache_storeand ensure thecache database is created and migrated:
development: <<: * default database: cacheTo disable caching setcache_store to:null_store
5. Other Cache Stores
Rails provides different stores for the cached data (with the exception of SQLCaching).
5.1. Configuration
You can set up a different cache store by setting theconfig.cache_store configuration option. Other parameters can be passed asarguments to the cache store's constructor:
config.cache_store=:memory_store,{size:64.megabytes}Alternatively, you can setActionController::Base.cache_store outside of a configuration block.
You can access the cache by callingRails.cache.
5.1.1. Connection Pool Options
:mem_cache_store and:redis_cache_store are configured touse connection pooling. This means that if you're using Puma, or anotherthreaded server, you can have multiple threads performing queries to the cachestore at the same time.
If you want to disable connection pooling, set:pool option tofalse when configuring the cache store:
config.cache_store=:mem_cache_store,"cache.example.com",{pool:false}You can also override default pool settings by providing individual options to the:pool option:
config.cache_store=:mem_cache_store,"cache.example.com",{pool:{size:32,timeout:1}}:size- This option sets the number of connections per process (defaults to 5).:timeout- This option sets the number of seconds to wait for a connection (defaults to 5). If no connection is available within the timeout, aTimeout::Errorwill be raised.
5.2.ActiveSupport::Cache::Store
ActiveSupport::Cache::Store provides the foundation for interacting with the cache in Rails. This is an abstract class, and you cannot use it on its own. Instead, you must use a concrete implementation of the class tied to a storage engine. Rails ships with several implementations, documented below.
The main API methods areread,write,delete,exist?, andfetch.
Options passed to the cache store's constructor will be treated as default options for the appropriate API methods.
5.3.ActiveSupport::Cache::MemoryStore
ActiveSupport::Cache::MemoryStore keeps entries in memory in the same Ruby process. The cachestore has a bounded size specified by sending the:size option to theinitializer (default is 32Mb). When the cache exceeds the allotted size, acleanup will occur and the least recently used entries will be removed.
config.cache_store=:memory_store,{size:64.megabytes}If you're running multiple Ruby on Rails server processes (which is the caseif you're using Phusion Passenger or puma clustered mode), then your Rails serverprocess instances won't be able to share cache data with each other. This cachestore is not appropriate for large application deployments. However, it canwork well for small, low traffic sites with only a couple of server processes,as well as development and test environments.
New Rails projects are configured to use this implementation in the development environment by default.
Since processes will not share cache data when using:memory_store,it will not be possible to manually read, write, or expire the cache via the Rails console.
5.4.ActiveSupport::Cache::FileStore
ActiveSupport::Cache::FileStore uses the file system to store entries. The path to the directory where the store files will be stored must be specified when initializing the cache.
config.cache_store=:file_store,"/path/to/cache/directory"With this cache store, multiple server processes on the same host can share acache. This cache store is appropriate for low to medium traffic sites that areserved off one or two hosts. Server processes running on different hosts couldshare a cache by using a shared file system, but that setup is not recommended.
As the cache will grow until the disk is full, it is recommended toperiodically clear out old entries.
5.5.ActiveSupport::Cache::MemCacheStore
ActiveSupport::Cache::MemCacheStore uses Danga'smemcached server to provide a centralized cache for your application. Rails uses the bundleddalli gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very high performance and redundancy.
When initializing the cache, you should specify the addresses for all memcached servers in your cluster, or ensure theMEMCACHE_SERVERS environment variable has been set appropriately.
config.cache_store=:mem_cache_store,"cache-1.example.com","cache-2.example.com"If neither are specified, it will assume memcached is running on localhost on the default port (127.0.0.1:11211), but this is not an ideal setup for larger sites.
config.cache_store=:mem_cache_store# Will fallback to $MEMCACHE_SERVERS, then 127.0.0.1:11211See theDalli::Client documentation for supported address types.
Thewrite (andfetch) method on this cache accepts additional options that take advantage of features specific to memcached.
5.6.ActiveSupport::Cache::RedisCacheStore
ActiveSupport::Cache::RedisCacheStore takes advantage of Redis support for automatic evictionwhen it reaches max memory, allowing it to behave much like a Memcached cache server.
Deployment note: Redis doesn't expire keys by default, so take care to use adedicated Redis cache server. Don't fill up your persistent-Redis server withvolatile cache data! Read theRedis cache server setup guide in detail.
For a cache-only Redis server, setmaxmemory-policy to one of the variants of allkeys.Redis 4+ supports least-frequently-used eviction (allkeys-lfu), an excellentdefault choice. Redis 3 and earlier should use least-recently-used eviction (allkeys-lru).
Set cache read and write timeouts relatively low. Regenerating a cached valueis often faster than waiting more than a second to retrieve it. Both read andwrite timeouts default to 1 second, but may be set lower if your network isconsistently low-latency.
By default, the cache store will attempt to reconnect to Redis once if theconnection fails during a request.
Cache reads and writes never raise exceptions; they just returnnil instead,behaving as if there was nothing in the cache. To gauge whether your cache ishitting exceptions, you may provide anerror_handler to report to anexception gathering service. It must accept three keyword arguments:method,the cache store method that was originally called;returning, the value thatwas returned to the user, typicallynil; andexception, the exception thatwas rescued.
To get started, add the redis gem to your Gemfile:
gem"redis"Finally, add the configuration in the relevantconfig/environments/*.rb file:
config.cache_store=:redis_cache_store,{url:ENV["REDIS_URL"]}A more complex, production Redis cache store may look something like this:
cache_servers=%w(redis://cache-01:6379/0 redis://cache-02:6379/0)config.cache_store=:redis_cache_store,{url:cache_servers,connect_timeout:30,# Defaults to 1 secondread_timeout:0.2,# Defaults to 1 secondwrite_timeout:0.2,# Defaults to 1 secondreconnect_attempts:2,# Defaults to 1error_handler:->(method:,returning:,exception:){# Report errors to Sentry as warningsSentry.capture_exceptionexception,level:"warning",tags:{method:method,returning:returning}}}5.7.ActiveSupport::Cache::NullStore
ActiveSupport::Cache::NullStore is scoped to each web request, and clears stored values at the end of a request. It is meant for use in development and test environments. It can be very useful when you have code that interacts directly withRails.cache but caching interferes with seeing the results of code changes.
config.cache_store=:null_store5.8. Custom Cache Stores
You can create your own custom cache store by simply extendingActiveSupport::Cache::Store and implementing the appropriate methods. This way,you can swap in any number of caching technologies into your Rails application.
To use a custom cache store, simply set the cache store to a new instance of yourcustom class.
config.cache_store=MyCacheStore.new6. Cache Keys
The keys used in a cache can be any object that responds to eithercache_key orto_param. You can implement thecache_key method on your classes if you needto generate custom keys. Active Record will generate keys based on the class nameand record id.
You can use Hashes and Arrays of values as cache keys.
# This is a valid cache keyRails.cache.read(site:"mysite",owners:[owner_1,owner_2])The keys you use onRails.cache will not be the same as those actually used withthe storage engine. They may be modified with a namespace or altered to fittechnology backend constraints. This means, for instance, that you can't savevalues withRails.cache and then try to pull them out with thedalli gem.However, you also don't need to worry about exceeding the memcached size limit orviolating syntax rules.
7. Conditional GET Support
Conditional GETs are a feature of the HTTP specification that provide a way for web servers to tell browsers that the response to a GET request hasn't changed since the last request and can be safely pulled from the browser cache.
They work by using theHTTP_IF_NONE_MATCH andHTTP_IF_MODIFIED_SINCE headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (ETag) or last modified since timestamp matches the server's version then the server only needs to send back an empty response with a not modified status.
It is the server's (i.e. our) responsibility to look for a last modified timestamp and the if-none-match header and determine whether or not to send back the full response. With conditional-get support in Rails this is a pretty easy task:
classProductsController<ApplicationControllerdefshow@product=Product.find(params[:id])# If the request is stale according to the given timestamp and etag value# (i.e. it needs to be processed again) then execute this blockifstale?(last_modified:@product.updated_at.utc,etag:@product.cache_key_with_version)respond_todo|wants|# ... normal response processingendend# If the request is fresh (i.e. it's not modified) then you don't need to do# anything. The default render checks for this using the parameters# used in the previous call to stale? and will automatically send a# :not_modified. So that's it, you're done.endendInstead of an options hash, you can also simply pass in a model. Rails will use theupdated_at andcache_key_with_version methods for settinglast_modified andetag:
classProductsController<ApplicationControllerdefshow@product=Product.find(params[:id])ifstale?(@product)respond_todo|wants|# ... normal response processingendendendendIf you don't have any special response processing and are using the default rendering mechanism (i.e. you're not usingrespond_to or calling render yourself) then you've got an easy helper infresh_when:
classProductsController<ApplicationController# This will automatically send back a :not_modified if the request is fresh,# and will render the default template (product.*) if it's stale.defshow@product=Product.find(params[:id])fresh_whenlast_modified:@product.published_at.utc,etag:@productendendWhen bothlast_modified andetag are set, behavior varies depending on the value ofconfig.action_dispatch.strict_freshness.If set totrue, only theetag is considered as specified by RFC 7232 section 6.If set tofalse, both are considered and the cache is considered fresh if both conditions are satisfied, as was the historical Rails behavior.
Sometimes we want to cache response, for example a static page, that never getsexpired. To achieve this, we can usehttp_cache_forever helper and by doingso browser and proxies will cache it indefinitely.
By default cached responses will be private, cached only on the user's webbrowser. To allow proxies to cache the response, setpublic: true to indicatethat they can serve the cached response to all users.
Using this helper,last_modified header is set toTime.new(2011, 1, 1).utcandexpires header is set to a 100 years.
Use this method carefully as browser/proxy won't be able to invalidatethe cached response unless browser cache is forcefully cleared.
classHomeController<ApplicationControllerdefindexhttp_cache_forever(public:true)dorenderendendend7.1. Strong v/s Weak ETags
Rails generates weak ETags by default. Weak ETags allow semantically equivalentresponses to have the same ETags, even if their bodies do not match exactly.This is useful when we don't want the page to be regenerated for minor changes inresponse body.
Weak ETags have a leadingW/ to differentiate them from strong ETags.
W/"618bbc92e2d35ea1945008b42799b0e7" → Weak ETag"618bbc92e2d35ea1945008b42799b0e7" → Strong ETagUnlike weak ETag, strong ETag implies that response should be exactly the sameand byte by byte identical. Useful when doing Range requests within alarge video or PDF file. Some CDNs support only strong ETags, like Akamai.If you absolutely need to generate a strong ETag, it can be done as follows.
classProductsController<ApplicationControllerdefshow@product=Product.find(params[:id])fresh_whenlast_modified:@product.published_at.utc,strong_etag:@productendendYou can also set the strong ETag directly on the response.
response.strong_etag=response.body# => "618bbc92e2d35ea1945008b42799b0e7"