When Memcache runs as a PHP module (as indicated by the memcache.ini
file in /opt/eduserver/etc/php/conf.d
), flushing its data typically requires either restarting the PHP service or using a programmatic approach. Since you want to avoid service restarts (restartphp
or restartwww
options in your control script), we'll focus on CLI solutions.
The most direct way to flush Memcache is through its protocol interface. If Memcache is running on default port 11211:
echo "flush_all" | nc localhost 11211
# Or using telnet:
telnet localhost 11211
flush_all
quit
Create a standalone PHP script to execute the flush:
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$memcache->flush();
echo "Memcache flushed successfully\n";
?>
Execute it with: /opt/eduserver/bin/php /path/to/script.php
For quick operations without creating files:
/opt/eduserver/bin/php -a
> $m = new Memcache;
> $m->connect('localhost', 11211);
> $m->flush();
> exit;
For automated flushing, create a cron job:
# Add to crontab -e
0 3 * * * echo "flush_all" | nc localhost 11211 >/dev/null 2>&1
Check cache stats after flushing:
echo "stats" | nc localhost 11211 | grep "cmd_flush"
You should see the flush counter increment.
If methods fail:
- Verify Memcache is running:
ps aux | grep memcached
- Check connection:
telnet localhost 11211
- Confirm PHP module is loaded:
/opt/eduserver/bin/php -m | grep memcache
When working with PHP applications that utilize Memcache as a caching layer, you'll frequently need to clear the cache during development or troubleshooting. The situation described involves a custom server setup where Memcache is installed as a PHP module with configuration in /opt/eduserver/etc/php/conf.d/memcache.ini
.
Instead of restarting the entire web server or PHP service, you have several cleaner approaches:
# Method 1: Using telnet (if memcache is running on default port)
telnet localhost 11211
flush_all
quit
# Method 2: Using netcat (alternative to telnet)
echo "flush_all" | nc localhost 11211
# Method 3: PHP CLI approach
php -r '$m = new Memcache; $m->connect("localhost", 11211) or die("Could not connect"); $m->flush();'
Since you have access to the server control script at /opt/eduserver/eduserver
, you could extend its functionality:
#!/bin/bash
case "$1" in
flushmemcache)
echo "flush_all" | nc localhost 11211
echo "Memcache flushed successfully"
;;
# Existing cases...
esac
For regular maintenance, set up a cron job:
# Add to crontab -e
0 3 * * * echo "flush_all" | nc localhost 11211 >/dev/null 2>&1
After clearing the cache, verify using the stats command:
echo "stats" | nc localhost 11211 | grep "cmd_flush"
- Consider using cache namespaces instead of full flushes in production
- Monitor cache hit/miss ratios after flushing
- For multi-server setups, ensure you flush all memcache instances
If connection fails, verify memcache is running:
ps aux | grep memcached
netstat -tulnp | grep 11211
Check configuration in /opt/eduserver/etc/php/conf.d/memcache.ini
for the correct server specifications.