Symfony: Ignore unused GET parameters for caching

When browsing my cache folder I realised, that there are cache files for pages with GET parameters like "gclid", "utm_source", etc. - these are parameters for tracking ad campaigns and in my case they do not affect page contents. I have not found any ready solutions so here is mine. I am using Symfony 1.4.

I have created myViewCacheManager class with modified getCurrentCacheKey method - this method strips selected parameters from cache key.

lib/myViewCacheManager.class.php:

class myViewCacheManager extends sfViewCacheManager { public function getCurrentCacheKey() { $cacheKey = $this->routing->getCurrentInternalUri(); if ($getParameters = $this->request->getGetParameters()) { // clear ignored parameters if (is_array($this->options['cache_key_ignore']) && is_array($getParameters)) foreach ($this->options['cache_key_ignore'] as $key) unset($getParameters[$key]); $cacheKey .= false === strpos($cacheKey, '?') ? '?' : '&'; $cacheKey .= http_build_query($getParameters, null, '&'); } return $cacheKey; } }

Next we have to instruct Symfony to use our class by modifying factories.yml config.

apps/[...]/config/factories.yml

[...] all: [...] view_cache_manager: class: myViewCacheManager param: cache_key_use_vary_headers: true cache_key_use_host_name: true cache_key_ignore: [ gclid, x, y, utm_source, utm_medium, utm_campaign, utm_content ] [...]

Yes, in cache_key_ignore parameter we can specify parameters we want to ignore in cache system. I have selected gclid, utm_source, utm_medium, utm_campaign and utm_content - which are used to track ad campaigns, and also x and y which are generated by clicking on image form element to track mouse position (e.g. for server side image map) which I am not using in this project.

But wait - there is another parameter which is not affecting my pages - I have a routing with id and slug - where slug is only for bots and humans - I can strip that too, but I need to modify my class to also strip hardcoded parameters.

lib/myViewCacheManager.class.php:

class myViewCacheManager extends sfViewCacheManager { public function getCurrentCacheKey() { $cacheKey = $this->routing->getCurrentInternalUri(); // remove ignored keys from URL parameters if (is_array($this->options['cache_key_ignore']) && strpos($this->routing->getCurrentInternalUri(), '?') !== false) { $url = parse_url($this->routing->getCurrentInternalUri()); parse_str($url['query'], $params); foreach ($this->options['cache_key_ignore'] as $key) unset($params[$key]); $cacheKey = $url['path'] . (count($params) ? '?' . http_build_query($params) : ''); } if ($getParameters = $this->request->getGetParameters()) { // clear igonred parameters if (is_array($this->options['cache_key_ignore']) && is_array($getParameters)) foreach ($this->options['cache_key_ignore'] as $key) unset($getParameters[$key]); $cacheKey .= false === strpos($cacheKey, '?') ? '?' : '&'; $cacheKey .= http_build_query($getParameters, null, '&'); } return $cacheKey; } }

Then I modified my routing to use x parameter which will be stripped
url: /:id/:x/:page.html
and now only id and page parameters are used to build cache. I have got rid of hundreds of unnecessary files and gain some speed by not generating new cache files for some requests.

Remember - parameters will be stripped application-wide so prepare your ignore list with care and remember not to use these parameters to modify cached contents.

PS. Remember to clear cache to see results. :)

Tags: PHP