README 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. NDB_REDIS Module
  2. Daniel-Constantin Mierla
  3. <[email protected]>
  4. Edited by
  5. Daniel-Constantin Mierla
  6. <[email protected]>
  7. Vicente Hernando
  8. <[email protected]>
  9. Copyright © 2011 asipto.com
  10. Copyright © 2012 www.systemonenoc.com
  11. __________________________________________________________________
  12. Table of Contents
  13. 1. Admin Guide
  14. 1. Overview
  15. 2. Dependencies
  16. 2.1. Kamailio Modules
  17. 2.2. External Libraries or Applications
  18. 3. Parameters
  19. 3.1. server (str)
  20. 4. Functions
  21. 4.1. redis_cmd(srvname, command, ..., replyid)
  22. 4.2. redis_free(replyid)
  23. List of Examples
  24. 1.1. Set server parameter
  25. 1.2. redis_cmd usage
  26. 1.3. redis_free usage
  27. Chapter 1. Admin Guide
  28. Table of Contents
  29. 1. Overview
  30. 2. Dependencies
  31. 2.1. Kamailio Modules
  32. 2.2. External Libraries or Applications
  33. 3. Parameters
  34. 3.1. server (str)
  35. 4. Functions
  36. 4.1. redis_cmd(srvname, command, ..., replyid)
  37. 4.2. redis_free(replyid)
  38. 1. Overview
  39. This module provides a connector to interact with REDIS NoSQL Database
  40. from configuration file. You can read more about REDIS at
  41. http://redis.io.
  42. It can connect to many REDIS servers and store the results in different
  43. containers.
  44. 2. Dependencies
  45. 2.1. Kamailio Modules
  46. 2.2. External Libraries or Applications
  47. 2.1. Kamailio Modules
  48. The following modules must be loaded before this module:
  49. * none.
  50. 2.2. External Libraries or Applications
  51. The following libraries or applications must be installed before
  52. running Kamailio with this module loaded:
  53. * hiredis - available at https://github.com/antirez/hiredis .
  54. 3. Parameters
  55. 3.1. server (str)
  56. 3.1. server (str)
  57. Specify the details to connect to REDIS server. It takes a list of
  58. attribute=value separated by semicolon, the attributes can be name,
  59. unix, addr, port and db. Name is a generic identifier to be used with
  60. module functions. unix is the path to the unix domain socket provided
  61. by redis server. addr and port are the IP address and the port to
  62. connect to REDIS server. unix and (addr, port) are mutually exclusive.
  63. If both appear in same server settings unix domain socket is
  64. configured. db is the DB number to use (defaults to 0 if not
  65. specified).
  66. You can set this parameter many times, in case you want to connect to
  67. many REDIS servers, just give different attributes and use the specific
  68. server name when querying the REDIS instance.
  69. Default value is NULL.
  70. Example 1.1. Set server parameter
  71. ...
  72. modparam("ndb_redis", "server", "name=srvN;addr=127.0.0.1;port=6379;db=1")
  73. modparam("ndb_redis", "server", "name=srvX;addr=127.0.0.2;port=6379;db=4")
  74. # Unix domain socket
  75. modparam("ndb_redis", "server", "name=srvY;unix=/tmp/redis.sock;db=3")
  76. ...
  77. 4. Functions
  78. 4.1. redis_cmd(srvname, command, ..., replyid)
  79. 4.2. redis_free(replyid)
  80. 4.1. redis_cmd(srvname, command, ..., replyid)
  81. Send a command to REDIS server identified by srvname. The reply will be
  82. stored in a local container identified by replyid. All the parameters
  83. can be strings with pseudo-variables that are evaluated at runtime.
  84. Minimum required arguments are srvname, command and replyid. Command
  85. argument can be separated into several ones using %s token. (See
  86. examples) Total number of arguments cannot exceed six.
  87. The reply can be accessed via pseudo-variable $redis(key). The key can
  88. be: type - type of the reply (as in hiredis.h); value - the value
  89. returned by REDIS server; info - in case of error from REDIS, it will
  90. contain an info message.
  91. If reply type is an array (as in hiredis.h), there are other keys
  92. available:
  93. * size - returns number of elements in the array.
  94. * type[n] - returns the type of the nth element in the array. type -
  95. returns array type.
  96. * value[n] - returns value of the nth element. value - returns null
  97. for an array. You need to get each element by index.
  98. Example 1.2. redis_cmd usage
  99. ...
  100. if(redis_cmd("srvN", "INCR cnt", "r")) {
  101. # success - the incremented value is in $redis(r=>value)
  102. xlog("===== $redis(r=>type) * $redis(r=>value)\n");
  103. }
  104. # set a value
  105. redis_cmd("srvN", "SET foo bar", "r");
  106. redis_cmd("srvN", "SET ruri $ru", "r");
  107. # get a value
  108. redis_cmd("srvN", "GET foo", "r");
  109. # same command separated into two arguments:
  110. redis_cmd("srvN", "GET %s", "foo", "r");
  111. # if we have a key with spaces within it:
  112. redis_cmd("srvN", "GET %s", "foo bar", "r");
  113. # several values substitution:
  114. redis_cmd("srvN", "HMGET %s %s %s", "key1", "field1", "field2", "r");
  115. # array example
  116. if(redis_cmd("srvN", "HMGET foo_key field1 field3", "r")) {
  117. xlog("array size: $redis(r=>size)\n");
  118. xlog("first values: $redis(r=>value[0]) , $redis(r=>value[1])\n");
  119. }
  120. ...
  121. 4.2. redis_free(replyid)
  122. Frees data in a previous reply from memory. After this function call,
  123. accessing to a freed replyid returns null value.
  124. It is not necessary to free a reply to use it again in a new redis_cmd
  125. function. When ndb_redis module closes, all pending replies are freed
  126. automatically.
  127. Example 1.3. redis_free usage
  128. ...
  129. After a redis command call:
  130. redis_cmd("srvN", "INCR cnt", "r");
  131. free reply data:
  132. redis_free("r");
  133. ...