Skip to content

Commit ff44b08

Browse files
authored
DiceDB#824 Update GETSET doc (DiceDB#848)
1 parent 0b46f4f commit ff44b08

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

docs/src/content/docs/commands/GETSET.md

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,49 @@ GETSET key value
1313

1414
## Parameters
1515

16-
- `key`: The key whose value you want to retrieve and set. This must be a string.
17-
- `value`: The new value to set for the specified key. This must be a string.
16+
| Parameter | Description | Type | Required |
17+
|-----------------|--------------------------------------------------|---------|----------|
18+
| `key` | The key whose value you want to retrieve and set. | String | Yes |
19+
| `value` | The new value to set for the specified key. | String | Yes |
1820

19-
## Return Value
2021

21-
The `GETSET` command returns the old value stored at the specified key before the new value was set. If the key did not exist, it returns `nil`.
22+
23+
## Return values
24+
25+
| Condition | Return Value |
26+
|------------------------------------------------|---------------------------------------------------|
27+
| The old value stored at the specifiied `key` | A string value |
28+
| The key does not exist | `nil` |
29+
2230

2331
## Behaviour
2432

2533
When the `GETSET` command is executed, the following sequence of actions occurs:
2634

2735
1. The current value of the specified key is retrieved.
2836
2. The specified key is updated with the new value.
29-
3. The old value is returned to the client.
37+
3. If the specified key had an existing `TTL` , it is reset.
38+
4. The old value is returned to the client.
39+
3040

3141
This operation is atomic, meaning that no other commands can be executed on the key between the get and set operations.
3242

33-
## Error Handling
43+
## Errors
3444

3545
The `GETSET` command can raise errors in the following scenarios:
3646

3747
1. `Wrong Type Error`: If the key exists but is not a string, DiceDB will return an error.
38-
- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
48+
- Error Message: `(error) ERROR WRONGTYPE Operation against a key holding the wrong kind of value`
3949
2. `Syntax Error`: If the command is not provided with exactly two arguments (key and value), DiceDB will return a syntax error.
40-
- Error Message: `(error) ERR wrong number of arguments for 'getset' command`
50+
- Error Message: `(error) ERROR wrong number of arguments for 'getset' command`
4151

42-
## Example Usage
52+
## Examples
4353

4454
### Basic Example
4555

4656
```DiceDB
47-
SET mykey "Hello"
48-
GETSET mykey "World"
49-
```
50-
51-
`Output:`
52-
53-
```
57+
127.0.0.1:7379> SET mykey "Hello"
58+
127.0.0.1:7379> GETSET mykey "World"
5459
"Hello"
5560
```
5661

@@ -63,12 +68,7 @@ GETSET mykey "World"
6368
### Example with Non-Existent Key
6469

6570
```DiceDB
66-
GETSET newkey "NewValue"
67-
```
68-
69-
`Output:`
70-
71-
```
71+
127.0.0.1:7379> GETSET newkey "NewValue"
7272
(nil)
7373
```
7474

@@ -78,17 +78,34 @@ GETSET newkey "NewValue"
7878
- The `GETSET` command sets the value of `newkey` to "NewValue".
7979
- Since the key did not exist before, `nil` is returned.
8080

81-
### Error Example: Wrong Type
8281

82+
### Example with Key having pre-existing TTL
8383
```DiceDB
84-
LPUSH mylist "item"
85-
GETSET mylist "NewValue"
84+
127.0.0.1:7379> SET newkey "test"
85+
OK
86+
127.0.0.1:7379> EXPIRE newkey 60
87+
1
88+
127.0.0.1:7379> TTL newkey
89+
55
90+
127.0.0.1:7379> GETSET newkey "new value"
91+
"test"
92+
127.0.0.1:7379> TTL newkey
93+
(integer) -1
8694
```
8795

88-
`Output:`
96+
`Explanation:`
8997

90-
```
91-
(error) WRONGTYPE Operation against a key holding the wrong kind of value
98+
- The `newkey` used in the `GETSET` command had an existing `TTL` set to expire in 60 seconds
99+
- When `GETSET` is executed on the mentioned key, it updates the value and resets the `TTL` on the key.
100+
- Hence, the `TTL` on `newkey` post `GETSET` returns `-1` , suggesting that the key exists without any `TTL` configured
101+
102+
103+
### Error Example: Wrong Type
104+
105+
```DiceDB
106+
127.0.0.1:7379> LPUSH mylist "item"
107+
127.0.0.1:7379> GETSET mylist "NewValue"
108+
(error) ERROR WRONGTYPE Operation against a key holding the wrong kind of value
92109
```
93110

94111
`Explanation:`
@@ -99,25 +116,12 @@ GETSET mylist "NewValue"
99116
### Error Example: Syntax Error
100117

101118
```DiceDB
102-
GETSET mykey
103-
```
104-
105-
`Output:`
106-
107-
```
108-
(error) ERR wrong number of arguments for 'getset' command
119+
127.0.0.1:7379> GETSET mykey
120+
(error) ERROR wrong number of arguments for 'getset' command
109121
```
110122

111123
`Explanation:`
112124

113125
- The `GETSET` command requires exactly two arguments: a key and a value.
114126
- Since only one argument is provided, DiceDB returns a syntax error.
115127

116-
## Best Practices
117-
118-
- Ensure that the key you are operating on is of type string to avoid `WRONGTYPE` errors.
119-
- Use `GETSET` when you need to update a value and also need to know the previous value in a single atomic operation.
120-
- Handle the `nil` return value appropriately in your application logic, especially when dealing with non-existent keys.
121-
122-
By following this documentation, you should be able to effectively use the `GETSET` command in DiceDB to manage key-value pairs with atomic get-and-set operations.
123-

0 commit comments

Comments
 (0)