windows C++远程调试Linux下redis

本文介绍了如何在Linux环境下安装Redis 5.0.7,包括解压、编译与安装步骤。同时,详细阐述了在Visual Studio 2017中设置远程调试的流程,包括安装C++的Linux开发工具和配置连接管理器。此外,还展示了C++操作Redis的基本方法,如连接、释放、检查键是否存在、删除键、操作list以及字符串的插入与获取等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Linux下redis安装

所需redis安装包redis-5.0.7.tar.gz;

tar -zxvf redis-5.0.7.tar.gz                       

cd redis-5.0.7/                                         

make malloc=libc

cd src/

make install

由此redis安装工作完成。

  • vs2017远程调试设置
  1. 在vs2017上方菜单栏中找到[工具]->[获取工具和功能(T)...]
  2. 勾选[工作负荷]->[其他工具集]->[使用C++的Linux开发]并确认
  3. 安装完成后在vs2017上方菜单栏中找到[工具]->[选项]
  4.  
  5. 找到[跨平台]->[连接管理器]

添加所要连接的Linux设备(注:需要Linux支持ssh,并且支持外部访问及开放端口)

连接成功后会在[连接管理器]中看到新增的链接;

创建C++解决方案;

右键新加的解决方案,找到[属性]->[配置属性]->[常规]->[远程生成计算机]找到新添加的远程机器。

由此vs2017的准备工作完成。

  • redis库设置

 右键新加的解决方案,找到[属性]->[链接器]->[输入]->[库依赖项],增加内容    hiredis;

 将redis的头文件及库目录添加到[包含目录]及[库目录]

  • c++操作redis

连接操作    需要确认IP,端口,密码,及用的数据库索引号:

bool Redis_connect(std::string host, int port)
	{
		this->_connect = redisConnect(host.c_str(), port);
		if (this->_connect != NULL && this->_connect->err)
		{
			printf("Redis connect error: %s\n", this->_connect->errstr);
			return 0;
		}

		redisCommand(this->_connect, "auth Risen12348765");

		redisCommand(this->_connect, "select 0");

		
		return 1;
	}

 释放操作

bool Redis_free()
	{
		if (NULL != this->_reply)
		{
			freeReplyObject(this->_reply);
		}

		if (NULL != this->_connect)
			redisFree(this->_connect);
		return 1;
	}

 判断redis的keys是否存在

    //判断是否存在 >0:存在 <0:失败
	int Redis_Exist(std::string key)
	{
		this->_reply = (redisReply*)redisCommand(this->_connect, "TTL %s", key.c_str());

		return this->_reply->integer;
	}

删除keys 

 void  Redis_Delete(std::string key)
    {
        redisCommand(this->_connect, "DEL %s", key.c_str());
    } 

 list形式的左侧插入及获取

    //左侧插入最新值
	void Redis_Direct_Lists_Insert_Left(std::string key, std::string value)
	{
		redisCommand(this->_connect, "LPUSH %s %s", key.c_str(), value.c_str());
	}

    //右侧插入最新值
	void Redis_Lists_Insert_Right(std::string key, std::string value)
	{
		redisCommand(this->_connect, "RPUSH %s %s", key.c_str(), value.c_str());
	}

    //查询全部
	void Redis_Lists_ALLShow(std::string key)
	{
		this->_reply = (redisReply*)redisCommand(this->_connect, "lrange %s 0 -1", key.c_str());
		for (int i = 0; i < this->_reply->elements; i++)
		{
			std::string str = this->_reply->element[i]->str;
			printf("Redis All Show : %s\n", str.c_str());
		}
		freeReplyObject(this->_reply);
		//return NULL;
	}

	//查询列表中数目
	int32_t Redis_Lists_TotolNum(std::string key)
	{
		this->_reply = (redisReply*)redisCommand(this->_connect, "lrange %s 0 -1", key.c_str());

		return this->_reply->elements;
	}

    //根据下标查询数据
	std::string Redis_Lists_FindIndex(std::string key, std::int32_t index)
	{
		this->_reply = (redisReply*)redisCommand(this->_connect, "lrange %s %d %d", key.c_str(), index - 1, index);
		std::string str = this->_reply->element[0]->str;
		freeReplyObject(this->_reply);
		return str;
	}

  string类型的插入与获取

    //获取最新值
	std::string Redis_String_Get(std::string key)
	{
		this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key.c_str());
		std::string str = this->_reply->str;
		freeReplyObject(this->_reply);
		return str;
	}

	//设置最新值
	void Redis_String_Set(std::string key, std::string value)
	{
		redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str());
		//this->Redis_ValidTime(key.c_str(), m_iValidTime, 0);
	}

    //获取最新值然后设置他
	void Redis_String_Getset(std::string key, std::string value)
	{
		redisCommand(this->_connect, "GETSET %s %s", key.c_str(), value.c_str());
		//this->Redis_ValidTime(key.c_str(), m_iValidTime, 0);
	}

设置keys的超时清理时间

    //设置超时时间
	//Redis 有四个不同的命令可以用于设置键的生存时间(键可以存在多久)或过期时间(键什么时候会被删除) :
	//EXPlRE <key> <ttl> 将key 的生存时间设置为ttl 秒。
	//PEXPIRE <key> <ttl> 将key 的生存时间设置为ttl 毫秒。
	//EXPIREAT <key> < timestamp> 将key 的过期时间设置为timestamp秒数时间戳。
	//PEXPIREAT <key> < timestamp > 将key 的过期时间设置为timestamp毫秒数时间戳。
	void Redis_ValidTime(std::string key, std::int32_t msec, std::int32_t timestampmod/*0:TTL  1:UNIT TIME*/)
	{
		if (timestampmod == 0)
		{
			redisCommand(this->_connect, "PEXPIRE  %s %d", key.c_str(), msec);
		}
		else
		{
			redisCommand(this->_connect, "PEXPIREAT  %s %d", key.c_str(), msec);
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南来丶北往

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值