EXISTS
Checks if one or more keys exist.
Syntax
EXISTS key [key ...]Description
The EXISTS command checks if the specified key(s) exist in Echo DB. It returns the number of keys that exist. Keys that have expired are considered non-existent.
Arguments
key(required, one or more): The name(s) of the key(s) to check.
Return Value
Returns an Integer: The number of keys that exist (between 0 and the number of keys specified).
Examples
Checking a Single Key
SET mykey "value"
EXISTS mykeyResponse:
OK
(integer) 1Checking a Non-Existent Key
EXISTS nonexistentResponse:
(integer) 0Checking Multiple Keys
SET key1 "value1"
SET key2 "value2"
EXISTS key1 key2 key3Response:
OK
OK
(integer) 2Checking Expired Keys
SET mykey "value"
EXPIRE mykey 1
# Wait 1 second
EXISTS mykeyResponse:
OK
(integer) 1
(integer) 0Notes
- Returns
1if the key exists,0if it doesn't - When checking multiple keys, returns the count of existing keys
- Expired keys are considered non-existent
- Works with both string and list keys
Use Cases
- Conditional Operations: Check if a key exists before performing operations
- Validation: Verify that required keys are present
- Monitoring: Count how many keys of a certain pattern exist
- Cache Checks: Verify if cached data exists before fetching