C + + Redis-client

Source: Internet
Author: User
Tags redis cluster redis server

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:

  1. tar -zxvf redis-2.8.7.tar.gz
  2. cd redis-2.8.7
  3. sudo apt-get install tcl(redis测试程序需要tcl版本至少为8.5)
  4. make 32bit(64位系统直接使用make即可)
  5. sudo make install(将编译生成的可执行文件拷贝到/usr/local/bin目录下)
  6. 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:

    1. git clone https://github.com/redis/hiredis
    2. cd hiredis
    3. make
    4. sudo make install(复制生成的库到/usr/local/lib目录下)
    5. 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:

  1. #include
  2. #include <iostream>
  3. #include <string>
  4. int main(int argc, char **argv)
  5. {
  6. struct timeval timeout = {2, 0}; //2s的超时时间
  7. //redisContext是Redis操作对象
  8. rediscontext * prediscontext = (rediscontext* ) redisconnectwithtimeout ( "127.0.0.1" , 6379, Timeout
  9. if ( (null == prediscontext) | | (prediscontext ->err) ) /span>
  10. {
  11. if (pRedisContext)
  12. {
  13. std::cout "Connect error:" < Span class= "pun" ><< prediscontext->errstr << std:: endl
  14. }
  15. else
  16. {
  17. std::cout << "connect error: can‘t allocate redis context." << std::endl;
  18. }
  19. return -1;
  20. }
  21. //redisReply是Redis命令回复对象 redis返回的信息保存在redisReply对象中
  22. redisreply predisreply = (redisreply *) rediscommand (prediscontext, "INFO" ); //Execute info command
  23. std ::cout < Span class= "pun" ><< predisreply->str < < std:: endl
  24. //当多条Redis命令使用同一个redisReply对象时
  25. //每一次执行完Redis命令后需要清空redisReply 以免对下一次的Redis操作造成影响
  26. freeReplyObject(pRedisReply);
  27. return 0;
  28. }

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
      1. Http://redis.io/:Redis official website
      2. Http://redis.cn/:Redis Chinese website
      3. http://try.redis.io/: Redis Online Experience
      4. Https://github.com/antirez/redis:Redis Development Version Source code
      5. Http://www.redisdoc.com/en/latest/:Redis Command Reference
      6. Http://blog.nosqlfan.com/topics/redis:Redis Series Articles
      7. Http://redisbook.readthedocs.org/en/latest/:Redis Design and implementation
      8. Https://github.com/huangz1990/annotated_redis_source: Annotated version of Redis source code

C + + Redis-client

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.