You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

POCO-Libraries:Redis set方法的键过期时间不生效问题

Using Poco Redis's 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 a std::string)
  • value: The corresponding value to associate with the key (as a std::string)
  • overwrite: Set to true if you want to overwrite an existing key with the same name; false to skip if the key already exists
  • expireTime: The 7-day Timespan we just created
  • create: Set to true if you only want to create the key if it doesn't exist; false to 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=false and create=true.
  • Error Handling: Wrap the execute call in an additional try-catch block to handle runtime issues like connection drops mid-operation.

内容的提问来源于stack exchange,提问作者Cpp Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:59:03