SSIを使ったキャッシュ戦略

よくありそうなネタなのに全然見かけないのは何でだろう?
もう使い古されたネタなのか、とっくに使い物にならないことが分かっているのか…



概要



詳細

Passengerの設定がこんな感じ

/etc/httpd/conf.d/passenger.conf
LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.15
PassengerRuby /usr/local/bin/ruby

<VirtualHost *:80>
  #ServerName www.yourhost.com
  DocumentRoot /opt/hello/public
  <Directory /opt/hello/public>
    AllowOverride all
    Options -MultiViews
  </Directory>

  <Location />
    Options Includes
    AddOutputFilterByType INCLUDES text/html
  </Location>
</VirtualHost>


レイアウトでSSIを使ってユーザ名を出す。

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Hello</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>
<body>
<span style="color:#e47911; :font-weight:bold;">
  Hello, <!--#include virtual="/users/name" -->
</span>
<%= yield %>

</body>
</html>


includeで呼び出すActionはこんな感じ。

app/controllers/users_controller.rb

class UsersController < ApplicationController
  def name
    render :text => 'Yamada Taro'
  end
end



結果



気になることとか

  • Railsは下手に304を返さない方がいいのかな?
    • いや、一つのブラウザからアクセスするユーザは同じだろうし、大丈夫なのかも
    • てゆーかRailsが304を返すときのフィルターの動作ってどうなるんだ?そのまま304を返すだけ?
  • INCLUDESフィルター側で304を返すことはできるんだろうか?