I’m stuck on stage 7
I’ve implemented the expiry function
Here are my logs:
redis-cli SET apple pear px 100
remote: [stage-7] Sent bytes: "*5\r\n$3\r\nSET\r\n$5\r\napple\r\n$4\r\npear\r\n$2\r\npx\r\n$3\r\n100\r\n"
remote: [your_program] Logs from your program will appear here!
remote: [your_program] Waiting for a client to connect...
remote: [your_program] Client connected
remote: [your_program] Received: *5
remote: [your_program] $3
remote: [your_program] SET
remote: [your_program] $5
remote: [your_program] apple
remote: [your_program] $4
remote: [your_program] pear
remote: [your_program] $2
remote: [your_program] px
remote: [your_program] $3
remote: [your_program] 100
remote: [your_program]
remote: [stage-7] Received bytes: "$2\r\nOK\r\n"
remote: [stage-7] Received RESP value: "OK"
remote: [stage-7] Received "OK"
remote: [stage-7] Received OK at 07:02:11.208
remote: [stage-7] Fetching key "apple" at 07:02:11.208 (should not be expired)
remote: [stage-7] $ redis-cli GET apple
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
remote: [your_program] Received: *2
remote: [your_program] $3
remote: [your_program] GET
remote: [your_program] $5
remote: [your_program] apple
remote: [your_program] $4
remote: [your_program] pear
remote: [your_program] $2
remote: [your_program] px
remote: [your_program] $3
remote: [your_program] 100
remote: [your_program]
remote: [stage-7] Received bytes: "$4\r\npear\r\n"
remote: [stage-7] Received RESP value: "pear"
remote: [stage-7] Received "pear"
remote: [stage-7] Sleeping for 101ms
remote: [stage-7] Fetching key "apple" at 07:02:11.310 (should be expired)
remote: [stage-7] $ redis-cli GET apple
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
remote: [your_program] Received: *2
remote: [your_program] $3
remote: [your_program] GET
remote: [your_program] $5
remote: [your_program] apple
remote: [stage-7] Received bytes: "$4\r\npear\r\n"
remote: [stage-7] Received RESP value: "pear"
remote: [your_program] $4
remote: [your_program] pear
remote: [your_program] $2
remote: [your_program] px
remote: [stage-7] Expected null bulk string ("$-1\r\n"), got BULK_STRING
remote: [stage-7] Test failed
remote: [stage-7] Terminating program
remote: [your_program] $3
remote: [your_program] 100
remote: [your_program]
remote: [stage-7] Program terminated successfully
And here’s a snippet of my code:
else if( resp[0]=="SET"){
in_map[resp[1]]=resp[2];
string response=encode("OK");
if(resp.size()>3){
if(resp[3]=="PX"){
int expiry=stoi(resp[4]);
std::chrono::time_point<std::chrono::system_clock> strt = std::chrono::system_clock::now();
std::chrono::milliseconds duration(expiry);
std::chrono::time_point<std::chrono::system_clock> expiration_time = strt + std::chrono::milliseconds(expiry);
expiry_map[resp[1]]= expiration_time;
}
}
send(client_sock, response.c_str(), response.length(), 0);
}
else if (resp[0] == "GET") {
string response = "";
std::chrono::time_point<std::chrono::system_clock> strt = std::chrono::system_clock::now();
string key = resp[1];
if (expiry_map.count(key) != 0 && expiry_map[key] <= strt) {
in_map.erase(in_map.find(key));
expiry_map.erase(expiry_map.find(key));
} else if (in_map.count(resp[1]) != 0) {
response = in_map[key];
}
response = encode(response);
send(client_sock, response.c_str(), response.length(), 0);
}
}