Home  >  Article  >  Database  >  redis delete cached code

redis delete cached code

下次还敢
下次还敢Original
2024-04-20 00:00:31368browse

Redis provides the following methods to delete the cache: DEL command: delete the cache value corresponding to the specified key UNLINK command: mark the key as deleted, delete it on the next restart FLUSHALL command: delete the cache value FLUSHDB corresponding to all keys in the database Command: Delete cached values ​​corresponding to all keys in the current database

redis delete cached code

Redis Delete cached code

Redis provides There are many ways to delete the cache. Common codes are listed below:

DEL command:

<code>DEL key</code>

is used to delete the cache value corresponding to a single key.

UNLINK command:

<code>UNLINK key</code>

Similar to the DEL command, but the key will not be deleted immediately, but will be marked as deleted the next time the Redis service is restarted. will actually be deleted.

FLUSHALL command:

<code>FLUSHALL</code>

Delete the cached values ​​corresponding to all keys in the database.

FLUSHDB command:

<code>FLUSHDB</code>

Delete the cache values ​​corresponding to all keys in the current database.

Detailed description:

DEL command:

  • Delete the cache value corresponding to the specified key immediately
  • If the key does not exist, return 0, otherwise return 1

UNLINK command:

  • Mark the specified key as deleted
  • The key will not be deleted immediately. It will be deleted only when the Redis service is restarted next time.
  • If the key does not exist, it will return 0, otherwise it will return 1

FLUSHALL command:

  • Delete the cached values ​​corresponding to all keys in the database
  • Return the number of deleted keys

FLUSHDB Command:

  • Delete the cached values ​​corresponding to all keys in the current database
  • Return the number of deleted keys

Usage example:

<code>// 使用 DEL 命令删除单个键对应的缓存值
redis.del("key");

// 使用 UNLINK 命令标记键为删除状态
redis.unlink("key");

// 使用 FLUSHALL 命令删除数据库中所有键对应的缓存值
redis.flushall();

// 使用 FLUSHDB 命令删除当前数据库中所有键对应的缓存值
redis.flushdb();</code>

The above is the detailed content of redis delete cached code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn