Cache-presence checks
Before reading or updating cached data, determine whether a requested key is present.
Redis · Module
EXISTS determines whether one or more Redis keys exist and returns the number found. It can check multiple keys in one command.
EXISTS examines each supplied key and returns how many of those keys are present. Supplying the same existing key more than once counts that key once for every occurrence, and checking N keys has O(N) complexity.
EXISTS key [key ...]| Name | Type | Description |
|---|---|---|
key [key ...] | key names | One or more keys to check for existence. A repeated key is counted once per occurrence. |
Integer reply: the number of keys that exist among the specified arguments.
SET key1 "Hello"
EXISTS key1
EXISTS nosuchkey
SET key2 "World"
EXISTS key1 key2 nosuchkeyCreates two keys, then checks one or several keys and returns the count that currently exist.
Before reading or updating cached data, determine whether a requested key is present.
Check several candidate keys and use the returned count for conditional logic.
Verify that deletion or another lifecycle operation removed the expected keys.
EXISTS takes one or more key names and returns an integer equal to the number that exist. For a single key, that result is effectively 1 or 0.
Follow-up: What is the complexity for checking several keys? It is O(N), where N is the number of keys supplied.
The command accepts key [key ...], so a call can test multiple keys at once. Its result is the count of existing keys among the arguments.
Follow-up: What happens if one existing key is repeated? Each occurrence is counted, so repeating it can increase the result.
EXISTS is a read-only keyspace command that determines whether one or more keys exist. In clustered deployments, multi-key behavior varies, so cluster-specific multi-key rules must be considered.
Follow-up: Why can a multi-key request be problematic in a cluster? Keys may not be handled like a single local-key operation across cluster slots.
SETDELHEXISTS