The delete
method in PHP's Memcached extension is commonly used to remove a key-value pair from the Memcached server. This can be particularly useful when you want to invalidate specific cached data before it naturally expires due to time or memory constraints.
Here's an example of how to use the Memcached::delete
method:
<?php $mem = new Memcached(); $mem->addServer("localhost", 11211); // Store a value for future use $mem->set('my_key', 'Hello, world!', 60); // The value will be stored for 60 seconds // Later on... $response = $mem->delete('my_key'); if ($response) { echo "Key deleted successfully."; } else { echo "Failed to delete key."; } ?>
In this code, we first create a connection to the Memcached server running on localhost at port 11211. We then store a value using set
, with a time limit of 60 seconds. Afterwards, we use delete
to remove that key-value pair immediately. The delete
method returns a boolean indicating whether the deletion was successful.
delete
function as it helps identify whether the operation was successful or not.delete
function's return value contains useful information about the success or failure of the operation, which should not be ignored.Q: What happens if I try to delete a key that doesn't exist?
A: The Memcached delete
function will simply return false, but it won't raise an error.
Q: Can I delete multiple keys at once?
A: Yes, you can use the deleteMulti
method to delete multiple keys at once. However, keep in mind that the operation is atomic and will either delete all keys or none at all.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.