Command: LIST.GPOP
Overview
The LIST.GPOP
command is used to remove and return the last (most recently pushed) element from a list stored at the given key. It behaves like a POP
operation in a list, specifically removing the element at the end (LIFO order) while also returning it to the user.
Command Name
LIST.GPOP
Description
Removes and returns the last element from the list stored at the specified key.
-
If the key does not exist, it returns an empty response.
-
If the key exists but the value is not a list, an error is thrown.
-
If the list is empty, an error is thrown.
-
Preserves the list state after removing the last item.
Use Cases
-
Implementing a task stack where the most recent task needs to be popped for processing.
-
Managing temporary storage where the last inserted value must be retrieved and removed.
-
Stack-like operations in workflows, e.g., undo buffers or processing queues.
Syntax
LIST.GPOP <key>
<key>
: The identifier of the list.
Permissions
-
Read & Write access required on the specified key.
-
Only users or systems with permission to mutate list data should be allowed to execute
LIST.GPOP
.
Examples
Example 1: Basic Use
localhost:9219> LIST.PUSH myList "hello"
Ok
localhost:9219> LIST.PUSH myList "world"
Ok
localhost:9219> LIST.GPOP myList
Ok "world"
localhost:9219> LIST.GET myList
Ok [
"hello"
]
Example 2: Pop JSON Objects and Numbers
localhost:9219> LIST.PUSH myKey "string value"
Ok
localhost:9219> LIST.PUSH myKey '{"name":"Alice","city":"Delhi"}' 99.9
Ok
localhost:9219> LIST.GPOP myKey
Ok 99.9
localhost:9219> LIST.GET myKey
Ok [
"string value",
{
"name": "Alice",
"city": "Delhi"
}
]
Example 3: Pop a Boolean Value
localhost:9219> LIST.PUSH flags true false
Ok
localhost:9219> LIST.GPOP flags
Ok false
localhost:9219> LIST.GET flags
Ok [
true
]
Example 4: Pop From a List with Only One Element
localhost:9219> LIST.PUSH singleList "only"
Ok
localhost:9219> LIST.GPOP singleList
Ok "only"
localhost:9219> LIST.GET singleList
Ok []
Example 5: Pop from List with Nested JSON
localhost:9219> LIST.PUSH users '{"id":1,"data":{"name":"John","age":30}}'
Ok
localhost:9219> LIST.PUSH users '{"id":2,"data":{"name":"Jane","age":25}}'
Ok
localhost:9219> LIST.GPOP users
Ok {
"id": 2,
"data": {
"name": "Jane",
"age": 25
}
}
localhost:9219> LIST.GET users
Ok [
{
"id": 1,
"data": {
"name": "John",
"age": 30
}
}
]
Example 6: Pop Mixed-Type List (string, number, JSON)
localhost:9219> LIST.PUSH mix "text" 42 '{"x": 1}'
Ok
localhost:9219> LIST.GPOP mix
Ok {
"x": 1
}
localhost:9219> LIST.GET mix
Ok [
"text",
42
]
Example 7: Attempting to GPOP from Non-List Key
localhost:9219> SET notAList "hello"
Ok
localhost:9219> LIST.GPOP notAList
Error: InvalidValueError: the existing value for the provided key must be a list
Behavior on Error
Error Condition | Error Message Example |
---|---|
No key provided | InvalidKeyError: Key must be provided |
Key exists but value is not a list | InvalidValueError: the existing value for the provided key must be a list |
List is empty or has no elements | InvalidKeyError: index out of range [last] with length 0 |
Return Value
- The last element from the list is returned and removed from the list.
- If it’s a JSON object, it’s returned in structured format.
- If it’s a primitive (string, number), it is returned as-is.#