summaryrefslogtreecommitdiff
path: root/src/expire.c
diff options
context:
space:
mode:
authorny0312 <49037844+ny0312@users.noreply.github.com>2021-05-29 23:20:32 -0700
committerGitHub <noreply@github.com>2021-05-30 09:20:32 +0300
commit53d1acd598b689b2bbc470d907b9e40e548d63f6 (patch)
tree5c2795c16dfced605f0eaadc7c289058ff5e5310 /src/expire.c
parent501d7755831527b4237f9ed6050ec84203934e4d (diff)
downloadredis-53d1acd598b689b2bbc470d907b9e40e548d63f6.tar.gz
Always replicate time-to-live(TTL) as absolute timestamps in milliseconds (#8474)
Till now, on replica full-sync we used to transfer absolute time for TTL, however when a command arrived (EXPIRE or EXPIREAT), we used to propagate it as is to replicas (possibly with relative time), but always translate it to EXPIREAT (absolute time) to AOF. This commit changes that and will always use absolute time for propagation. see discussion in #8433 Furthermore, we Introduce new commands: `EXPIRETIME/PEXPIRETIME` that allow extracting the absolute TTL time from a key.
Diffstat (limited to 'src/expire.c')
-rw-r--r--src/expire.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/expire.c b/src/expire.c
index 982301542..8996ae57e 100644
--- a/src/expire.c
+++ b/src/expire.c
@@ -539,6 +539,10 @@ void expireGenericCommand(client *c, long long basetime, int unit) {
} else {
setExpire(c,c->db,key,when);
addReply(c,shared.cone);
+ /* Propagate as PEXPIREAT millisecond-timestamp */
+ robj *when_obj = createStringObjectFromLongLong(when);
+ rewriteClientCommandVector(c, 3, shared.pexpireat, key, when_obj);
+ decrRefCount(when_obj);
signalModifiedKey(c,c->db,key);
notifyKeyspaceEvent(NOTIFY_GENERIC,"expire",key,c->db->id);
server.dirty++;
@@ -566,8 +570,8 @@ void pexpireatCommand(client *c) {
expireGenericCommand(c,0,UNIT_MILLISECONDS);
}
-/* Implements TTL and PTTL */
-void ttlGenericCommand(client *c, int output_ms) {
+/* Implements TTL, PTTL and EXPIRETIME */
+void ttlGenericCommand(client *c, int output_ms, int output_abs) {
long long expire, ttl = -1;
/* If the key does not exist at all, return -2 */
@@ -575,11 +579,12 @@ void ttlGenericCommand(client *c, int output_ms) {
addReplyLongLong(c,-2);
return;
}
+
/* The key exists. Return -1 if it has no expire, or the actual
* TTL value otherwise. */
expire = getExpire(c->db,c->argv[1]);
if (expire != -1) {
- ttl = expire-mstime();
+ ttl = output_abs ? expire : expire-mstime();
if (ttl < 0) ttl = 0;
}
if (ttl == -1) {
@@ -591,12 +596,22 @@ void ttlGenericCommand(client *c, int output_ms) {
/* TTL key */
void ttlCommand(client *c) {
- ttlGenericCommand(c, 0);
+ ttlGenericCommand(c, 0, 0);
}
/* PTTL key */
void pttlCommand(client *c) {
- ttlGenericCommand(c, 1);
+ ttlGenericCommand(c, 1, 0);
+}
+
+/* EXPIRETIME key */
+void expiretimeCommand(client *c) {
+ ttlGenericCommand(c, 0, 1);
+}
+
+/* PEXPIRETIME key */
+void pexpiretimeCommand(client *c) {
+ ttlGenericCommand(c, 1, 1);
}
/* PERSIST key */