📜 ⬆️ ⬇️

3 simple tips to make your Rails application faster, part 3

The final article on optimizing a Ruby on Rails application.
Tip # 1: Clean up your static content.
Tip # 2: Remove all unnecessary
Tip # 3: Cache the whole page

Tip # 3: Cache the whole page

This last tip is the most effective. The web server caches the page completely, and then gives only static content. To start working with caches_page, just look at the wonderful railscasts .

It must be remembered that after full caching of the page, it will be displayed the same for all users, no checks or queries will be made to the database. Therefore, you should get rid of all constructions of the form in the page: <%= ... if logged_in? %> <%= ... if logged_in? %>
')
You can still use JavaScript to show or hide the code for registered users. Here is a small example:
var CurrentUser = {
loggedIn: false,
author: false,
admin: false,

init: function() {
this.loggedIn = Cookie.get('token') != null;
this.admin = Cookie.get('admin') != null;
}
};

var Application = {
init: function() {
CurrentUser.init();
},

onBodyLoaded: function() {
if (CurrentUser.loggedIn) {
$$('.if_logged_in').invoke('show');
$$('.unless_logged_in').invoke('hide');
}
if (CurrentUser.admin) {
$$('.if_admin').invoke('show');
}
}
};

Also, you can no longer fully use <%= flash[:notice] %> . However, this is not a problem, there is a great plugin Cacheable Flash.

We put:
ruby script/plugin install svn://rubyforge.org/var/svn/pivotalrb/cacheable_flash/trunk

In ApplicationController we write:
include CacheableFlash

In the controller:
flash[:notice] = "Welcome to Eternity" if current_user

And in layout:
<div id="error_div_id" class="flash flash_error"></div>
<div id="notice_div_id" class="flash flash_notice"></div>


Now all flash messages will be recorded in Cookies. By the way, for the operation of this plugin, it is necessary to put gem json, but in my Ubuntu 8.10 there were problems with this, this gem did not want to get up, as it turned out that this problem was not just me. I declined this problem, put ruby-json , and replaced the line gem "json" with gem "ruby-json" in /vendor/plugins/cachable-flash/init.rb. And it all worked like a clock.

I hope these tips helped you.

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


All Articles