Libmemcached Simple to use

Source: Internet
Author: User
Tags delete key

1. Download and install libmemcached
$ wget http://launchpad.net/libmemcached/1.0/0.44/+download/libmemcached-0.44.tar.gz
$ tar xvzf libmemcached-0.44tar.gz
$ CD libmemcached-0.44
$./configure
$ make
$ sudo make install
Libmemcached is installed by default in/usr/local/, the header file is installed in/usr/local/include/libmemcachde/, and the dynamic library is installed by default under/usr/local/lib/.
2, libmemcached simple test use

1#include<iostream>
2#include<String>
3#include<Libmemcached/Memcached.h>
4
5UsingNamespaceStd
6
7IntMainIntargcChar*Argv[])
8{
9//Connect server
10Memcached_st*Memc
11Memcached_return RC;
12Memcached_server_st*Server
13time_t expiration;
14uint32_t flags;
15
16Memc=Memcached_create (NULL);
17Server=Memcached_server_list_append (NULL,"localhost",11211,&RC);
18Rc=Memcached_server_push (Memc,server);
19Memcached_server_list_free (server);
20
21st StringKey="Key";
22StringValue="Value";
23size_t value_length=Value.length ();
24size_t key_length=Key.length ();
25
26
27//Save data
28Rc=Memcached_set (Memc,key.c_str (), Key.length (), Value.c_str (), Value.length (), expiration,flags);
29If(RC==memcached_success)
30{
31cout<<"Save Data:"<<Value<<"sucessful!"<<Endl
32}
33
34//Get data
35 Char*Result=Memcached_get (Memc,key.c_str (), Key_length,&Value_length,&Flags&RC);
36If(RC==memcached_success)
37{
38cout<<"Get Value:"<<Result<<"sucessful!"<<Endl
39}
40
41//Delete data
42Rc=Memcached_delete (Memc,key.c_str (), key_length,expiration);
43If(RC==memcached_success)
44{
45cout<<"Delete Key:"<<Key<<"sucessful!"<<Endl
46}
47 
48      // free
49 < Span style= "color: #008000;" >    memcached_free (MEMC);
50     return 0 51 
52  
53&NBSP;


Compilation: g++-O testmemcached testmemcached.cpp-lmemcached
Run:./testmemcached
Result: Save data:value sucessful!
Get Value:value sucessful!
Delete Key:key sucessful!

Libmemcached Simple to use

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.