redis数据库 C++封装

  • Post author:
  • Post category:其他




前言

Redis是一种内存数据库,常用做Cache缓存使用,内置常用的五种数据结构:

  • String
  • List
  • Hashmap
  • Set
  • Zset



Linux下安装redis

在 Ubuntu 系统安装 Redis 可以使用以下命令:

$sudo apt-get update
$sudo apt-get install redis-server

启动 Redis

$ redis-server

查看 redis 是否启动?

$ redis-cli --raw

raw的作用是让中文能够正确显示出来。

以上命令将打开以下终端:

redis 127.0.0.1:6379>

127.0.0.1 是本机 IP ,6379 是 redis 服务端口。现在我们输入 PING 命令。

redis 127.0.0.1:6379> ping

PONG

以上说明我们已经成功安装了redis。



redis基本数据类型

通过这一片文章:

https://www.runoob.com/redis/redis-data-types.html


可以了解到redis的基本数据类型。



redis进行C++封装

C++连接redis数据库需要使用

hiredis

库,可以从github上面直接下载。

git clone https://github.com/redis/hiredis
cd hiredis
make 
sudo make install(复制生成的库到/usr/local/lib目录下)
sudo ldconfig /usr/local/lib

安装好动态链接库之后,只需要加上

#include <hiredis/hiredis.h>

头文件;

并且在编译时加上

-lhiredis

即可连接到redis库。

下面是可以对Redis进行CRUD操作的C++源码:


github

:

https://github.com/Worthy-Wang/MyRedis

可以先看一下头文件,与Redis数据库实际操作完美契合。


#ifndef C_MY_REDIS_H
#define C_MY_REDIS_H
#include <string.h>
#include <hiredis/hiredis.h>
#include "C_Error.h"
using std::string;
class CMyRedis
{
public:
	CMyRedis();
	~CMyRedis();
	void Connect(const string &strIp, int nPort); //连接操作
	bool CheckConenct();

	/***************************String 类型*********************************/
	void Set(const string &strKey, const string &strValue);
	string Get(const string &strKey);
	int Del(const string &strKey);

	/***************************List 类型*********************************/
	int Push(const string &strListKey, const string &strValue, int nFlag = 0);
	string Pop(const string &strListKey, int nFlag = 0);
	string Lindex(const string &strListKey, int nIndex);
	void Lrange(const string &strListKey, int nStart, int nEnd, string strArray[],
				int &nCount);

	/***************************Set 类型*********************************/
	int Sadd(const string &strSetKey, const string &strItem);
	int Srem(const string &strSetKey, const string &strItem);
	bool SisMember(const string &strSetKey, const string &strItem);
	void Smembers(const string &strSetKey, string strArray[], int &nCount);

	/***************************哈希表 类型*********************************/
	int HSet(const string &strHashKey, const string &key, const string &value);
	string HGet(const string &strHashKey, const string &key);
	int HDel(const string &strHashKey, const string &key);
	void HGetAll(const string &strHashKey, string strArray[], int &nCount);

	/***************************Sorted Set 类型*********************************/
	int ZAdd(const string &strZSetKey, int nScore, const string &value);
	void ZRange(const string &strZSetKey, int nStartIndex, int nEndIndex,
				string strArray[], int &nCount);
	void ZRangeByScore(const string &strZSetKey, int nStartScore,
					   int nEndScore, string strArray[], int &nCount);
	int ZRem(const string &strZSetKey, const string &value);

private:
	redisContext *m_pRedisContext;
	redisReply *m_pRedisReply;
	string m_strIp;
	int m_nPort;
};
#endif // C_MY_REDIS_H



版权声明:本文为Worthy_Wang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。