Changeset 21918
- Timestamp:
- 09/11/09 16:07:35 (4 years ago)
- Files:
-
- branches/1.3/WHATS_NEW (modified) (1 diff)
- branches/1.3/lib/config/config/factories.yml (modified) (1 diff)
- branches/1.3/lib/config/sfFactoryConfigHandler.class.php (modified) (2 diffs)
- branches/1.3/lib/task/generator/skeleton/app/app/config/factories.yml (modified) (1 diff)
- branches/1.3/lib/view/sfViewCacheManager.class.php (modified) (5 diffs)
- branches/1.3/test/unit/view/sfViewCacheManagerTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.3/WHATS_NEW
r21915 r21918 886 886 <p>No results.</p> 887 887 <?php endif; ?> 888 889 View cache 890 ---------- 891 892 The view cache manager nows accept params in factories.yml. Generating the 893 cache key for a view has been refactored in different methods to ease extending the class. 894 895 Two params are available in factories.yml: 896 * `cache_key_use_vary_headers` (default: true): specify if the cache keys 897 should include the vary headers part. In practice, it says if the page 898 cache should be http header dependent, as specified in `vary` cache 899 parameter. 900 * `cache_key_use_host_name` (default: true): specify if the cache keys should 901 include the host name part. In practice, it says if page cache should be 902 hostname dependant. 903 branches/1.3/lib/config/config/factories.yml
r21884 r21918 41 41 view_cache_manager: 42 42 class: sfViewCacheManager 43 param: 44 cache_key_use_vary_headers: true 45 cache_key_use_host_name: true 43 46 44 47 view_cache: branches/1.3/lib/config/sfFactoryConfigHandler.class.php
r21808 r21918 121 121 " \$class = sfConfig::get('sf_factory_view_cache', '%s');\n". 122 122 " \$cache = new \$class(sfConfig::get('sf_factory_view_cache_parameters', %s));\n". 123 " \$this->factories['viewCacheManager'] = new %s(\$this, \$cache );\n".123 " \$this->factories['viewCacheManager'] = new %s(\$this, \$cache, %s);\n". 124 124 " }\n". 125 125 " else\n". … … 127 127 " \$this->factories['viewCacheManager'] = null;\n". 128 128 " }\n", 129 $class, var_export($parameters, true), $config['view_cache_manager']['class'] );129 $class, var_export($parameters, true), $config['view_cache_manager']['class'], var_export($config['view_cache_manager']['param'], true)); 130 130 break; 131 131 branches/1.3/lib/task/generator/skeleton/app/app/config/factories.yml
r21845 r21918 40 40 generate_shortest_url: true 41 41 extra_parameters_as_query_string: true 42 43 view_cache_manager: 44 class: sfViewCacheManager 45 param: 46 cache_key_use_vary_headers: true 47 cache_key_use_host_name: true branches/1.3/lib/view/sfViewCacheManager.class.php
r21908 r21918 37 37 * @see initialize() 38 38 */ 39 public function __construct($context, sfCache $cache )40 { 41 $this->initialize($context, $cache );39 public function __construct($context, sfCache $cache, $options = array()) 40 { 41 $this->initialize($context, $cache, $options); 42 42 } 43 43 … … 48 48 * @param sfCache $cache An sfCache instance 49 49 */ 50 public function initialize($context, sfCache $cache )50 public function initialize($context, sfCache $cache, $options = array()) 51 51 { 52 52 $this->context = $context; 53 53 $this->dispatcher = $context->getEventDispatcher(); 54 54 $this->controller = $context->getController(); 55 $this->options = array_merge(array( 56 'cache_key_use_vary_headers' => true, 57 'cache_key_use_host_name' => true, 58 ), $options); 55 59 56 60 if (sfConfig::get('sf_web_debug')) … … 158 162 } 159 163 164 $cacheKey = sprintf('/%s/%s/%s', $this->getCacheKeyHostNamePart($hostName), $this->getCacheKeyVaryHeaderPart($internalUri, $vary), $cacheKey); 165 166 // replace multiple / 167 $cacheKey = preg_replace('#/+#', '/', $cacheKey); 168 169 return $cacheKey; 170 } 171 172 /** 173 * Gets the vary header part of view cache key. 174 * 175 * @param string $vary 176 * @return string 177 */ 178 protected function getCacheKeyVaryHeaderPart($internalUri, $vary = '') 179 { 180 if (!$this->options['cache_key_use_vary_headers']) 181 { 182 return ''; 183 } 184 160 185 // prefix with vary headers 161 186 if (!$vary) 162 187 { 163 188 $varyHeaders = $this->getVary($internalUri); 164 if ($varyHeaders) 189 190 if (!$varyHeaders) 165 191 { 166 sort($varyHeaders); 167 $request = $this->context->getRequest(); 168 $vary = ''; 169 170 foreach ($varyHeaders as $header) 171 { 172 $vary .= $request->getHttpHeader($header).'|'; 173 } 174 175 $vary = $vary; 192 return 'all'; 176 193 } 177 else 194 195 sort($varyHeaders); 196 $request = $this->context->getRequest(); 197 $vary = ''; 198 199 foreach ($varyHeaders as $header) 178 200 { 179 $vary = 'all';201 $vary .= $request->getHttpHeader($header).'|'; 180 202 } 181 203 } 182 204 183 // prefix with hostname 205 return $vary; 206 } 207 208 /** 209 * Gets the hostname part of view cache key. 210 * 211 * @param string $hostName 212 * @return void 213 */ 214 protected function getCacheKeyHostNamePart($hostName = '') 215 { 216 if (!$this->options['cache_key_use_host_name']) 217 { 218 return ''; 219 } 220 184 221 if (!$hostName) 185 222 { 186 $ request = $this->context->getRequest();187 $hostName = $request->getHost();188 } 223 $hostName = $this->context->getRequest()->getHost(); 224 } 225 189 226 $hostName = preg_replace('/[^a-z0-9\*]/i', '_', $hostName); 190 $hostName = strtolower(preg_replace('/_+/', '_', $hostName)); 191 192 $cacheKey = sprintf('/%s/%s/%s', $hostName, $vary, $cacheKey); 193 194 // replace multiple / 195 $cacheKey = preg_replace('#/+#', '/', $cacheKey); 196 197 return $cacheKey; 227 $hostName = preg_replace('/_+/', '_', $hostName); 228 229 return strtolower($hostName); 198 230 } 199 231 … … 397 429 /** 398 430 * Returns true if the action is cacheable. 399 * 431 * 400 432 * @param string $moduleName A module name 401 433 * @param string $actionName An action or partial template name 402 * 434 * 403 435 * @return boolean True if the action is cacheable 404 * 436 * 405 437 * @see isCacheable() 406 438 */ … … 668 700 /** 669 701 * Checks that the supplied parameters include a cache key. 670 * 702 * 671 703 * If no 'sf_cache_key' parameter is present one is added to the array as 672 704 * it is passed by reference. 673 * 705 * 674 706 * @param array $parameters An array of parameters 675 * 707 * 676 708 * @return string The cache key 677 709 */ branches/1.3/test/unit/view/sfViewCacheManagerTest.php
r19531 r21918 4 4 * This file is part of the symfony package. 5 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 6 * 7 7 * For the full copyright and license information, please view the LICENSE 8 8 * file that was distributed with this source code. … … 12 12 require_once($_test_dir.'/unit/sfContextMock.class.php'); 13 13 14 $t = new lime_test(3 3);14 $t = new lime_test(36); 15 15 16 16 class myViewCacheManager extends sfViewCacheManager … … 265 265 ); 266 266 } 267 268 // ->initialize() 269 $t->diag('Cache key generation options'); 270 $m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_vary_headers' => false)); 271 $t->is($m->generateCacheKey('mymodule/myaction'), '/localhost/mymodule/myaction', '->generateCacheKey() uses "cache_key_use_vary_headers" option to know if vary headers changes cache key.'); 272 273 $m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_host_name' => false)); 274 $t->is($m->generateCacheKey('mymodule/myaction'), '/all/mymodule/myaction', '->generateCacheKey() uses "cache_key_use_host_name" option to know if vary headers changes cache key.'); 275 276 $m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_host_name' => false, 'cache_key_use_vary_headers' => false)); 277 $t->is($m->generateCacheKey('mymodule/myaction'), '/mymodule/myaction', '->generateCacheKey() allows the use of both "cache_key_use_host_name" and "cache_key_use_vary_headers" options.'); 278