Connecting to Memcached on the Libmemcached (C++)
Access a DCS Memcached instance using a C++ client on an ECS in the same VPC.
Prerequisites
- The DCS Memcached instance you want to access is in the Running state.
- Log in to the ECS. For details on how to create ECSs, see the Elastic Cloud Server User Guide.
An ECS can communicate with a DCS instance that belongs to the same VPC and is configured with the same security group.
- If the ECS and DCS instance are in different VPCs, establish a VPC peering connection to achieve network connectivity between the ECS and DCS instance. For details, see Does DCS Support Cross-VPC Access?
- If different security groups have been configured for the ECS and DCS instance, set security group rules to achieve network connectivity between the ECS and DCS instance. For details, see How Do I Configure a Security Group?
- GCC has been installed on the ECS. The recommended version is 4.8.4 or later.
- You have obtained the libmemcached-x.y.z.tar.gz dependency package.
x.y.z indicates the version of the dependency package. The latest version is recommended.
Connecting to Memcached on the Libmemcached (C++)
- Log in to the DCS console.
- Click in the upper left corner of the management console and select the region where your instance is located.
- In the navigation pane, choose Cache Manager.
- On the Cache Manager page, click the name of the DCS Memcached instance you want to access. Obtain the IP address or domain name and port number of the instance.
- Upload the obtained libmemcached-x.y.z.tar.gz dependency package to the created ECS.
- Log in to the ECS.
- Install related SASL dependency packages.
For OSs of Debian series: apt install libsasl2-dev cloog.ppl
For OSs of Red Hat series: yum install cyrus-sasl*
- Run the following commands to install the dependency package:
tar -xzvf libmemcached-x.y.z.tar.gz
cd libmemcached-x.y.z
./configure --enable-sasl
make
make install
- Create a file named build.sh and copy the following code to the file.
g++ -o dcs_sample dcs_sample.cpp -lmemcached -std=c++0x -lpthread -lsasl2
If the libmemcached.so.11 file cannot be found during compilation, run the find command to find the file and copy the file to the /usr/lib directory.
- Create a file named dcs_sample.cpp, copy the following C++ code to the file, and modify the code.
- Example code for the password mode
Change ip or domain name and port to the IP address or domain name and port number obtained in 4. Set userName and password respectively to the username and password of the Memcached instance.
#include <iostream> #include <string> #include <libmemcached/memcached.h> using namespace std; #define IP "ip " #define PORT "port" #define USERNAME "userName" #define PASSWORD "password" memcached_return result; memcached_st * init() { memcached_st *memcached = NULL; memcached_server_st *cache; memcached = memcached_create(NULL); cache = memcached_server_list_append(NULL, IP, PORT, &result); sasl_client_init(NULL); memcached_set_sasl_auth_data(memcached, USERNAME, PASSWORD); memcached_behavior_set(memcached,MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,1); memcached_server_push(memcached,cache); memcached_server_list_free(cache); return memcached; } int main(int argc, char *argv[]) { memcached_st *memcached=init(); string key = "memcached"; string value = "hello world!"; size_t value_length = value.length(); int expire_time = 0; uint32_t flag = 0; result = memcached_set(memcached,key.c_str(),key.length(),value.c_str(),value.length(),expire_time,flag); if (result != MEMCACHED_SUCCESS){ cout <<"set data failed: " << result << endl; return -1; } cout << "set succeed, key: " << key << ", value: " << value << endl; cout << "get key:" << key << endl; char* result = memcached_get(memcached,key.c_str(),key.length(),&value_length,&flag,&result); cout << "value:" << result << endl; memcached_free(memcached); return 0; }
- Example code for the password-free mode
Change ip and port to the IP address or domain name and port number obtained in 4.
#include <iostream> #include <string> #include <libmemcached/memcached.h> using namespace std; #define IP "ip or domain name" #define PORT port memcached_return result; memcached_st * init() { memcached_st *memcached = NULL; memcached_server_st *cache; memcached = memcached_create(NULL); cache = memcached_server_list_append(NULL, IP, PORT, &result); memcached_server_push(memcached,cache); memcached_server_list_free(cache); return memcached; } int main(int argc, char *argv[]) { memcached_st *memcached=init(); string key = "memcached"; string value = "hello world!"; size_t value_length = value.length(); int expire_time = 0; uint32_t flag = 0; result = memcached_set(memcached,key.c_str(),key.length(),value.c_str(),value.length(),expire_time,flag); if (result != MEMCACHED_SUCCESS){ cout <<"set data failed: " << result << endl; return -1; } cout << "set succeed, key: " << key << " ,value: " << value << endl; cout << "get key:" << key << endl; char* result = memcached_get(memcached,key.c_str(),key.length(),&value_length,&flag,&result); cout << "value:" << result << endl; memcached_free(memcached); return 0; }
- Example code for the password mode
- Run the following commands to compile the source code:
chmod 700 build.sh
./build.sh
The dcs_sample binary file is generated.
- Run the following command to access the chosen DCS Memcached instance:
./dcs_sample
set succeed, key: memcached ,value: hello world! get key:memcached value:hello world!
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot