http://hahaya.github.io/operator-redis-under-linux/#0-tsina-1-10809-397232819ff9a47a7b7e80a40613cfe1
C + + operation Redis under Linux
Hahaya
Date: 2014-03-12
Introduced
The full name of Redis is remote dictonary server, a high-performance Key-value storage System written by Salvatore Sanfilippo, and Redis has the following advantages:
1. High performance-Redis can support more than 100k+ per second read and write frequency
2. Rich data Types-Redis supports data types such as strings, Lists, hashes, sets, and ordered sets
3. atomicity-All operations of Redis are atomic, while Redis also supports the atomic operation after merging several operations
4. Rich Features-Redis also supports features such as publish/subscribe, transactions, key expiration, and more
Installing Redis
Open the Redis official website, go to the download page, choose a suitable version of their own computer to download, download tickets http://redis.io/download, download the completion of the decompression, compilation, installation, in turn, in order to execute the following command at the terminal:
tar -zxvf redis-2.8.7.tar.gz
cd redis-2.8.7
sudo apt-get install tcl(redis测试程序需要tcl版本至少为8.5)
make 32bit(64位系统直接使用make即可)
sudo make install(将编译生成的可执行文件拷贝到/usr/local/bin目录下)
make test(用于确认安装正确与否)
The resulting executable file is compiled:
1. Redis-server Redis Server
2. REDIS-CLI Redis Client
3. Redis-benchmark Redis Performance Testing Tool
4. redis-check-aof aof File Repair Tool
5. redis-check-dump Rdb File Check tool
6. Redis-sentinel Redis Cluster Management tool
After compiling and installing, enter the Redis server in the terminal in redis-server
the simplest way, and then enter in the other terminal redis-cli
to connect to the Redis server, then you can try various commands. Try.redis.io Preview the various commands under Redis, and also view Redis-supported commands on the Redis website.
Installing Hiredis
If you need to operate Redis in C/s + +, you will need to install the C + + Redis Client Library, where I use Hiredis, which is the official library, and uses more people, and then executes the following commands to download and install them in sequence under terminal:
git clone https://github.com/redis/hiredis
cd hiredis
make
sudo make install(复制生成的库到/usr/local/lib目录下)
sudo ldconfig /usr/local/lib
A/C + + operations Redis
After all the preparation has been done, next test how to use the C + + operations Redis, the code is as follows:
#include
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
struct timeval timeout = {2, 0}; //2s的超时时间
//redisContext是Redis操作对象
rediscontext * prediscontext = (rediscontext* ) redisconnectwithtimeout ( "127.0.0.1" , 6379, Timeout
if ( (null == prediscontext) | | (prediscontext ->err) ) /span>
{
if (pRedisContext)
{
std::cout "Connect error:" < Span class= "pun" ><< prediscontext->errstr << std:: endl
}
else
{
std::cout << "connect error: can‘t allocate redis context." << std::endl;
}
return -1;
}
//redisReply是Redis命令回复对象 redis返回的信息保存在redisReply对象中
redisreply predisreply = (redisreply *) rediscommand (prediscontext, "INFO" ); //Execute info command
std ::cout < Span class= "pun" ><< predisreply->str < < std:: endl
//当多条Redis命令使用同一个redisReply对象时
//每一次执行完Redis命令后需要清空redisReply 以免对下一次的Redis操作造成影响
freeReplyObject(pRedisReply);
return 0;
}
Save exit, execute g++ OperatorRedis.cpp -o OperatorRedis -lhiredis
to compile, run the program after compiling ./OperatorRedis
(you need to start the Redis server before running the program, otherwise you will get connect error:Connection refused
such a mistake), no unexpected words will see the output Redis servers information ~
OK, C + + operations Redis First here, I have encapsulated a C + + operation Redis class, and so on will be released ...
Redis Learning Links
- Http://redis.io/:Redis official website
- Http://redis.cn/:Redis Chinese website
- http://try.redis.io/: Redis Online Experience
- Https://github.com/antirez/redis:Redis Development Version Source code
- Http://www.redisdoc.com/en/latest/:Redis Command Reference
- Http://blog.nosqlfan.com/topics/redis:Redis Series Articles
- Http://redisbook.readthedocs.org/en/latest/:Redis Design and implementation
- Https://github.com/huangz1990/annotated_redis_source: Annotated version of Redis source code
C + + Redis-client