全部产品

C

更新时间:2017-06-07 13:26:11   分享:   

客户端下载

客户端下载地址

客户端介绍

客户端版本介绍

环境配置

  1. 下载编译安装 C++ 客户端。

    https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz

  2. 执行以下命令。

    1. tar -xvf libmemcached-1.0.18.tar.gz
    2. cd libmemcached-1.0.18
    3. ./configure --enable-sasl
    4. make
    5. make install (可能需要sudo权限)

C++代码示例

  1. 下载 ocs_test.tar.gz

  2. 执行以下命令。

    1. tar -xvf ocs_test.tar.gz
    2. cd ocs_test
    3. vim ocs_test_sample1.cpp
  3. 修改 TARGET_HOST 为购买到的 Memcache 地址, USERNAME 为购买到的用户名, PASSWORD 为设置好的密码。

  4. 执行 build.sh 生成 ocs_test。运行 ./ocs_test 即可看到写入一个 key 到 Memcache 中,再从 Memcache 获取到,最后将这个 key 从 Memcache 中删除。

    ocs_test_sample1.cpp 的代码如下:

    1. #include <iostream>
    2. #include <string>
    3. #include <libmemcached/memcached.h>
    4. using namespace std;
    5. #define TARGET_HOST ""
    6. #define USERNAME ""
    7. #define PASSWORD ""
    8. int main(int argc, char *argv[])
    9. {
    10. memcached_st *memc = NULL;
    11. memcached_return rc;
    12. memcached_server_st *server;
    13. memc = memcached_create(NULL);
    14. server = memcached_server_list_append(NULL, TARGET_HOST, 11211,&rc);
    15. /* SASL */
    16. sasl_client_init(NULL);
    17. rc = memcached_set_sasl_auth_data(memc, USERNAME, PASSWORD);
    18. if(rc != MEMCACHED_SUCCESS) {
    19. cout<<"Set SASL err:"<< endl;
    20. }
    21. rc = memcached_behavior_set(memc,MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,1);
    22. if(rc != MEMCACHED_SUCCESS) {
    23. cout<<"Binary Set err:"<<endl;
    24. }
    25. /* SASL */
    26. rc = memcached_server_push(memc,server);
    27. if(rc != MEMCACHED_SUCCESS) {
    28. cout <<"Connect Mem err:"<< rc << endl;
    29. }
    30. memcached_server_list_free(server);
    31. string key = "TestKey";
    32. string value = "TestValue";
    33. size_t value_length = value.length();
    34. size_t key_length = key.length();
    35. int expiration = 0;
    36. uint32_t flags = 0;
    37. //Save data
    38. rc = memcached_set(memc,key.c_str(),key.length(),value.c_str(),value.length(),expiration,flags);
    39. if (rc != MEMCACHED_SUCCESS){
    40. cout <<"Save data failed: " << rc << endl;
    41. return -1;
    42. }
    43. cout <<"Save data succeed, key: " << key << " value: " << value << endl;
    44. cout << "Start get key:" << key << endl;
    45. char* result = memcached_get(memc,key.c_str(),key_length,&value_length,&flags,&rc);
    46. cout << "Get value:" << result << endl;
    47. //Delete data
    48. cout << "Start delete key:" << key << endl;
    49. rc = memcached_delete(memc,key.c_str(),key_length,expiration);
    50. if (rc != MEMCACHED_SUCCESS) {
    51. cout << "Delete key failed: " << rc << endl;
    52. }
    53. cout << "Delete key succeed: " << rc << endl;
    54. //free
    55. memcached_free(memc);
    56. return 0;
    57. }

