Redis中未删除的键

cgh8pdjw  于 2023-06-28  发布在  Redis
关注(0)|答案(1)|浏览(109)

我们在C#应用中使用StackExchange.Redis库连接到Azure中的Redis缓存,注意到我们定义了过期时间的一些密钥没有被删除,这种情况随机发生,我们仍然无法确定问题的来源,基本上我们定义了密钥和过期时间如下:

await redis.StringSetAsync(index, listJson);
await redis.KeyExpireAsync(index, DateTime.UtcNow.AddMinutes(5));

我们不知道该如何确定/解决这个问题。有人知道它可能是什么吗?我们能做些什么?

pjngdqdw

pjngdqdw1#

  • 使用Try and Catch to Trace检查代码失败的位置。为此,所以,谢谢你的评论@拉尔夫

*使用Try and catch:

关于redisManager.DeleteKey

try
        {
            bool keyDeleted = redisDatabase.KeyDelete(key);

            if (keyDeleted)
            {
                Console.WriteLine("Key deleted successfully.");
            }
            else
            {
                Console.WriteLine("Key does not exist or failed to delete.");
            }
        }
        catch (RedisConnectionException ex)
        {
            Console.WriteLine("Redis connection error: " + ex.Message);
        }
        catch (RedisException ex)
        {
            Console.WriteLine("Redis error: " + ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

public class Program
{
    public static void Main(string[] args)
    {
     
        string redisConnectionString = "your-redis-connection-string";

        RedisManager redisManager = new RedisManager(redisConnectionString);

        // Replace "your-key-name" with the actual key you want to delete
        string keyToDelete = "your-key-name";

        redisManager.DeleteKey(keyToDelete);
    }
}

输出:

相关问题