国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++操作Redis的簡單例子

2019-11-09 13:41:36
字體:
來源:轉載
供稿:網友

相信做過服務端開發的應該都知道Redis的大名,它是一個開源的使用ANSI C語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,我們后臺是用C++開發的,問了下他們,用的緩存框架有Redis,SSDB,今天看了幾個帖子,簡單了解Redis的用法。記錄一下過程。

首先去官網下載最新的Redis源碼http://redis.io/解壓之后,進入目錄編譯

makemake testsudo make install

下載hredishttps://github.com/redis/hiredis解壓之后,同樣的

makesudo make install

進入Redis的src目錄啟動服務

#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_

創建redis.cpp

#include "redis.h"int main(){ Redis *r = new Redis(); if(!r->connect("127.0.0.1", 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; return 0;}

編寫Makefile文件

redis: redis.cpp redis.h g++ redis.cpp -o redis -L/usr/local/lib/ -lhiredisclean: rm redis.o redis

進行編譯

g++ redis.cpp -o redis -L/usr/local/lib/ -lhiredis

運行如果出現找不到動態鏈接庫

在/etc/ld.so.conf.d/目錄下新建文件usr-libs.conf,內容是:/usr/local/lib

最后執行

參考http://blog.csdn.net/achelloworld/article/details/41598389?utm_source=tuicool&utm_medium=referral

FROM:http://www.jianshu.com/p/11f4c7c71953
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 金平| 屯门区| 北辰区| 永兴县| 横峰县| 阳山县| 济南市| 巴青县| 霍州市| 南漳县| 台南市| 平陆县| 阜宁县| 崇礼县| 收藏| 东乡族自治县| 滦南县| 汉源县| 武平县| 三原县| 江陵县| 冀州市| 寻乌县| 新田县| 肃南| 邯郸市| 东山县| 会泽县| 阿克| 阿勒泰市| 新民市| 虎林市| 江川县| 贺州市| 兴国县| 勐海县| 奉贤区| 郓城县| 波密县| 班玛县| 花莲市|