zoukankan      html  css  js  c++  java
  • memcached的客户端

    memcached有很多版本的Client,具体可参照:http://code.google.com/p/memcached/wiki/Clients

    本次只讨论libmemcached这个client的安装及使用

    官网:http://libmemcached.org/libMemcached.html

    libMemcached was designed to provide the greatest number of options to use Memcached. Some of the features provided:

     

    1. Bullet Asynchronous and Synchronous Transport Support.

    2. Bullet Consistent Hashing and Distribution.

    3. Bullet Tunable Hashing algorithm to match keys.

    4. Bullet Access to large object support.

    5. Bullet Local replication.

    6. Bullet A complete reference guide and documentation to the API.

    7. Bullet Tools to Manage your Memcached networks.

    0.52版本:

    http://launchpad.net/libmemcached/1.0/0.52/+download/libmemcached-0.52.tar.gz

    1、安装

    tar zxvf libmemcached-0.52.tar.gz

    cd libmemcached-0.52

    ./configure

    make

    make install

    2、测试

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

    g++ -o testmemcached testmemcached.cpp -lmemcached

    [ybt@mj247 memcached]$ ./testmemcached 

    Save data value successful!!

    Get Value value successful!!

    Delete key:keyvalue successful!!

    此时在server端上有输出:

    [root@mj247 memcached]# <28 new auto-negotiating client connection

    28: Client using the ascii protocol

    <28 set keyvalue 0 0 5

    >28 STORED

    <28 get keyvalue 

    >28 sending key keyvalue

    >28 END

    <28 delete keyvalue

    >28 DELETED

    <28 quit

    <28 connection closed.

      由于memcached支持分布式,所以在libmemcached上会根据key值做hash来得到需要存储在哪个server上。代码上memcached server分别11211及11212这两个端口来测试。

     2013-07-01更新

    1.0.17版本安装

    会报错:

    error: tr1/cinttypes: No such file or directory

    要求gcc版本比较高

    # yum install gcc44 gcc44-c++ libstdc++44-devel
    
    # export CC=/usr/bin/gcc44
    # export CXX=/usr/bin/g++44

    # ./configure --enable-tcmalloc

  • 相关阅读:
    10.30NOIP集训总结
    【JZOJ5363】【NOIP2017提高A组模拟9.14】生命之树 Trie+启发式合并
    【JZOJ5338】【NOIP2017提高A组模拟8.25】影子 点分治?/ 排序
    2017.08.13涉猎题集
    【JZOJ5233】【GDOI模拟8.5】概率博弈 树形dp+期望
    【JZOJ5231】【NOIP2017模拟A组模拟8.5】序列问题 线段树
    java8 对List<对象>获取某个属性并去重
    jquery 获取多选select的文本中并拼接成字符串
    idea 配置maven web项目
    如何做PPT
  • 原文地址:https://www.cnblogs.com/coderyoyo/p/libmemcached.html
Copyright © 2011-2022 走看看