Redis学习小计(2) - c++ client

5 篇文章 0 订阅

Redis server运行起来后(如何启动server,请参考上一篇 ”安装运行server“),可以使用自带的client - “redis-cli” 连接server。
为了后续的扩展,用c++写了个demo来连接server,使用的是hiredis.h里面的接口

CRedis::start - 与server建立连接
CRedis::set - 添加[key, value]
CRedis::get - 获取指定的数据,可以是整数或字符串

class CRedis
{
public:
	CRedis(const std::string& ip, unsigned int port)
		: redis_(NULL), serverIp_(ip), serverPort_(port)
	{
		std::cout << "server ip is: " << serverIp_ << std::endl;
		std::cout << "server port is: " << serverPort_ << std::endl;
	}

	~CRedis()
	{
		if (redis_)
		{
			redisFree(redis_);
			redis_ = NULL;
		}
	}

	bool start()
	{
		redis_ = redisConnect(serverIp_.c_str(), serverPort_);

		if (!redis_)
		{
			std::cout << "connect redis server is failed" << std::endl;
			return false;
		}

		if (redis_->err)
		{
			std::cout << "connect redis server is failed with: " << redis_->errstr << std::endl;
			redisFree(redis_);
			redis_ = NULL;
			return false;
		}

		std::cout << "connect redis server is successful" << std::endl;
		return true;
	}

	void set(const std::string& command)
	{
		redisReply* reply = sendCommand(command);
		if (!reply)
		{
			std::cout << "execute command[" << command << "] is failed" << std::endl;
			return;
		}

		if (!(reply->type == REDIS_REPLY_STATUS and strcasecmp(reply->str, "OK") == 0))
		{
			std::cout << "execute command[" << command << "] is failed with " << reply->str << std::endl;
			freeReplyObject(reply);
			return;
		}

		freeReplyObject(reply);
		std::cout << "execute command[" << command << "] is successful" << std::endl;
	}

	bool get(const std::string& command, long long& out)
	{
		redisReply* reply = sendCommand(command);
		if (!reply)
		{
			std::cout << "execute command[" << command << "] is failed" << std::endl;
			return false;
		}

		if (reply->type != REDIS_REPLY_INTEGER)
		{
			std::cout << "execute command[" << command << "] is failed due to invalid type[" << reply->type << "]" << std::endl;
			freeReplyObject(reply);
			return false;
		}

		out = reply->integer;
		freeReplyObject(reply);
		return true;
	}

	bool get(const std::string& command, std::string& out)
	{
		redisReply* reply = sendCommand(command);
		if (!reply)
		{
			std::cout << "execute command[" << command << "] is failed" << std::endl;
			return false;
		}

		if (reply->type != REDIS_REPLY_STRING)
		{
			std::cout << "execute command[" << command << "] is failed due to invalid type[" << reply->type << "]" << std::endl;
			freeReplyObject(reply);
			return false;
		}

		out = reply->str;
		freeReplyObject(reply);
		return true;
	}

private:
	inline redisReply* sendCommand(const std::string& command) const
	{
		return (redisReply*)redisCommand(redis_, command.c_str());
	}

private:
	redisContext* redis_;
	std::string serverIp_;
	unsigned int serverPort_;
};

外面创建实例,调用一些简单的命令

	CRedis redis("127.0.0.1", 6379);
	if (redis.start())
	{
		redis.set("set key1 abcdefg");
		redis.set("set key2 xyz");

		long long len1, len2;
		redis.get("strlen key1", len1);
		redis.get("strlen key2", len2);
		std::cout << "key1 length is " << len1 << std::endl;
		std::cout << "key2 length is " << len2 << std::endl;

		std::string v1, v2;
		redis.get("get key1", v1);
		redis.get("get key2", v2);
		std::cout << "key1 value is " << v1 << std::endl;
		std::cout << "key2 value is " << v2 << std::endl;
	}

输出
在这里插入图片描述


上一篇:Redis学习小计(1) - 安装运行server
下一篇:Redis学习小计(3) - redisObject

Redis C++ Client 是一个开源的 Redis C++ 客户端库,使用 Boost 和 hiredis 库实现。它支持同步和异步操作,并提供了简单易用的接口,方便地与 Redis 进行交互。 以下是 Redis C++ Client 的一些特点: - 支持同步和异步操作 - 使用 Boost 库实现,具有跨平台性 - 使用 hiredis 库实现 Redis 协议解析和序列化 - 提供简单易用的接口,方便与 Redis 进行交互 - 支持多个 Redis 实例的连接和操作 - 支持 Redis 事务和管道操作 以下是 Redis C++ Client 的一个简单例子: ```c++ #include <iostream> #include <redisclient/redisasyncclient.h> int main() { redisclient::RedisAsyncClient client("127.0.0.1", 6379); client.connect([](boost::system::error_code ec) { if (ec) { std::cerr << "Connect error: " << ec.message() << std::endl; return; } std::cout << "Connected to Redis server" << std::endl; client.set("name", "Alice", [](boost::system::error_code ec) { if (ec) { std::cerr << "Set error: " << ec.message() << std::endl; return; } std::cout << "Set name to Alice" << std::endl; client.get("name", [](const redisclient::RedisValue& value) { if (value.isOk()) { std::cout << "Name: " << value.toString() << std::endl; } else { std::cerr << "Get error: " << value.toString() << std::endl; } }); }); }); client.run(); return 0; } ``` 这个例子中使用了 `redisasyncclient` 库,需要在编译时链接 `redisclient`、`boost_system`、`boost_thread` 和 `hiredis` 库。在 Ubuntu 上,可以使用以下命令编译: ``` g++ -o example example.cpp -I/path/to/redisclient/include -I/path/to/boost -L/path/to/redisclient/lib -L/path/to/hiredis/lib -lredisclient -lhiredis -lboost_system -lboost_thread ``` 希望这个例子可以帮助你快速了解 Redis C++ Client 的基本用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值