Redis简单使用(C++)

use_redis.h

#ifndef _REDIS_H_
#define _REDIS_H_

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>

#include <hiredis/hiredis.h>

class Redis
{
	public:

		Redis(){}

		~Redis()
		{
			this->_connect = NULL;
			this->_reply = NULL;                
		}

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

		std::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 set(std::string key, std::string value)
		{
			redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str());
		}

	private:

		redisContext* _connect;
		redisReply* _reply;

};

#endif  //_REDIS_H_


use_redis.cpp

#include "use_redis.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif


void showRank()
{
	redisContext * pConn = redisConnect("192.168.10.119", 6379);
	if (NULL == pConn)
	{
		printf("redisConnect err\n");
		return ;
	}

	while(1)
	{
#ifdef _WIN32
		Sleep(1000); //休眠1秒
#else //linux
		sleep(1); //休眠1秒
#endif
		redisReply *pRet = (redisReply *)redisCommand(pConn, "zrevrange players 0 5 withscores");
		if(NULL == pRet)
		{
			printf("获取前五失败\n");
			continue;
		}
		printf("======================\n");
		int i =0; 
		for(i = 0; i < pRet->elements; i++)
		{
			std::cout << pRet->element[i]->str << std::endl;
		}
		printf("======================\n");
	}
}




int main()
{
	showRank();
#if 0
	   Redis *r = new Redis();
	   if(!r->connect("192.168.10.119", 6379))
	   {
	   printf("connect error!\n");
	   return 0;
	   }
	   r->set("name", "Andy");
	   printf("Get the name is %s\n", r->get("name").c_str());
	   delete r;
#endif
	return 0;
}


Makefile

redis: *.cpp *.h
	g++ use_redis.cpp -o test -L/usr/local/lib/ -lhiredis

clean:
	rm test.o redis


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于在Windows上使用C++Redis进行交互,您可以使用第三方库hiredis来实现。以下是一些基本步骤: 1. 首先,您需要从Redis官网下载并安装Redis服务器。确保Redis服务器正在运行。 2. 下载hiredis库并将其包含到您的C++项目中。可以在GitHub上找到该库的源代码。 3. 在您的C++代码中包含hiredis头文件: ```cpp #include <hiredis/hiredis.h> ``` 4. 创建一个redisContext对象来连接Redis服务器: ```cpp redisContext* context = redisConnect("localhost", 6379); // 默认情况下Redis服务器在本地运行,端口号为6379 if (context == NULL || context->err) { if (context) { printf("Error: %s\n", context->errstr); redisFree(context); } else { printf("Cannot allocate redis context\n"); } return 1; } ``` 5. 使用redisCommand函数来执行Redis命令。例如,您可以执行SET和GET命令来设置和获取键值对: ```cpp redisReply* reply = (redisReply*)redisCommand(context, "SET key1 value1"); freeReplyObject(reply); reply = (redisReply*)redisCommand(context, "GET key1"); if (reply->type == REDIS_REPLY_STRING) { printf("GET key1: %s\n", reply->str); } freeReplyObject(reply); ``` 6. 在完成后,记得释放redisContext对象并关闭与Redis服务器的连接: ```cpp redisFree(context); ``` 这是一个简单的示例,您可以根据您的需求使用更多的Redis命令和hiredis函数来与Redis进行交互。希望对您有所帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值