Command: STR.GET
Overview
The STR.GET
command is used to retrieve the string value associated with a specified key. If the key does not exist or has a different data type, appropriate responses are returned. This command is safe to use for read-only operations and ensures type safety for string keys.
Command Name
STR.GET
Description
Retrieves the string value stored at the specified key. If the key exists and holds a string, the value is returned. If the key does not exist, an empty string is returned. If the key holds a non-string type (e.g., list, set, etc.), an error is returned.
Use Cases
Read from cache/store
To retrieve a value previously stored using STR.SET
, e.g., fetching user session token or a config value.
Fallback handling
To check whether a key exists or needs initialization:
if STR.GET mykey == ""
then STR.SET mykey "default_value"
Syntax
STR.GET <key>
<key>
– The key whose associated string value is to be retrieved.
Permissions
-
No special permissions required.
-
The client must have read access to the key’s shard.
Input Examples
Existing Key with String
localhost:9219> STR.SET greeting "Hello, World"
Ok
localhost:9219> STR.GET greeting
Ok "Hello, World"
Non-Existing Key
localhost:9219> STR.GET missing_key
Ok ""
Invalid Example: Missing Key Argument
localhost:9219> STR.GET
Error: ERR_INVALID_KEY: Key must be provided
Invalid Example: More Than One Argument
localhost:9219> STR.GET key1 key2
Error: ERR_INVALID_ARGS: invalid number of arguments
Invalid Example: Wrong Data Type
localhost:9219> LIST.APPEND mylist "value"
Ok
localhost:9219> STR.GET mylist
Error: ERR_INVALID_VALUE: the existing value for the provided key must be a string
Output Examples
Case | Output |
---|---|
Existing key | Ok "value" |
Non-existing key | Ok "" |
Invalid key (empty) | Error: ERR_INVALID_KEY |
Wrong type (not string) | Error: ERR_INVALID_VALUE |
Wrong arg count | Error: ERR_INVALID_ARGS |
Behavior on Error
Error Type | Cause | Message |
---|---|---|
ERR_INVALID_KEY |
No key provided or invalid characters in key | Key must be provided or key format issue |
ERR_INVALID_ARGS |
More or fewer than one argument | invalid number of arguments |
ERR_INVALID_VALUE |
Key exists but value is not of type string | the existing value must be a string |
Errors are returned with a consistent Error:
prefix and proper error codes for easier automation.
Key validation
To ensure the key points to a valid string value before performing string operations.