kvz.io
Published on

Flush Memcached Using Bash

Authors
  • avatar
    Name
    Kevin van Zonneveld
    Twitter
    @kvz

If you store application data in memcache, you may want to invalidate it once you deploy a new version to avoid corruption or weird results. There are several ways to do this but I recently tried one using nothing but Bash, and I like it.

Flush Memcache in Bash

Just add this to your deploy script:

$ echo "flush_all" | /bin/netcat -q 2 127.0.0.1 11211

(remember, all entries will be flushed. this is not the way to fly in high performance environments)

Bonus: Flush Disk Cache

Also, if you have cache files on disk, this is probably one of the best ways to trash them:

$ find YOUR/WEB/DIR/app/tmp/cache/ -type f -print0 | xargs -0 rm

It's actually a simplified version from what PHP uses to clean up session garbage files (see /etc/cron.d/php5)

What's good about this elaborate approach, is that it deals with

  • "argument list too long" by using find instead of a rm *
  • non-unix characters

print0 will delimit files by the 0 character, so you won't have to escape spaces or any other 'crazy' chars

Legacy Comments (7)

These comments were imported from the previous blog system (Disqus).

Robin Speekenbrink
Robin Speekenbrink·

Nice and to the point article...

a little off topic maybe: why not use the -delete option in find to delete all the found files instead of piping it to xargs etc? Does that have a special reason to it?

Kev van Zonneveld
Kev van Zonneveld·

@ Robin Speekenbrink: thx. No not really. Guess I\'m just really used to xargs as it gives you the power to do anything with files found. But -delete should work just fine!

Maurits
Maurits·

Thanks for sharing how to flush memcache in bash.. good article!

Kelvin Nicholson
Kelvin Nicholson·

Good tip. I added this to the end of my Hg hook to refresh things when I push out an update. Thanks!

Janis
Janis·

What does the "-q 2" part mean?

Kirrus
Kirrus·

Janis, it's a netcat option. From the man page:
-q seconds after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.

trungnguyen
trungnguyen·

Do you like Japan; A high level country on the world
Do you want to visit this beauty country? You want to see Sakura blossom season, Kyoto is an ancient city, a legendary Mount Fuji and more..?
Do you want to make business with Japanese people?
You really want to study Japanese language but you scare quality education in some language center?
You are making business with Japanese people; you want to your work development more. You want to get “Kaizen” in your company; you want to improve all your staff language skill?
You have a child and you want to bring them to get the best education system? You choose Japanese education
You are Japanese people, now you are living in Ha Noi, you want to study English, Vietnamese language. You try to understand Vietnamese culture, you love Vietnamese food but you don’t know how to bargain price, how to find special items, how to ask some question how to…???
Are you finding a good center to study Japanese in Ha Noi, Viet Nam?!
You already studied in other center but you are hopeless their skill? You want to change
You want to study by: one by one, or small group. Our teacher is Japanese teacher. 100% have diploma to teach people by Japanese government and so many experience for you.
Our course from Basic to High level. You can choice the way to study by yourself. Our service suitable for all of customer
With Japanese quality system in training. We believe to give you “what you want”

More detail, please contact to us Magin gam Viet Nam Co., Ltd
Add: No2/62, Ngoc Ha, Ha Noi, Viet Nam
Tel: (+84 4) 373 49010 Fax: (+84 4) 373 49011
Hotline (^0^)
Vietnamese support: Mr.Nghi (+84) 977 837 289
Foreigner support: Mr.Masa (+84)1658 195 949
Mr.Ataka (+84) 1287 382 225
E-mail: magingam.co.ltd@gmail.com
Also we are going to find partner in Ho Chi Minh and Da Nang

If you visit to Vietnam what do you should bring excluded your luggage. This is some information, some experience and special that is some knowledge to the country you arrive
Uhm, if you want. Pls call for us
SHIMIZU JAPANESE LANGUAGE CENTER - Magin gam Vietnam – 100% free