Skip to content

Commit

Permalink
add expired_keys statistic if the expiration time is already expired
Browse files Browse the repository at this point in the history
Signed-off-by: Ray Cao <[email protected]>
  • Loading branch information
RayaCoo committed Jan 7, 2025
1 parent c0014ef commit aafe746
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,8 @@ void deleteExpiredKeyFromOverwriteAndPropagate(client *c, robj *keyobj) {
robj *aux = server.lazyfree_lazy_expire ? shared.unlink : shared.del;
rewriteClientCommandVector(c, 2, aux, keyobj);
signalModifiedKey(c, c->db, keyobj);
notifyKeyspaceEvent(NOTIFY_GENERIC, "del", keyobj, c->db->id);
notifyKeyspaceEvent(NOTIFY_EXPIRED, "expired", keyobj, c->db->id);
server.stat_expiredkeys++;
}

/* Propagate an implicit key deletion into replicas and the AOF file.
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/expire.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ start_server {tags {"expire"}} {
list $e $f
} {somevalue {}}

test {EXPIRE / EXPIREAT / PEXPIRE / PEXPIREAT Expiration time is already expired} {
set prev_expired [s expired_keys]
r del x

r set x somevalue
r expire x -1
assert_equal {0} [r exists x]
assert_equal {1} [expr {[s expired_keys] - $prev_expired}]

r set x somevalue
r expireat x [expr [clock seconds] - 1]
assert_equal {0} [r exists x]
assert_equal {2} [expr {[s expired_keys] - $prev_expired}]

r set x somevalue
r pexpire x -1
assert_equal {0} [r exists x]
assert_equal {3} [expr {[s expired_keys] - $prev_expired}]

r set x somevalue
r pexpireat x [expr [clock milliseconds] - 1]
assert_equal {0} [r exists x]
assert_equal {4} [expr {[s expired_keys] - $prev_expired}]
}

test {TTL returns time to live in seconds} {
r del x
r setex x 10 somevalue
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/pubsub.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ start_server {tags {"pubsub network"}} {
$rd1 close
}

test "Keyspace notification: expired event (Expiration time is already expired)" {
r config set notify-keyspace-events Ex
r del foo
set rd1 [valkey_deferring_client]
assert_equal {1} [psubscribe $rd1 *]
r set foo 1
r expire foo 0
assert_equal "pmessage * __keyevent@${db}__:expired foo" [$rd1 read]
$rd1 close
}

test "Keyspace notifications: evicted events" {
r config set notify-keyspace-events Ee
r config set maxmemory-policy allkeys-lru
Expand Down

0 comments on commit aafe746

Please sign in to comment.