You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| The old value stored at the specifiied `key`| A string value |
28
+
| The key does not exist |`nil`|
29
+
22
30
23
31
## Behaviour
24
32
25
33
When the `GETSET` command is executed, the following sequence of actions occurs:
26
34
27
35
1. The current value of the specified key is retrieved.
28
36
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
+
30
40
31
41
This operation is atomic, meaning that no other commands can be executed on the key between the get and set operations.
32
42
33
-
## Error Handling
43
+
## Errors
34
44
35
45
The `GETSET` command can raise errors in the following scenarios:
36
46
37
47
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`
39
49
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`
41
51
42
-
## Example Usage
52
+
## Examples
43
53
44
54
### Basic Example
45
55
46
56
```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"
54
59
"Hello"
55
60
```
56
61
@@ -63,12 +68,7 @@ GETSET mykey "World"
63
68
### Example with Non-Existent Key
64
69
65
70
```DiceDB
66
-
GETSET newkey "NewValue"
67
-
```
68
-
69
-
`Output:`
70
-
71
-
```
71
+
127.0.0.1:7379> GETSET newkey "NewValue"
72
72
(nil)
73
73
```
74
74
@@ -78,17 +78,34 @@ GETSET newkey "NewValue"
78
78
- The `GETSET` command sets the value of `newkey` to "NewValue".
79
79
- Since the key did not exist before, `nil` is returned.
80
80
81
-
### Error Example: Wrong Type
82
81
82
+
### Example with Key having pre-existing TTL
83
83
```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
86
94
```
87
95
88
-
`Output:`
96
+
`Explanation:`
89
97
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
92
109
```
93
110
94
111
`Explanation:`
@@ -99,25 +116,12 @@ GETSET mylist "NewValue"
99
116
### Error Example: Syntax Error
100
117
101
118
```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
109
121
```
110
122
111
123
`Explanation:`
112
124
113
125
- The `GETSET` command requires exactly two arguments: a key and a value.
114
126
- Since only one argument is provided, DiceDB returns a syntax error.
115
127
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.
0 commit comments