summaryrefslogtreecommitdiff
path: root/src/t_string.c
diff options
context:
space:
mode:
authorNykolas Laurentino de Lima <nykolas.lima@gmail.com>2020-10-02 09:07:19 -0300
committerGitHub <noreply@github.com>2020-10-02 15:07:19 +0300
commit66ee45b65cc8165ba86e98192920374dcd06d943 (patch)
tree45a5709614c8205fd51d0fbdbb942dcc68a43f45 /src/t_string.c
parent3945a321779a0be52e556aa11babb3fe15883a78 (diff)
downloadredis-66ee45b65cc8165ba86e98192920374dcd06d943.tar.gz
Add GET parameter to SET command (#7852)
Add optional GET parameter to SET command in order to set a new value to a key and retrieve the old key value. With this change we can deprecate `GETSET` command and use only the SET command with the GET parameter.
Diffstat (limited to 'src/t_string.c')
-rw-r--r--src/t_string.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/t_string.c b/src/t_string.c
index 6c54a6fe9..a881b86f6 100644
--- a/src/t_string.c
+++ b/src/t_string.c
@@ -44,9 +44,9 @@ static int checkStringLength(client *c, long long size) {
/* The setGenericCommand() function implements the SET operation with different
* options and variants. This function is called in order to implement the
- * following commands: SET, SETEX, PSETEX, SETNX.
+ * following commands: SET, SETEX, PSETEX, SETNX, GETSET.
*
- * 'flags' changes the behavior of the command (NX or XX, see below).
+ * 'flags' changes the behavior of the command (NX, XX or GET, see below).
*
* 'expire' represents an expire to set in form of a Redis object as passed
* by the user. It is interpreted according to the specified 'unit'.
@@ -64,6 +64,7 @@ static int checkStringLength(client *c, long long size) {
#define OBJ_SET_EX (1<<2) /* Set if time in seconds is given */
#define OBJ_SET_PX (1<<3) /* Set if time in ms in given */
#define OBJ_SET_KEEPTTL (1<<4) /* Set and keep the ttl */
+#define OBJ_SET_GET (1<<5) /* Set if want to get key before set */
void setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire, int unit, robj *ok_reply, robj *abort_reply) {
long long milliseconds = 0; /* initialized to avoid any harmness warning */
@@ -84,16 +85,23 @@ void setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire,
addReply(c, abort_reply ? abort_reply : shared.null[c->resp]);
return;
}
+
+ if (flags & OBJ_SET_GET) {
+ getGenericCommand(c);
+ }
+
genericSetKey(c,c->db,key,val,flags & OBJ_SET_KEEPTTL,1);
server.dirty++;
if (expire) setExpire(c,c->db,key,mstime()+milliseconds);
notifyKeyspaceEvent(NOTIFY_STRING,"set",key,c->db->id);
if (expire) notifyKeyspaceEvent(NOTIFY_GENERIC,
"expire",key,c->db->id);
- addReply(c, ok_reply ? ok_reply : shared.ok);
+ if (!(flags & OBJ_SET_GET)) {
+ addReply(c, ok_reply ? ok_reply : shared.ok);
+ }
}
-/* SET key value [NX] [XX] [KEEPTTL] [EX <seconds>] [PX <milliseconds>] */
+/* SET key value [NX] [XX] [KEEPTTL] [GET] [EX <seconds>] [PX <milliseconds>] */
void setCommand(client *c) {
int j;
robj *expire = NULL;
@@ -106,7 +114,7 @@ void setCommand(client *c) {
if ((a[0] == 'n' || a[0] == 'N') &&
(a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
- !(flags & OBJ_SET_XX))
+ !(flags & OBJ_SET_XX) && !(flags & OBJ_SET_GET))
{
flags |= OBJ_SET_NX;
} else if ((a[0] == 'x' || a[0] == 'X') &&
@@ -114,6 +122,11 @@ void setCommand(client *c) {
!(flags & OBJ_SET_NX))
{
flags |= OBJ_SET_XX;
+ } else if ((a[0] == 'g' || a[0] == 'G') &&
+ (a[1] == 'e' || a[1] == 'E') &&
+ (a[2] == 't' || a[2] == 'T') && a[3] == '\0' &&
+ !(flags & OBJ_SET_NX)) {
+ flags |= OBJ_SET_GET;
} else if (!strcasecmp(c->argv[j]->ptr,"KEEPTTL") &&
!(flags & OBJ_SET_EX) && !(flags & OBJ_SET_PX))
{