POCO-Libraries:Redis set方法的键过期时间不生效问题
set Command for Auto-Expiring Key-Value Pairs Got it, let's walk through exactly how to use Poco Redis's set command to store key-value pairs that automatically expire after 7 days. I'll break this down into clear steps with code examples that fit your use case.
Step 1: Connect to Your Redis Database
First, you need to establish a connection to your Redis instance using Poco's RedisClient. Here's how to do that with basic error handling to catch connection issues:
#include <Poco/Redis/RedisClient.h> #include <Poco/Redis/Command.h> #include <Poco/Timespan.h> #include <iostream> int main() { try { // Initialize client with your Redis host/port (adjust as needed) Poco::Redis::RedisClient client("localhost", 6379); client.connect(); if (client.isConnected()) { std::cout << "Successfully connected to Redis!" << std::endl; // We'll add the set command logic here next } } catch (const Poco::Exception& e) { std::cerr << "Redis connection failed: " << e.displayText() << std::endl; return 1; } return 0; }
Step 2: Define the 7-Day Expiration Time
Next, create a Poco::Timespan object to represent 7 days. Poco's Timespan has a convenient constructor that takes days, hours, minutes, and seconds directly:
// 7 days, 0 hours, 0 minutes, 0 seconds Poco::Timespan sevenDays(7, 0, 0, 0);
If you prefer calculating via seconds, you can do this too:
// 7 * 24 * 60 * 60 = 604800 seconds total Poco::Timespan sevenDays(Poco::Timespan::SECONDS * 604800);
Step 3: Execute the set Command with Expiration
Now let's use the set command signature you provided. Let's break down each parameter first to make sure you pick the right values:
key: The Redis key you want to store (as astd::string)value: The corresponding value to associate with the key (as astd::string)overwrite: Set totrueif you want to overwrite an existing key with the same name;falseto skip if the key already existsexpireTime: The 7-dayTimespanwe just createdcreate: Set totrueif you only want to create the key if it doesn't exist;falseto allow setting even if the key is already present
For most cases where you want to set (or update) a key and guarantee it expires in 7 days, you'll use overwrite=true and create=false. Here's the full integrated code:
// Inside the connected block from Step 1... std::string targetKey = "user:preference:456"; std::string targetValue = "dark_mode_enabled"; // Build the set command Poco::Redis::Command setCmd = Poco::Redis::Command::set( targetKey, targetValue, true, // Allow overwriting existing keys sevenDays, // Auto-expire after 7 days false // Don't restrict to only new keys ); // Run the command client.execute<void>(setCmd); std::cout << "Key stored successfully with 7-day auto-expiration!" << std::endl;
Quick Tips
- Expiration Precision: Redis handles expiration with second-level accuracy, so your key will be deleted within a second of the 7-day window.
- Safe Creation Only: If you want to avoid overwriting existing keys (only create new ones with expiration), use
overwrite=falseandcreate=true. - Error Handling: Wrap the
executecall in an additional try-catch block to handle runtime issues like connection drops mid-operation.
内容的提问来源于stack exchange,提问作者Cpp Learner