下面是另一个 C++程序使用 Memcache 实例,在这里我们可以看见 Memcache 缓存与 MySQL 数据库相结合的场景。编译安装 C++客户端的步骤还是与上一个例子相同。

  1. 在 MySQL 数据库中创建示例 database 和 table。

    1. mysql -h host -uUSER -pPASSSWORD
    2. create database testdb;
    3. create table user_info (user_id int, user_name char(32) not null, password char(32) not null, is_online int, primary key(user_id) );
  2. 下载 ocs_test_2.tar.gz,并执行以下命令。

    1. tar -xvf ocs_test_2.tar.gz
    2. cd ocs_test
    3. vim ocs_test_sample2.cpp

    注意:修改 OCS_TARGET_HOST 为购买到的 Memcache 地址, OCS_USERNAME 为 Memcache 的实例名, OCS_PASSWORD 为设置好的密码;MYSQL_HOST 为 MySQL 地址, MYSQL_USERNAME 为数据库的用户名,MYSQL_PASSWORD 为数据库的密码。

  3. 执行 build.sh 生成 ocs_test ,执行 /ocs_test 即可。

    ocs_test_sample2.cpp 代码如下:

    1. #include <iostream>
    2. #include <string>
    3. #include <sstream>
    4. #include <libmemcached/memcached.h>
    5. #include <mysql/mysql.h>
    6. using namespace std;
    7. #define OCS_TARGET_HOST "xxxxxxxxxx.m.yyyyyyyyy.ocs.aliyuncs.com"
    8. #define OCS_USERNAME "your_user_name"
    9. #define OCS_PASSWORD "your_password"
    10. #define MYSQL_HOST "zzzzzzzzzz.mysql.rds.aliyuncs.com"
    11. #define MYSQL_USERNAME "db_user"
    12. #define MYSQL_PASSWORD "db_paswd"
    13. #define MYSQL_DBNAME "testdb"
    14. #define TEST_USER_ID "100"
    15. MYSQL *mysql = NULL;
    16. memcached_st *memc = NULL;
    17. memcached_return rc;
    18. int InitMysql()
    19. {
    20. mysql = mysql_init(0);
    21. if (mysql_real_connect(mysql, MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DBNAME, MYSQL_PORT, NULL, CLIENT_FOUND_ROWS) == NULL )
    22. {
    23. cout << "connect mysql failure!" << endl;
    24. return EXIT_FAILURE;
    25. }
    26. cout << "connect mysql success!" << endl;
    27. return 0;
    28. }
    29. bool InitMemcached()
    30. {
    31. memcached_server_st *server;
    32. memc = memcached_create(NULL);
    33. server = memcached_server_list_append(NULL, OCS_TARGET_HOST, 11211,&rc);
    34. /* SASL */
    35. sasl_client_init(NULL);
    36. rc = memcached_set_sasl_auth_data(memc, OCS_USERNAME, OCS_PASSWORD);
    37. if (rc != MEMCACHED_SUCCESS)
    38. {
    39. cout<<"Set SASL err:"<< endl;
    40. return false;
    41. }
    42. rc = memcached_behavior_set(memc,MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,1);
    43. if (rc != MEMCACHED_SUCCESS)
    44. {
    45. cout<<"Binary Set err:"<<endl;
    46. return false;
    47. }
    48. /* SASL */
    49. rc = memcached_server_push(memc,server);
    50. if (rc != MEMCACHED_SUCCESS)
    51. {
    52. cout <<"Connect Mem err:"<< rc << endl;
    53. return false;
    54. }
    55. memcached_server_list_free(server);
    56. return true;
    57. }
    58. struct UserInfo
    59. {
    60. int user_id;
    61. char user_name[32];
    62. char password[32];
    63. int is_online;
    64. };
    65. bool SaveToCache(string &key, string &value, int expiration)
    66. {
    67. size_t value_length = value.length();
    68. size_t key_length = key.length();
    69. uint32_t flags = 0;
    70. //Save data
    71. rc = memcached_set( memc,key.c_str(), key.length(), value.c_str(), value.length(), expiration, flags);
    72. if (rc != MEMCACHED_SUCCESS){
    73. cout <<"Save data to cache failed: " << rc << endl;
    74. return false;
    75. }
    76. cout <<"Save data to cache succeed, key: " << key << " value: " << value << endl;
    77. return true;
    78. }
    79. UserInfo *GetUserInfo(int user_id)
    80. {
    81. UserInfo *user_info = NULL;
    82. //get from cache
    83. string key;
    84. stringstream out;
    85. out << user_id;
    86. key = out.str();
    87. cout << "Start get key:" << key << endl;
    88. size_t value_length;
    89. uint32_t flags;
    90. char* result = memcached_get(memc, key.c_str(), key.size(), &value_length, &flags, &rc);
    91. if (rc != MEMCACHED_SUCCESS)
    92. {
    93. cout << "Get Cache Failed, start get from mysql."<< endl;
    94. int status;
    95. char select_sql[1024];
    96. memset(select_sql, 0x0, sizeof(select_sql));
    97. sprintf(select_sql, "select * from user_info where user_id = %d", user_id);
    98. status = mysql_query(mysql, select_sql);
    99. if (status !=0 )
    100. {
    101. cout << "query from mysql failure!" << endl;
    102. return NULL;
    103. }
    104. cout << "the status is :" << status << endl;
    105. MYSQL_RES *mysql_result = mysql_store_result(mysql);
    106. user_info = new UserInfo;
    107. MYSQL_ROW row;
    108. while (row = mysql_fetch_row(mysql_result))
    109. {
    110. user_info->user_id = atoi(row[0]);
    111. strncpy(user_info->user_name, row[1], strlen(row[1]));
    112. strncpy(user_info->password, row[2], strlen(row[2]));
    113. user_info->is_online = atoi(row[3]);
    114. }
    115. mysql_free_result(mysql_result);
    116. return user_info;
    117. }
    118. cout << "Get from cache succeed" << endl;
    119. user_info = new UserInfo;
    120. memcpy(user_info, result, value_length);
    121. return user_info;
    122. }
    123. bool DeleteCache(string &key, int expiration)
    124. {
    125. rc = memcached_delete(memc, key.c_str(), key.length(), expiration);
    126. if (rc != MEMCACHED_SUCCESS) {
    127. cout << "Delete key failed: " << rc << endl;
    128. return false;
    129. }
    130. cout << "Delete key succeed: " << rc << endl;
    131. return true;
    132. }
    133. void PrintUserInfo(UserInfo *user_info)
    134. {
    135. cout << "user_id: " << user_info->user_id << " " << " name: " << user_info->user_name << endl;
    136. }
    137. bool SaveMysql(UserInfo *user_info)
    138. {
    139. char insert_sql[1024];
    140. memset(insert_sql, 0x0, sizeof(insert_sql));
    141. sprintf(insert_sql, "insert into user_info(user_id, user_name, password, is_online) values(%d, '%s', '%s', %d)", user_info->user_id, user_info->user_name, user_info->password, user_info->is_online);
    142. int status = mysql_query(mysql, insert_sql);
    143. if (status != 0)
    144. {
    145. cout << "insert failed" << endl;
    146. return false;
    147. }
    148. cout << "insert user_info" << endl;
    149. //insert mysql
    150. return true;
    151. }
    152. int main(int argc, char *argv[])
    153. {
    154. if (InitMysql() != 0)
    155. {
    156. return -1;
    157. }
    158. if (!InitMemcached())
    159. {
    160. return -1;
    161. }
    162. //generate user_info
    163. UserInfo user_info;
    164. user_info.user_id = atoi(TEST_USER_ID);
    165. strcpy(user_info.user_name, "James");
    166. strcpy(user_info.password, "12345678");
    167. user_info.is_online = 1;
    168. //save to mysql
    169. if (!SaveMysql(&user_info))
    170. {
    171. //return -1;
    172. }
    173. string user_str;
    174. user_str.assign((char*)&user_info, sizeof(UserInfo));
    175. //save to memcached
    176. string key_str = TEST_USER_ID;
    177. SaveToCache(key_str, user_str, 10);
    178. //start get, exist in memcahced
    179. UserInfo *get_user_info = GetUserInfo(user_info.user_id);
    180. PrintUserInfo(get_user_info);
    181. //wait 10 secons
    182. sleep(2);
    183. //delete memcached or expired
    184. DeleteCache(key_str, 0);
    185. //start get, exist in mysql
    186. delete get_user_info;
    187. get_user_info = GetUserInfo(user_info.user_id);
    188. PrintUserInfo(get_user_info);
    189. delete get_user_info;
    190. //free
    191. memcached_free(memc);
    192. mysql_close(mysql);
    193. return 0;
    194. }
本文导读目录
本文导读目录
以上内容是否对您有帮助?