» Flush memcached using BASH

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

You probably shouldn't follow me


Like this Article?

I'd appreciate it if you leave a comment, spread the word, or consider a small donation


tags: memcached, flush, netcat, bash, cakephp
category: Howto - System
read: 22,682 times

Add comment

(required, shown)(required, not shown)for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

 Track replies: rss feed comments feed

Comments

#6. Kirrus on 17 January 2012

Gravatar.com: KirrusJanis, 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.

#5. Janis on 06 June 2011

Gravatar.com: JanisWhat does the "-q 2" part mean?

#4. Kelvin Nicholson on 12 June 2010

Gravatar.com: Kelvin NicholsonGood tip. I added this to the end of my Hg hook to refresh things when I push out an update. Thanks!

#3. Maurits on 14 January 2010

Gravatar.com: MauritsThanks for sharing how to flush memcache in bash.. good article!

#2. Kevin on 17 September 2009

Twitter.com: kvz@ 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!

#1. Robin Speekenbrink on 08 September 2009

Gravatar.com: Robin SpeekenbrinkNice 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?