Ya' row ze: Lets go!

You are here

Flushing Memcache in Acquia's Cloud Hooks

Using memcache with Drupal has some interesting problems.
1) If you do continuous rebuilds, you'll notice that memcache doesn't purge it's cache during site install - causing the old configuration to kick in with new code.
2) Certain drush commands can wind up using the cache instead of the updated code values.

I've found that it's easier to purge all of the memcache servers at the very beginning of the acquia cloud hooks just to make sure that everything has been properly cleared before running the environmental builds. One problem I ran into is that you need to know the addresses of the memcache servers. That's easy to get via drush/code based on the docs here: https://docs.acquia.com/articles/memcache-monitoring-and-flush-using-nc-...

But how can you then iteratively flush them w/o hardcoding the servers as the article mentions. I wound up putting together this (very strange looking) command:

drush @$drush_alias vget memcache_servers|grep -E "'.+:[0-9]+'.*$"|sed -E -e "s/^[[:space:]]+'(.+):([0-9]+).*$/\/bin\/echo -e 'flush_all\\\\nquit' | nc -q1 \1 \2/e"

All this does is convert the php array that is the list of servers line by line into the flush command. Here's a breakdown of what each part of the command does:

1) drush @$drush_alias vget memcache_servers
This returns a servers list that looks something like this:

memcache_servers: Array
(
[database-1234.prod.hosting.acquia.com:11211] => default
[database-5678.prod.hosting.acquia.com:11211] => default
)

2) grep -E "'.+:[0-9]+'.*$"
this choses only the lines that look like this: "[database-1234.prod.hosting.acquia.com:11211] => default" and excludes the others.

3) sed -E -e "s/^[[:space:]]+'(.+):([0-9]+).*$/\/bin\/echo -e 'flush_all\\\\nquit' | nc -q1 \1 \2/e"
This converts the chosen lines into "/bin/echo -e 'flush_all\nquit'|nc -q1 \1\2"
because it's a CLI command, there are some strange things here: these regexes need POSIX character classes (so [[:space:]] instead of "\s". Also backslashes that need to result in back slashes need to be in sets of four so: "\\\\n" results in "\n".
Lastly, using the "/e" at the end of the command allows the replaced line to be executed.

Using this, we can be sure that all memcache servers associated with the cluster are flushed before the script can ontinue.

Back to top