2

I use Memcache to store content lists with very key combinations, when user edited the content, I must refresh the cache, but it is hard to say what particular list to refresh, it is not either a good idea to flush the entire Memcache server, so my question is: Can I group the Memcache keys so that I can flush a group and not the total Memcache?

Christophe
  • 74,672
  • 10
  • 115
  • 187
Yu Jiaao
  • 131
  • 4

1 Answers1

1

You can't do this with one request only.

But you can loop on every keys using the same prefix to delete all of them like so :

$cache = new Memcached();
$keys = $cache->getAllKeys();
$prefix = "always_this_one_";
$prefixLength = strlen($prefix);


foreach ($keys as $key) {
    if (substr_compare($key, $prefix, 0, $prefixLength) === 0) {
        $cache->delete(substr($key, 0, -1));
    }
}
Steve Chamaillard
  • 1,691
  • 1
  • 11
  • 20