2003年3月13日(木) また寒くなるのか? [同日]
● 三寒四温というかなんというか…。
● [tDiary] makelirs.rbを使ってHTTPヘッダのキャッシュを作ってみた
これでキャッシュディレクトリにヘッダのテキストファイルを作り、
Index: makelirs.rb
===================================================================
RCS file: /arpa/tz/z/zunda/cvs/d/misc/plugin/makelirs.rb,v
retrieving revision 1.1.1.1
retrieving revision 1.1.1.1.2.1
diff -u -r1.1.1.1 -r1.1.1.1.2.1
--- makelirs.rb 2003/02/25 17:46:20 1.1.1.1
+++ makelirs.rb 2003/03/13 08:16:50 1.1.1.1.2.1
@@ -1,6 +1,7 @@
# makelirs.rb $Revision: 1.3 $
#
# 更新情報をLIRSフォーマットのファイルに吐き出す
+# また、最新の日記のHTTPヘッダをファイルに吐き出す
#
# pluginディレクトリに置くだけで動作します。
#
@@ -18,6 +19,9 @@
# Copyright (C) 2002 by Kazuhiro NISHIYAMA
#
=begin ChangeLog
+2003-03-13 zunda <zunda at freeshell.org>
+ * also makes the header for the latest diary
+
2002-10-28 zoe <zoe@kasumi.sakura.ne.jp>
* merge 1.4. Thanks koyasu san.
@@ -34,9 +38,21 @@
add_update_proc do
file = @options['makelirs.file'] || 'antenna.lirs'
- # create_lirs
+ # parse the diary
t = TDiaryLatest::new( @cgi, "latest.rhtml", @conf )
body = t.eval_rhtml
+ # for header
+ head = {
+ 'type' => 'text/html',
+ 'Vary' => 'User-agent',
+ }
+ head['charset'] = @conf.charset
+ head['Content-Length'] = body.size.to_s
+ head['Last-Modified'] = CGI::rfc1123_date( t.last_modified )
+ File.open( "#{@cache_path}/latest_header", 'w' ) do |f|
+ f.print( @cgi.header( head ) )
+ end
+ # for lirs
# escape comma
e = proc{|str| str.gsub(/[,\\]/) { "\\#{$&}" } }
以下のようにindex.rbを改造して送信してみた。 3秒前後だったのが0.26秒くらいまで短縮されたよ。 ヘッダを送信した後にStandardErrorをraiseしているのがなさけないけど。
Index: index.rb
===================================================================
RCS file: /arpa/tz/z/zunda/cvs/d/index.rb,v
retrieving revision 1.1.1.1.2.3
retrieving revision 1.1.1.1.2.4
diff -u -r1.1.1.1.2.3 -r1.1.1.1.2.4
--- index.rb 2003/03/04 14:22:49 1.1.1.1.2.3
+++ index.rb 2003/03/13 09:05:09 1.1.1.1.2.4
@@ -3,6 +3,16 @@
$KCODE= 'e'
BEGIN { $defout.binmode }
+def timesRecord( message = nil )
+ begin
+ message = '' unless message
+ File.open( '/arpa/tz/z/zunda/d/data/cache/times', 'a' ) do |f|
+ f.puts "#{Time.now}|#{ENV['REMOTE_ADDR']}|#{ENV['REQUEST_URI']}|#{ENV['HTTP_USER_AGENT']}|#{Time.times.utime}|#{Time.times.stime}|#{message}"
+ end
+ rescue
+ end
+end
+
begin
if FileTest::symlink?( __FILE__ ) then
org_path = File::dirname( File::readlink( __FILE__ ) )
@@ -52,6 +62,10 @@
tdiary = TDiary::TDiaryCategoryLatest::new( @cgi, "category.rhtml", conf )
end
else
+ if /HEAD/i =~ @cgi.request_method and FileTest.exist?( "#{conf.data_path}cache/latest_header" ) then
+ print( File::open( "#{conf.data_path}cache/latest_header" ) {|f| f.read } )
+ raise StandardError, 'header sent'
+ end
tdiary = TDiary::TDiaryLatest::new( @cgi, "latest.rhtml", conf )
end
rescue TDiary::PermissionError
@@ -103,6 +117,7 @@
<body>Wait or <a href="#{$!.path}">Click here!</a></body>
</html>]
end
+rescue StandardError
rescue Exception
print "Content-Type: text/plain\n\n"
puts "#$! (#{$!.class})"
@@ -110,3 +125,4 @@
puts $@.join( "\n" )
end
+timesRecord
● [tDiary] HEADリクエスト応答の更なる軽量化
たださんのアイディアで、mod_rewriteを使ってみました。 まず、キャッシュしておいたHTTPヘッダだけを送信するCGIを 今はindex_head.cgiとして作成。
#!/bin/sh head_cache='/hoge/fuga/data/cache/latest_header' if [ -r $head_cache ]; then cat $head_cache else ./index.rb fi
そして、最新の日記に対するHEADリクエストをこのCGIにリダイレクトする。
RewriteEngine on以降にこんな感じで。
RewriteCond %{REQUEST_METHOD} HEAD [NC]
RewriteRule ^(index.rb)?$ index_head.cgi [L]
今のところはうまくリダイレクトできてるみたいです。 これでアンテナからのHEADリクエスト*1には、 ちょっと軽く応えられるようになったハズ。
*1 ログを取ってみると案外多いのにビックリ。
本日のツッコミ(全2件) [ツッコミを入れる]
ツッコミ・コメントがあればどうぞ! spam対策でE-mail欄は隠してあります。もしE-mail欄が見えていても、何も入力しないで下さい。
脳味噌から汁が出るくらい考える。こともある。
最近のツッコまれどころ