memcached installation and use of libmemcached

Source: Internet
Author: User
Tags delete key

Original link: http://lontoken.com/memcached_install_and_libmemcached_uses.html

memcached Installation and Libmemcached usage environment and version

Operating system: Ubuntu14.04 32bit
Libevent version: 2.0.21
Memdatach version: v1.4.21

Libevent Installation
#wget http://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz#tar -xvzf libevent-2.0.21-stable.tar.gz#cd libevent-2.0.21-stable#./configure -prefix=/usr#make#make install

To see if the installation was successful:

#ls /usr/lib/ | grep  libevent
memcached Installation
#wget wget http://www.memcached.org/files/memcached-1.4.21.tar.gz#tar -xvzf memcached-1.4.21.tar.gz#cd memcached-1.4.21#./configure -with-libevent=/usr#make#make install

To see if the installation was successful:

#ll /usr/local/bin
memcached start
#/usr/local/bin/memcached -d -u root -m 512 127.0.0.1 -p 11211

To view the listening port and process information:

#netstat -a |grep 11211#ps -ef | grep memcached
Test memcached

The simplest way to connect memcached is through Telnet.

#telnet 127.0.0.1 11211

To view the status of Memcached (performed under Telnet):

stats

Simple setting, viewing and deletion of key values (Telnet execution):

set user_id 0 0 512345get user_iddelete user_idget user_id

PS: Exit Telnet, you can type ALT +] Q

libmemcached Installation
#wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz#tar -xvzf libmemcached-1.0.18.tar.gz#cd libmemcached-1.0.18#./configure#make#make install

To see if the libmemcached installed successfully:

#ls /usr/local/lib | grep libmemcached
Connect via libmemcached using C + + memcached#

C + + source file Libmemcachedtest.cpp

1#include <iostream>2#include <string>3#include <libmemcached/memcached.h>45UsingNamespaceStd;67IntMain(Intargc,Char*Argv[])8{9Connect server10cout<<"Test Start"<<Endl;11Memcached_st*Memc;12Memcached_returnRc;13Memcached_server_st*Server;14uint32_tFlags;1516Memc=Memcached_create(Null);17cout<<"Append Start"<<Endl;18Server=Memcached_server_list_append(Null,"LocalHost",11211,&Rc);19If(Rc!=Memcached_success){20cout<<"Memcached_server_list_append failed. Rc= "<<Rc<<Endl;21stReturn-1;22}2324Rc=Memcached_server_push(Memc,Server);25If(Rc!=Memcached_success){26cout<<"Memcached_server_push failed. Rc= "<<Rc<<Endl;27Memcached_server_free(Server);28Return-2;29};3031Memcached_server_list_free(Server);3233StringKey="Key";34StringValue="Value";35size_tValue_length=Value.Length();36size_tKey_length=Key.Length();3738Save data39cout<<"Save Data"<<Endl;40Rc=Memcached_set(Memc,Key.C_str(),Key_length,Value.C_str(),Value_length,0,Flags);41If(Rc==Memcached_success){42cout<<"Save data sucessful, key="<<Key<<", value="<<Value<<Endl;43}Else{44cout<<"Save data Faild, rc="<<Rc<<Endl;45}4647Get Data48cout<<"Get Data"<<Endl;49Char*Result=Memcached_get(Memc,Key.C_str(),Key_length,&Value_length,&Flags,&Rc);50If(Rc==Memcached_success){51cout<<"Get value sucessful, result="<<Result<<Endl;52}Else{53cout<<"Get value Faild, rc="<<Rc<<Endl;54}5556Delete data57cout<<"Delete Data"<<Endl;58Rc=Memcached_delete(Memc,Key.C_str(),Key_length,0);59If(Rc==Memcached_success){60cout<<"Delete key sucessful. Key= "<<Key<<Endl;61}else{62 cout <<  "Delete key faild, rc=" << rc << endl; }64 65 //free memcached_free (memc cout <<  "Test end." << endl; return 0;                 

The LD_LIBRARY_PATH environment changes need to be set before compiling so that libmemcached can be found.

$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

Compile and execute:

$g++ -std=c++11 -o libmemcachedtest libmemcachedtest.cpp -lmemcached$./libmemcachedtest

If all goes well, the output is as follows:

test startappend startsave datasave data sucessful, key=key,value=valueget dataget value sucessful, result=valuedelete datadelete key sucessful. key=keytest end.

The end of this article, if there are errors and questions, Welcome to Exchange (e-mail: [email protected]).

memcached installation and use of libmemcached

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.