Introduction to using simple functions of redis C interface hiredis

Source: Internet
Author: User

Hiredis is the C interface of redis database. Currently, it can only be used in Linux. Several Basic functions can be used to operate redis database.

 

Function prototype: rediscontext * redisconnect (const char * IP, int port)

Description: This function is used to connect to the redis database. The parameter is the IP address and port of the database. Generally, the port of the redis database is 6379.

This function returns a structure rediscontext.

 

Function prototype: void * rediscommand (rediscontext * C, const char * format ,...);

Note: This function executes commands, just like SQL statements in the SQL database, but only COMMANDS IN THE redis database. The first parameter is the rediscontext returned when the database is connected, the remaining parameters are variable parameters, just like the C standard function printf. The return value is void *, which is forcibly converted to the redisreply type for further processing.

 

Function prototype void freereplyobject (void * reply );

Note: The Memory occupied by the redisreply returned after rediscommand execution is released

 

Function prototype: void redisfree (rediscontext * C );

Note: releases the connection generated by redisconnect.

 

The following is a simple example:

 

 

[CPP]View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <stddef. h>
  4. # Include <stdarg. h>
  5. # Include <string. h>
  6. # Include <assert. h>
  7. # Include
  8. Void dotest ()
  9. {
  10. // The default listening port of redis is 6387, which can be modified in the configuration file.
  11. Rediscontext * c = redisconnect ("127.0.0.1", 6379 );
  12. If (c-> ERR)
  13. {
  14. Redisfree (C );
  15. Printf ("connect to redisserver faile \ n ");
  16. Return;
  17. }
  18. Printf ("connect to redisserver success \ n ");
  19. Const char * command1 = "set stest1 value1 ";
  20. Redisreply * r = (redisreply *) rediscommand (C, command1 );
  21. If (null = r)
  22. {
  23. Printf ("execut command1 failure \ n ");
  24. Redisfree (C );
  25. Return;
  26. }
  27. If (! (R-> type = redis_reply_status & strcasecmp (R-> STR, "OK") = 0 ))
  28. {
  29. Printf ("failed to execute command [% s] \ n", command1 );
  30. Freereplyobject (R );
  31. Redisfree (C );
  32. Return;
  33. }
  34. Freereplyobject (R );
  35. Printf ("succeed to execute command [% s] \ n", command1 );
  36. Const char * command2 = "strlen stest1 ";
  37. R = (redisreply *) rediscommand (C, command2 );
  38. If (R-> type! = Redis_reply_integer)
  39. {
  40. Printf ("failed to execute command [% s] \ n", command2 );
  41. Freereplyobject (R );
  42. Redisfree (C );
  43. Return;
  44. }
  45. Int length = r-> integer;
  46. Freereplyobject (R );
  47. Printf ("the length of 'stest1' is % d. \ n", length );
  48. Printf ("succeed to execute command [% s] \ n", command2 );
  49. Const char * command3 = "Get stest1 ";
  50. R = (redisreply *) rediscommand (C, command3 );
  51. If (R-> type! = Redis_reply_string)
  52. {
  53. Printf ("failed to execute command [% s] \ n", command3 );
  54. Freereplyobject (R );
  55. Redisfree (C );
  56. Return;
  57. }
  58. Printf ("the value of 'stest1' is % s \ n", R-> Str );
  59. Freereplyobject (R );
  60. Printf ("succeed to execute command [% s] \ n", command3 );
  61. Const char * command4 = "Get stest2 ";
  62. R = (redisreply *) rediscommand (C, command4 );
  63. If (R-> type! = Redis_reply_nil)
  64. {
  65. Printf ("failed to execute command [% s] \ n", command4 );
  66. Freereplyobject (R );
  67. Redisfree (C );
  68. Return;
  69. }
  70. Freereplyobject (R );
  71. Printf ("succeed to execute command [% s] \ n", command4 );
  72. Redisfree (C );
  73. }
  74. Int main ()
  75. {
  76. Dotest ();
  77. Return 0;
  78. }

 

The execution result is:

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.