Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stale? method in rails. What does it do?

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...

like image 660
Jwan622 Avatar asked Mar 11 '16 23:03

Jwan622


People also ask

What happens when you add a new model to a rails?

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.

What is the best way to cache data in rails?

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.

Why should you migrate your rails app to SQL?

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.

What is a session in rails?

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:


1 Answers

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

like image 126
toddmetheny Avatar answered Sep 30 '22 20:09

toddmetheny