‹‹ All posts

Git post checkout hook for purging Magento caches

05 of November, 2013


Probably anyone who ever worked with Magento knows that it heavily relies on caches. Which, obviously, is good for speeding things up, but as a developer sometimes I find it quite annoying to have to clear it all the time when switching between features in development. I store my projects in Git and each feature is isolated into separate branch, so most obvious way to automate it was to use post checkout hook.

Git hooks are very straightforward. They are small custom scripts, which fire off when certain events happen. By default there are few sample executable bash scripts, which live in .git/hooks directory.

I use Vagrant for firing up and managing project virtual machine and usually have Redis and Varnish setup for caching. First I flush these service caches in my vagrant box, then I remove file caches. Like this:

#! /bin/sh

# go to vagrant path
cd ./$(git rev-parse --show-cdup)/tools/vagrant

# restart cache services in box
vagrant ssh -c 'redis-cli flushall && sudo /etc/init.d/redis restart && sudo /etc/init.d/varnish restart'

# go back to project root
cd ../../

# flush file caches
rm -Rf public/var/cache/*
rm -Rf public/var/full_page_cache/*
rm -Rf public/media/js/*
rm -Rf public/media/css/*
rm -Rf public/media/css_secure/*

# celebrate!
echo 'flushed magento caches'

Right. Looks slow. But is it too slow? On my laptop it takes up to 8 seconds. I still have to realize how painfully show it is in reality, but so far was okay - just enough time to sip my tea or water from a glass.

comments powered by Disqus