- 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远程调试设置
- 在vs2017上方菜单栏中找到[工具]->[获取工具和功能(T)...]
- 勾选[工作负荷]->[其他工具集]->[使用C++的Linux开发]并确认
- 安装完成后在vs2017上方菜单栏中找到[工具]->[选项]
-
- 找到[跨平台]->[连接管理器]
添加所要连接的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);
}
}