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
Copy file name to clipboardExpand all lines: docs/src/content/docs/commands/SET.md
+29-1Lines changed: 29 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ The `SET` command in DiceDB is used to set the value of a key. If the key alread
8
8
## Syntax
9
9
10
10
```bash
11
-
SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL] [NX | XX]
11
+
SET key value [NX | XX] [GET] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
12
12
```
13
13
14
14
## Parameters
@@ -24,6 +24,7 @@ SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix
24
24
|`NX`| Only set the key if it does not already exist. | None | No |
25
25
|`XX`| Only set the key if it already exists. | None | No |
26
26
|`KEEPTTL`| Retain the time-to-live associated with the key. | None | No |
27
+
|`GET`| Return the value of the key before setting it. | None | No |
27
28
28
29
## Return values
29
30
@@ -32,6 +33,8 @@ SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix
32
33
| Command is successful |`OK`|
33
34
|`NX` or `XX` conditions are not met |`nil`|
34
35
| Syntax or specified constraints are invalid | error |
36
+
| If the `GET` option is provided | The value of the key before setting it or error if value cannot be returned as a string |
37
+
35
38
36
39
## Behaviour
37
40
@@ -41,6 +44,7 @@ SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix
41
44
- Using the `EX`, `EXAT`, `PX` or `PXAT` options together with `KEEPTTL` is not allowed and will result in an error.
42
45
- When provided, `EX` sets the expiry time in seconds and `PX` sets the expiry time in milliseconds.
43
46
- The `KEEPTTL` option ensures that the key's existing TTL is retained.
47
+
- The `GET` option can be used to return the value of the key before setting it. If the key does not exist, `nil` is returned. If the key exists but does not contain a value which can be returned as a string, an error is returned. The set operation is not performed in this case.
44
48
45
49
## Errors
46
50
@@ -131,3 +135,27 @@ Trying to set key `foo` with both `EX` and `KEEPTTL` will result in an error
131
135
127.0.0.1:7379> SET foo bar EX 10 KEEPTTL
132
136
(error) ERR syntax error
133
137
```
138
+
139
+
### Set with GET option
140
+
141
+
```bash
142
+
127.0.0.1:7379>set foo bar
143
+
OK
144
+
127.0.0.1:7379>set foo bazz get
145
+
"bar"
146
+
```
147
+
### Set with GET option when key does not exist
148
+
149
+
```bash
150
+
127.0.0.1:7379>set foo bazz get
151
+
(nil)
152
+
127.0.0.1:7379> get foo
153
+
(nil)
154
+
```
155
+
156
+
### Set with Get with wrong type of value
157
+
```bash
158
+
127.0.0.1:7379> sadd foo item1
159
+
(integer) 1
160
+
127.0.0.1:7379>set foo bazz get
161
+
(error) WRONGTYPE Operation against a key holding the wrong kind of value
0 commit comments