PHP developers often use a Redis cache to increase the performance of their applications by storing frequently accessed dataset. However, there are times when you might need to delete certain keys or even all keys from the Redis cache. This operation could be implemented during development/testing phase or when the cached data becomes stale and needs to be refreshed.
In PHP, you can use del
method of Redis class to delete a key from the Redis cache:
$redis = new Redis(); $redis->connect('localhost', 6379); $redis->del('key');
This code connects to the Redis server running on localhost at port 6379, and deletes the key named 'key'.
You can also delete multiple keys at once:
$redis = new Redis(); $redis->connect('localhost', 6379); $redis->del(['key1', 'key2', 'key3']);
This code deletes the keys 'key1', 'key2', and 'key3'.
To delete all keys from the Redis cache, you can use the flushAll
method:
$redis = new Redis(); $redis->connect('localhost', 6379); $redis->flushAll();
del
or flushAll
commands that can affect performance.No, once a key is deleted, it cannot be recovered.
del
and flushAll
methods?The del
method deletes specific keys from the Redis cache while flushAll
deletes all keys.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.