Search code examples
phpcachingmemcached

Clear specific item cache using MEMCACHE PHP


I used to clear cash using flush() in PHP every day, like 5-10 times. I want to clear specific items cache to prevent caching all server cache. This is the right way:

  1. Use Memcache::delete() to delete item
  2. Memcache::add() the same item

Is it correct?

https://www.php.net/manual/en/book.memcache.php


Solution

  • Yes, that is correct.

    But be aware that Memcache::add() will fail if the key already exists. If you always want to write the data even though it exists already, you can use Memcache::set() instead.

    Another a little bit funky thing with the memcache PHP class, is that TTL is measured in seconds, but if it is larger than 30 days it instead is interpreted as a date stamp (unix timestamp).