国产探花免费观看_亚洲丰满少妇自慰呻吟_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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 禄劝| 十堰市| 蓬溪县| 吉首市| 黔江区| 襄城县| 桐城市| 伊吾县| 监利县| 隆子县| 正蓝旗| 安阳市| 尼勒克县| 增城市| 嘉义市| 凉山| 章丘市| 兴城市| 桦川县| 塘沽区| 翁牛特旗| 南和县| 余庆县| 湖口县| 通化市| 视频| 尼玛县| 临城县| 博白县| 布尔津县| 河东区| 察哈| 林周县| 仪陇县| 中山市| 焦作市| 义马市| 从化市| 万源市| 萨嘎县| 从化市|