Page caching using lazy work

Hey everyone, if you've ever written to [craigslist], this question should make sense to you. Whenever you publish a listing (for example, to sell furniture or an apartment), your listing does not immediately hit the site. Rather, lists will be displayed in batches (numbers vary) approximately every 10-15 minutes. At first, I really thought about this behavior, tried to record notes, and then do mass inserts, but I realized that it was much easier. After talking with some colleagues, it made sense that Craigslist caches its pages and then empties this cache every 10-15 minutes. This greatly reduces the load on their database.

Now, to my question. How to do the same in Rails? I know how to implement caching - I read the [caching with Rails guide]. I will use action caching and fragment caching (because I cannot cache the entire page). I still need to do checks and access controls, so I can’t completely cache the page ...

+4
source share
3 answers

To perform timed page caching, you can use standard Rails caching and a small smart timer.

First you want to determine your level of caching. You have three options:

  • Page Caching - Caches the entire page, but subsequent requests do not hit the Rails stack. So if this is a Craiglist-esque page that will hit thousands of times per second, this request will only be sent to your web server (e.g. apache) and not to Rails or your db, which makes it much faster. The tradeoff is that you lose the authentication, session variables, etc. that Rails provides.
  • Action caching - caches the entire page, but returns a request to Rails so that it can execute any filters associated with this action.
  • Fragment caching - caches a page segment, basically bypassing the need to execute code with a block (and any subsequent calls to the database).

Then you need to select the appropriate level of caching and implement it in your application (check out the links above for example implementations).

After you have implemented caching, you need to find out how the cache expires. I can come up with two ways to do this, both come with advantages and disadvantages. For now, suppose you decide to use action caching.

  • Reliable, but more attractive - create an action in your controller, an expiring cache, and a job task cron that makes a request for this action. I asked a similar question that addresses this built-in scheduled task . For security reasons, you might want to include a generated hash or something similar so that someone cannot manually expire your cache by going to '/ products / expire_cache'.

    class ProductsController < ApplicationController caches_action :index def index # implementation end def expire_cache if params[:verification_hash] == 'sa89sf8sfsfehiwaf89yfea98fh' expire_action :action => :index end end end 
  • Unreliable, but simple - just ends the cache in your action with a conditional symbol. This implementation assumes that there will be enough traffic to regularly ensure that someone comes to your site for 0, 15, 30, and 45 minutes. You can reduce this interval to ensure that the cache is reset at a more likely interval.

     class ProductsController < ApplicationController caches_action :index def index # implementation expire_action :action => :index if Time.now.min % 15 == 0 end end 
+5
source

I think there are two approaches to what you want to accomplish:

  • Simple caching with fragments and actions so that when the page first hits the database is accessed, and the page loads normally, but each subsequent hit is from the cached version. The main disadvantage of this approach is that you do not need to deal with delayed tasks and do your pages outside the regular flow of things.
  • Create a delayed job that displays your page or individual fragments that will be cached. During rendering in a task with a delay, fragments of the page will actually be cached as if the user was viewing them (provided that you usually cached fragments and actions). Once the delay is completed, fill in the column in the database that indicates that this record / page is ready for viewing.
0
source

Probably the easiest caching method in Rails uses Memcached with the option :expires_in .

You probably need a VPS server to use it, which can be expensive for small sites.

But you can do quality caching even without memcached. This little SimpleCache worked wonders for my shared hosted sites.

0
source

Source: https://habr.com/ru/post/1335765/


All Articles