Development

Changeset 21918

You must first sign up to be able to contribute.

Changeset 21918

Show
Ignore:
Timestamp:
09/11/09 16:07:35 (4 years ago)
Author:
hartym
Message:

[1.3] fixed #4245. View cache manager now accepts params in factories.yml, added two params to enable or disable use of vary headers and host name in cache key generation, default to use all (as in symfony 1.0 1.1 and 1.2).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.3/WHATS_NEW

    r21915 r21918  
    886886      <p>No results.</p> 
    887887    <?php endif; ?> 
     888 
     889View cache 
     890---------- 
     891 
     892The view cache manager nows accept params in factories.yml. Generating the 
     893cache key for a view has been refactored in different methods to ease extending the class. 
     894 
     895Two 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  
    4141  view_cache_manager: 
    4242    class: sfViewCacheManager 
     43    param: 
     44      cache_key_use_vary_headers: true 
     45      cache_key_use_host_name:    true 
    4346 
    4447  view_cache: 
  • branches/1.3/lib/config/sfFactoryConfigHandler.class.php

    r21808 r21918  
    121121                             "    \$class = sfConfig::get('sf_factory_view_cache', '%s');\n". 
    122122                             "    \$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". 
    124124                             "  }\n". 
    125125                             "  else\n". 
     
    127127                             "    \$this->factories['viewCacheManager'] = null;\n". 
    128128                             "  }\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)); 
    130130          break; 
    131131 
  • branches/1.3/lib/task/generator/skeleton/app/app/config/factories.yml

    r21845 r21918  
    4040      generate_shortest_url:            true 
    4141      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  
    3737   * @see initialize() 
    3838   */ 
    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); 
    4242  } 
    4343 
     
    4848   * @param sfCache   $cache    An sfCache instance 
    4949   */ 
    50   public function initialize($context, sfCache $cache
     50  public function initialize($context, sfCache $cache, $options = array()
    5151  { 
    5252    $this->context    = $context; 
    5353    $this->dispatcher = $context->getEventDispatcher(); 
    5454    $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); 
    5559 
    5660    if (sfConfig::get('sf_web_debug')) 
     
    158162    } 
    159163 
     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 
    160185    // prefix with vary headers 
    161186    if (!$vary) 
    162187    { 
    163188      $varyHeaders = $this->getVary($internalUri); 
    164       if ($varyHeaders) 
     189 
     190      if (!$varyHeaders) 
    165191      { 
    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'; 
    176193      } 
    177       else 
     194 
     195      sort($varyHeaders); 
     196      $request = $this->context->getRequest(); 
     197      $vary = ''; 
     198 
     199      foreach ($varyHeaders as $header) 
    178200      { 
    179         $vary = 'all'; 
     201        $vary .= $request->getHttpHeader($header).'|'; 
    180202      } 
    181203    } 
    182204 
    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 
    184221    if (!$hostName) 
    185222    { 
    186       $request = $this->context->getRequest(); 
    187       $hostName = $request->getHost(); 
    188     } 
     223      $hostName = $this->context->getRequest()->getHost(); 
     224    } 
     225 
    189226    $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); 
    198230  } 
    199231 
     
    397429  /** 
    398430   * Returns true if the action is cacheable. 
    399    *  
     431   * 
    400432   * @param  string $moduleName A module name 
    401433   * @param  string $actionName An action or partial template name 
    402    *  
     434   * 
    403435   * @return boolean True if the action is cacheable 
    404    *  
     436   * 
    405437   * @see isCacheable() 
    406438   */ 
     
    668700  /** 
    669701   * Checks that the supplied parameters include a cache key. 
    670    *  
     702   * 
    671703   * If no 'sf_cache_key' parameter is present one is added to the array as 
    672704   * it is passed by reference. 
    673    *  
     705   * 
    674706   * @param  array  $parameters An array of parameters 
    675    *  
     707   * 
    676708   * @return string The cache key 
    677709   */ 
  • branches/1.3/test/unit/view/sfViewCacheManagerTest.php

    r19531 r21918  
    44 * This file is part of the symfony package. 
    55 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
    6  *  
     6 * 
    77 * For the full copyright and license information, please view the LICENSE 
    88 * file that was distributed with this source code. 
     
    1212require_once($_test_dir.'/unit/sfContextMock.class.php'); 
    1313 
    14 $t = new lime_test(33); 
     14$t = new lime_test(36); 
    1515 
    1616class myViewCacheManager extends sfViewCacheManager 
     
    265265  ); 
    266266} 
     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