æ¢åã®sinatraãPadrino::Cacheãã(2011å¹´11æç¾å¨)
ã©ããæ°æã¡æªãã®ã§ã¡ã¢ã
sinatraãrailsç¸å½ã«é«æ©è½åããpadrinoã¨ãããã®ãããã¾ããéç¨å®ç¸¾ã¨ãèããã¨ãããªãæ©è½ã ã¨ãã¾ã¯ã¾ã railsã§è¡ãã¹ãã¨æãã¾ããããµã³ãã«ã½ã¼ã¹è¦ãéãrinariãããårailsããªã¨ããã ãsinatraãªã®ã§railsã«æ¯ã¹ã¦ã«ã¼ãã£ã³ã°ãé«é(ããã)ãsinatra+padrinoã¯ãã®ãã¡railsããªãã¬ã¼ã¹ããã®ãããªã¼ã¨ããæãã¾ãããããè¯ããã®ã ã¨æãã¾ããã
ã¤ã³ã¹ãã¼ã«
gem install padrino #ãããã¯Gemfileã§
å©ç¨ãã
åæ©è½ãç¬ç«ãã¦ä½¿ãäºãã§ããã¨ããã®ã§æ¢åã®sinatraã«ä½¿ã£ã¦ã¿ããå°ããã¯ã¾ã£ãã
èªã¿è¾¼ãã æã«ããã©ã«ãã§ãã¡ã¤ã«ãã£ã·ã¥ãã«ã¼ã/tmp/#{app_name}/cacheã«ã¤ãããã¨ããã£ã½ããapp_nameãªãã¦setããã¦ãªãã®ã§ã¨ããããset :app_nameããªãã¨ã ãã ã£ãã
ãã¨ã¨ã³ã³ã¼ãã£ã³ã°ã§ã³ã±ãã®ã§ã²ã¨ã¤monkeyãã
loggerã¨ãã¦module Kernelã«loggerã§ä½ã£ãPadrino::loggerã使ãããã«ãªã£ã¦ããã®ã ããã©ããå°ãªãã¨ãèªåã®ç°å¢ã§ã¯Kernelã«ã¶ãä¸ãããã¦ãã¯ãã®ogger==Padrino::loggerãrubyçµã¿è¾¼ã¿ã®loggerã«åãæ¿ãã£ã¦ãã¾ã£ã¦ããããªãã¦ã¤ã©ã£ã¨ããããããloggerãåå®ç¾©ãã¦åãæããã
ãã®ãããã½ã¼ã¹ãèªãã¯ãã«ãªããªãããã«ãªãã¨ããã§ãããèªãã§ãå°ããã®ã§ã¾ãããã©ãã¯ç¡ãã§ããèªãåæéã¯ãã£ã±ããããã
åãæå°ã³ã¼ãã¯ãããªæãã§ãã
require 'sinatra/base' require 'padrino-core/application/routing' require 'padrino-cache' module Padrino module Cache module Store class File def get(key) init if ::File.exist?(path_for_key(key)) puts key contents = ::File.read(path_for_key(key)).force_encoding('ASCII-8BIT') #<= ã³ã³ expires_in, body = contents.split("\n", 2) expires_in = expires_in.to_i if expires_in == -1 or Time.new.to_i < expires_in Marshal.load(body) if body else delete(key) nil end else nil end end end end end end class MyApp < Sinatra::Application set :app_name, "musicdb_dev" end class MyApp < Sinatra::Application register Padrino::Routing register Padrino::Cache #loggerãPadrino::loggerãããªããªããæåã¯è¿½ãããããã¦ããªãã def logger Padrino::logger end enable :caching get "/cachetest",:cache => true do expires_in 60 "cached?" end end
幸ãã«ãªãããã©ãã
ãã¼ã¿ã½ã¼ã¹ã¢ã¯ã»ã¹ããªãåç´ãªerbãã¼ã¸ã§ãã£ãã·ã¥çæã®ããã«ååã¯åæéããããã
æéæè¦ã¯ãã£ãã·ã¥ãªãã§æ¯å100msã®ãã¼ã¸ãåå200msã§ãã£ãã·ã¥é
ä¿¡ã¯10msã¨ããã¤ã¡ã¼ã¸ã
ã¾ãã¡ã¢ãªéç¨ã®Varnishãè½ã¡ã¦monitã§å¸°ã£ã¦ãããã¨ããæã«unicornãççºããªãã¨ããæå³ã§ã¯å®å
¨å¼ç幸ç¦ãªã®ããcache_keyãããã£ã¦querystringãå«ãããã£ãã·ã¥ã¯å¯è½ãªã®ã§ååçæã許容ã§ããªãããªãGoodã2åãªãä½è£ã§è¨±å®¹ã§ããããªã¼ãã¨ãwebserverã§ãã¤ã¬ã¯ãã«é
ä¿¡ããã®ã¯ãã£ãã·ã¥ã®ä¸èº«ãMarshal.dumpãã¦ãã ããªã®ã§ç¡çãªã®ããã
Nginxã§ãã£ãã·ã¥å¹ããã¦ããã°Varnishè½ã¡ã¦ãNginxã®ãã£ãã·ã¥ãåããªãã¨ãããããããã£ãã·ã¥ã ãã®ããã«Padrinoå
¥ããã®ã¯ãã¾ãã¯ã¼ã«ã§ã¯ãªããªãã¨ããããªãã¯ããããPadrinoã§ã¨ããç¶æ³ããã¯ãæã¾ããã§ããã
è¿½è¨ 11/23
app_nameãsetããªãã¨ãã¡ãªé¨åã«ã¤ãã¦ã¯æ¬å®¶ãmergeãã¦ãããããã§ãã
class MyApp < Sinatra::Application set :app_name, "musicdb_dev" end
ããã¯ä¸è¦ã«ãªãã¾ããã