I don't understand the documentation here
The point of confusing is this for me: Why do you set etag or last_modified on the response? Isn't the client the one sending an etag and a if-modified-since header? Once the server (or proxy server) receives these client headers, isn't the server checking to see if the resource's etag and modified date match? The documentation suggest that the request is generated from scratch and not the data? Also, why are we setting the etag and last_modified on the response? Isn't it set by the client?
stale?(options) protected Sets the etag and/or last_modified on the response and checks it against the client request. If the request doesn’t match the options provided, the request is considered stale and should be generated from scratch. Otherwise, it’s fresh and we don’t need to generate anything and a reply of "304 Not Modified" is sent.
Parameters:
:etag :last_modified :public By default the Cache-Control header is private, set this to true if you want your application to be cachable by other devices (proxy caches).
What is it going? So some client request is coming in with an etag (some hash) and we check to see if that request is equal to the options in our controller that we set using stale? Here is my controller:
format.json_v20150501 do
expires_in 30.minutes, public: true
if stale?(last_modified: last_modified_for_models(recipes), etag: etag_for_models(recipes))
render json: recipes,
compact: true,
serializer: PaginationSerializer,
each_serializer: Api::V20150315::RecipeSerializer
end
end
I don't get what is happening...
When you add a new model to a Rails application, it generates a migration to create the corresponding table for you. If that’s not what you need, you can write your own.
Rails' caching mechanism works great for storing any kind of information. The most efficient way to implement low-level caching is using the Rails.cache.fetch method. This method does both reading and writing to the cache. When passed only a single argument, the key is fetched and value from the cache is returned.
Rails migrations free you from worrying about the differences between various SQL grammar so you can focus on your application code. So, take a closer look at migrations before you write SQL for your Rails app. Stackify’s APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
A session is just a place to store data during one request that you can read during later requests. It might not seem that interesting. But it takes coordination between your user’s browser and your Rails app to make everything connect up. And it all starts with cookies. When you request a webpage, the server can set a cookie when it responds back:
stale? checks the options provided (those args) to determine whether the object has been modified and therefore needs to be regenerated (in case of cacheing).
regarding the etags, reading this might be helpful: http://blog.bigbinary.com/2016/03/08/rails-5-switches-from-strong-etags-to-weak-tags.html?utm_source=rubyweekly&utm_medium=email
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With