I work in Rails 4 on WEBrick, trying to get a response to the cache until it expires at a specific time every day. It seems like an automatically generated ETag is interfering with the expiration cache, so I was looking for this answer. I did not find anything useful here, but I solved the problem, so I will share it.
tl; dr Set Last-Modified Header
But install it for what? In my situation, I tried to optimize a web service that returned the results of a process that runs at the same time every day. My response headers looked like this:
response.headers['Cache-Control'] = "max-age=86400" response.headers['Expires'] = getCacheTime response.headers['Last-Modified'] = getLastModified
First, you want to explicitly write the Cache-Control header to overwrite all the default values. I found that I should be 24 hours to match the maximum of my expiration header. I set the expiration header with a function that looks something like this:
def getCacheTime now = Time.now.utc cacheTime = Time.utc(now.year, now.month, now.day, 22, 00, 00) if now > cacheTime cacheTime = cacheTime + (60 * 60 * 24) end cacheTime.httpdate end
The getLastModified function returns 24 hours less than the getCacheTime function. It seems that setting this will overwhelm ETag (another validation cache header), at least in my current development environment.
Chris broski
source share