Skip to content

Commit

Permalink
Fix outdated result in type command for expired keys (#1286)
Browse files Browse the repository at this point in the history
Co-authored-by: hulk <hulk.website@gmail.com>
  • Loading branch information
jjz921024 and git-hulk committed Feb 28, 2023
1 parent 3e17a0e commit 3cea045
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/commands/cmd_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "commander.h"
#include "commands/ttl_util.h"
#include "error_constants.h"
#include "server/redis_reply.h"
#include "server/server.h"
#include "storage/redis_db.h"
#include "time_util.h"
Expand Down
7 changes: 6 additions & 1 deletion src/storage/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "parse_util.h"
#include "rocksdb/iterator.h"
#include "server/server.h"
#include "storage/redis_metadata.h"

namespace Redis {

Expand Down Expand Up @@ -441,7 +442,11 @@ rocksdb::Status Database::Type(const Slice &user_key, RedisType *type) {

Metadata metadata(kRedisNone, false);
metadata.Decode(value);
*type = metadata.Type();
if (metadata.Expired()) {
*type = kRedisNone;
} else {
*type = metadata.Type();
}
return rocksdb::Status::OK();
}

Expand Down
10 changes: 10 additions & 0 deletions tests/gocase/unit/keyspace/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ func TestKeyspace(t *testing.T) {
require.Equal(t, prefixKeys, gotKeys)
}
})

t.Run("Type a expired key", func(t *testing.T) {
expireTime := time.Second
key := "foo"
require.NoError(t, rdb.Del(ctx, key).Err())
require.Equal(t, "OK", rdb.SetEx(ctx, key, "bar", expireTime).Val())
require.Equal(t, "string", rdb.Type(ctx, key).Val())
time.Sleep(2 * expireTime)
require.Equal(t, "none", rdb.Type(ctx, key).Val())
})
}

0 comments on commit 3cea045

Please sign in to comment.