symfonyã®ãã£ãã·ã¥ã®æåã¾ã¨ã
symfonyã®ãã£ãã·ã¥ã«ã¤ãã¦ãè¨å®å¤ã®çµã¿åããã«ãã£ã¦å¤ããæåã調ã¹ã¦ã¿ã¾ããã
ã¯ããã«ã»ãã£ãã·ã¥ã®ãããã
symfonyã§æ±ããã£ãã·ã¥ã«ã¯ããã¤ãã®ç¨®é¡ãããã¾ãã
- ã¢ã¯ã·ã§ã³ãã£ãã·ã¥
- ãµã¼ãã¼å´ãã¢ã¯ã·ã§ã³ã®å®è¡çµæããã£ãã·ã¥ããã¾ãã
- ãã¼ã¸ãã£ãã·ã¥
- ãµã¼ãã¼å´ãç¹å®ã®ãªã¯ã¨ã¹ãã®å®è¡çµæã®ãã¼ã¸å ¨ä½ããã£ãã·ã¥ããã¾ãã
- ã¯ã©ã¤ã¢ã³ããã£ãã·ã¥
- ã¯ã©ã¤ã¢ã³ãå´ããã£ãã·ã¥å¶å¾¡ããããªã©ã«ããããã©ã¦ã¶ã§ãã£ãã·ã¥ããã¾ãã
settings.ymlâcacheã®on/off
symfonyã®ãã£ãã·ã¥æ©è½ãæå¹ã«ããã«ã¯ãsettings.ymlã§cache = on ã«ãã¾ãã
prod: .settings: cache: on
ãã®ãã©ã°ã®on/offã®éãã¯ä»¥ä¸ã®éãã§ãã
cache=off
ããã©ã«ãã®filterãã§ã¤ã³ã«sfCacheFilterã追å ããã¾ããã
ã¾ããã°ãã¼ãã«ãªè¨å®å¤ãsf_cacheããfalseã¨ãªãã¾ãã
cache=on
ããã©ã«ãã®filterãã§ã¤ã³ã«sfCacheFilterã追å ããã¾ãã
sf_cacheãtrueã«ãªãã¾ãã
sfCacheFilterã追å ããããã©ããã¯ãsymfonyã©ã¤ãã©ãªã®æ¬¡ã®ãã¡ã¤ã«ã® %SF_CACHE% é¨åã§å¶å¾¡ããã¦ãã¾ãã
# /lib/config/config/filters.yml cache: class: sfCacheFilter param: condition: %SF_CACHE%
ãã®ãã¡ã¤ã«ã®ãã¼ã¹ãsfFilterConfigHandlerã§è¡ãããconditionã®å¤ã«å¿ãã¦ãã£ã«ã¿ã¨ã³ããªãè¨å®ã«è¿½å ããããã©ãããå¶å¾¡ãã¦ãã¾ãã
ï¼ããã¸ã§ã¯ãã®ãã£ãã·ã¥ãã£ã¬ã¯ããªå
ã®ãconfig/modules_(ã¢ã¸ã¥ã¼ã«å)_config_filters.yml.phpã«åºåããã¾ãï¼
cache.ymlâenabledã®on/offãwith_layoutãlifetimeãclient_lifetime
settings.ymlã§cache=onã«è¨å®ããã ãã§ã¯ãå®éã«ãã£ãã·ã¥ã®æ©è½ã¯åä½ãã¾ããã
ãã£ãã·ã¥ã®æ©è½ãå®è¡ããã«ã¯ãcache.ymlã«ã¦enabled=onã«è¨å®ãã¦åãã¦ããã£ãã·ã¥ãå®éã«åä½ãã¾ãã
ãã®å
ã¯ãè¨å®ã«å¿ãã¦ä½¿ãããä»çµã¿ã®çµã¿åãããå¤ãã£ã¦ãã¾ãã
enabled=on / with_layout=false / lifetime > 0
enabledãã©ã°ã ããæå¹ã«ããã¨ããã¢ã¯ã·ã§ã³ãã£ãã·ã¥ãã®æ©è½ãæå¹ã«ãªãã¾ããã¢ã¯ã·ã§ã³ã®å®è¡çµæããµã¼ãã¼å´ã§ãã£ãã·ã¥ããã¾ããã¯ã©ã¤ã¢ã³ãï¼ãã©ã¦ã¶ï¼å´ã§ã¯ããã£ãã·ã¥ã¯è¡ããã¾ããã
â»lifetime=0ã«ããã¨ãã¢ã¯ã·ã§ã³ã¯ãã£ãã·ã¥ããã¾ããã
å®éãsfExecutionFilterããviewã®ã¬ã³ããªã³ã°ï¼renderï¼ãå®è¡ãããæç¹ã§ãå®è¡çµæããã£ãã·ã¥ã«ä¿åããã¾ããPHPViewã®å ´åã¯sfPHPView::rener()å ã
ãã£ãã·ã¥ãä¿åããã¦ããã¢ã¯ã·ã§ã³ã¯ã次åå®è¡æã«sfExecutionFilterã®executeAction()ã®å¼ã³åºããçç¥ãããã¬ã³ããªã³ã°æç¹ï¼sfPHPView::renderï¼ã§å¯¾å¿ãããã£ãã·ã¥ããçµæãèªã¿è¾¼ã¾ãã¾ãã
enabled=on / with_layout=true / lifetime > 0
enabledãã©ã°ã¨åããã¦with_layoutãtrueã«ããã¨ããã¢ã¯ã·ã§ã³ãã£ãã·ã¥ãã§ã¯ãªãããã¼ã¸ãã£ãã·ã¥ãã®æ©è½ãæå¹ã«ãªãã¾ããï¼lifetimeã0ãã大ããå ´åï¼
ãã®å ´åã¯ãsfCacheFilter::executeBeforeRenderingå ã§ãç¹å®ã®URLã«å¯¾ããã¬ã³ããªã³ã°çµæããã£ãã·ã¥ã¨ãã¦ä¿åããã¾ããï¼ã¢ã¯ã·ã§ã³ãã¨ã®ãã£ãã·ã¥ã¯ä¿åããã¾ããï¼
ãã£ãã·ã¥ãä¿åããã¦ããURLã®æ¬¡åå¼ã³åºãæã¯ãsfCacheFilter::executeBeforeExecutionã§ãã£ãã·ã¥ããå®è¡çµæã復å ããã¾ãããã®å ´åãã¢ã¯ã·ã§ã³ã®å®è¡ã¯ã¹ãããããã¾ãã
ã¯ã©ã¤ã¢ã³ãå´ã®ãã£ãã·ã¥ã¯ããã®è¨å®ã§ã¯ãlifetimeã«æå®ããæéã ããã£ãã·ã¥ãæå¹ã«ãªãããã«ããããçæããã¾ãï¼Expiresãããã¨Cache-Controlãããã®max-ageï¼
â
ãã®Expiresãããã®æ¥æã¯ããã¼ã¸ãã£ãã·ã¥ã®ä½ææ¥æãåºæºã¨ãªããã¨ã«æ³¨æãå¿
è¦ã§ãã
enabled=on / with_layout=true / lifetime > 0 / client_lifetime = 0
ãã®å ´åã¯ããã¼ã¸ãã£ãã·ã¥ãæå¹ã§ãããã«ããã©ã¦ã¶å´ã«ã¯ãExpiresããããã®ã¿ãè¿ããã¾ããï¼max-ageãã¤ãã¾ããï¼
ãããã£ã¦ã次ã«åãURLããã©ã¦ã¶ãããªã¯ã¨ã¹ãããéã¯ããIf-Modified-Sinceãããããä»å ããã¾ãã
If-Modified-SinceãããããããURLã«å¯¾ããã³ã³ãã³ãã®å
容ã«å¤åããªãå ´åãsymfonyããã¯ã304 Not Modifiedãã¨ããã¬ã¹ãã³ã¹ãè¿ãããã®ãæã¾ããæåãªã®ã§ãããããããä¸å
·åã«ããã304ã§ã¯ãªãã¦é常ã®200ã§å¿çãè¿ã£ã¦ãã¾ããï¼å¾è¿°ï¼
enabled=on / with_layout=true / lifetime > 0 / client_lifetime > 0
client_lifetimeãæå®ããªãå ´åã¨ã»ã¼åãã§ãããExpiresã¨max-ageããclient_lifetimeã§æå®ããæ°åãå
ã«è¨ç®ããã¾ããï¼client_lifetimeã¯ãæå®ããªãå ´ålifetimeã®å¤ã¨åä¸ã«ãªãã¾ãï¼
settings.ymlâetag=on
etagã®è¨å®ãæå¹ã«ãªãã¾ãããcache.ymlã§ä»¥ä¸ã®è¨å®ã®å ´åã«ã®ã¿ãå®éã«ETagãåºåããã¾ãã
enabled: on with_layout: true lifetime: 180 # 0ããã大ããå¤ client_lifetime: 0
ã¤ã¾ããä¸è¨ã®ãIf-Modified-Sinceããããã使ããããã¿ã¼ã³ã§ãåæã«ãIf-None-Matchããããã§ETagãæå®ãããããã«ãªãã¾ãã
â»ãã ãä¸ã§è¿°ã¹ãããã«ãä¸å
·åããããããããä¿®æ£ããªãã¨ETagãåºåããã¾ããã
ä¸å ·åã¨æãããç®æãã®1
sfCacheFilter.php executeBeforeExecution()å
http://trac.symfony-project.org/browser/branches/1.2/lib/filter/sfCacheFilter.class.php#L71
<?php 71 public function executeBeforeExecution() 72 { 73 $uri = $this->routing->getCurrentInternalUri(); 74 75 if (is_null($uri)) 76 { 77 return true; 78 } 79 80 // page cache 81 $cacheable = $this->cacheManager->isCacheable($uri); 82 if ($cacheable && $this->cacheManager->withLayout($uri)) 83 { 84 $inCache = $this->cacheManager->getPageCache($uri); $this->response = $this->context->getResponse(); // âã³ã³ 85 $this->cache[$uri] = $inCache; 86 87 if ($inCache) 88 { 89 // page is in cache, so no need to run execution filter 90 return false; 91 } 92 } 93 94 return true; 95 }
ãã¼ã¸ãã£ãã·ã¥ããã£ãã·ã¥ãã¡ã¤ã«ãã復å
ããå¦çã84è¡ç®ã®getPageCache()ã§ããã®ã¡ã½ããå
ã§ãã£ãã·ã¥ãã¡ã¤ã«ã®å
容ãunserializeããã³ã³ããã¹ããä¿æãã¦ããsfWebResponseãªãã¸ã§ã¯ããç½®ãæãã¾ãã
ããããsfCacheFilterãªã©ãä¿æãã¦ããsfWebResponseã®åç
§ï¼$this->responseï¼ãªã©ã¯sfCacheFilterã®åæåï¼initializeã¡ã½ããï¼æç¹ã§ã³ã³ããã¹ããä¿æãã¦ãããªãã¸ã§ã¯ãã¸ã®åç
§ã®ã¾ã¾ã§ããããã£ãã·ã¥ãã復å
ãããã®ã§ã¯ããã¾ããã
ãªã®ã§ããã£ãã·ã¥ãã復å ããå ´åã¯ãã®ç´å¾ã«ãsfCacheFilterå ã§ä¿æãã¦ããåç §ãå度æ´æ°ããå¿ è¦ããããã¨ããã®ãä¸è¨ã§ãã
ãã®ä¸å
·åãããããã«ãsfCacheFilter::checkCacheValidation()å
ã§304ããããªã©ãè¨å®ãã¦ããæçµçã«åºåãããã¬ã¹ãã³ã¹ã«åæ ãããªããã¨ããæåã«ãªã£ã¦ãã¾ã£ã¦ãã¾ãã
ä¸å ·åã¨æãããç®æãã®2
sfCacheFilter.php checkCacheValidationå
http://trac.symfony-project.org/browser/branches/1.2/lib/filter/sfCacheFilter.class.php#L209
<?php 207 // conditional GET support 208 // never in debug mode 209 if ($this->response->hasHttpHeader('Last-Modified') && !sfConfig::get('sf_debug')) 210 { 211 $lastModified = $this->response->getHttpHeader('Last-Modified'); if (is_array($lastModified)) { // âã³ã³ 212 $lastModified = $lastModified[0]; } // âã³ã³ 213 if ($this->request->getHttpHeader('IF_MODIFIED_SINCE') == $lastModified) 214 { 215 $this->response->setStatusCode(304); 216 $this->response->setHeaderOnly(true); 217 218 if (sfConfig::get('sf_logging_enabled')) 219 { 220 $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Last-Modified matches If-Modified-Since (send 304)'))); 221 } 222 } 223 }
sfWebResponse::getHttpHeader()ã®æ»ãå¤ãé
åã§ãããã¨ãåæã¨ãã¦ããããã§ãããããããgetHttpHeaderã§é
åãè¿ã£ã¦ãããã¨ãããã®ãã¡ãã£ã¨åããã¾ãããé常ã®ãããã§ã¯æååãè¿ã£ã¦ããããã§ãã
é
åãè¿ã£ã¦ãããã¨ãèæ
®ãã¦ãä¸è¨ã®ããã«é
åã®å ´åã¯å
é ã®è¦ç´ ã®ã¿ã使ç¨ããã¨ããifã追å ãã¾ãã
ãã®2ã«æã®ä¿®æ£ãè¡ãã¨ãæ£ãã304 Not Modifiedå¿çãè¿ãããããã«ãªãã¾ãã