libmemcached的安装及測试

Posted mfmdaoyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libmemcached的安装及測试相关的知识,希望对你有一定的参考价值。

1、安装memcached

~$ wget http://memcached.googlecode.com/files/memcached-1.2.8.tar.gz.

 $ tar xvzf lmemcached-1.2.8.tar.gz
 $ cd lmemcached-1.2.8
 $ ./configure
 $ make
 $ sudo make install

启动server(分配512兆内存,监听11211port):

./memcached -m 512 -p 11211 -vv

2、下载安装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 默认安装在/usr/local/,头文件安装在/usr/local/include/libmemcachde/,动态库默认安装在/usr/local/lib/下。


3、libmemcached简单測试使用

#include <iostream>
#include <string>
#include <libmemcached/memcached.h>

using namespace std;

int main(int argc, char *argv[])
{
    //connect server
    memcached_st *memc;
    memcached_return rc;
    memcached_server_st *server;
    time_t expiration = 0;
    uint32_t  flags = 0;

    memc = memcached_create(NULL);
    server = memcached_server_list_append(NULL, "localhost", 11211, &rc);
    rc = memcached_server_push(memc, server);
    memcached_server_list_free(server);

    string key = "key";
    string value = "value";
    size_t value_length = value.length();
    size_t key_length = key.length();


    //Save data
    rc = memcached_set(memc, key.c_str(), key.length(), value.c_str(), value.length(), expiration, flags);
    if (rc == MEMCACHED_SUCCESS)
    {
        cout << "Save data:" << value << " sucessful!" << endl;
    }

    //Get data
    char* result = memcached_get(memc, key.c_str(), key_length, &value_length, &flags, &rc);
    if (rc == MEMCACHED_SUCCESS)
    {
        cout << "Get value:" << result << " sucessful!" << endl;
    }

    //Delete data
    rc = memcached_delete(memc, key.c_str(), key_length, expiration);
    if (rc == MEMCACHED_SUCCESS)
    {
        cout << "Delete key:" << key << " sucessful!" << endl;
    }

    //free
    memcached_free(memc);
    return 0;
}

















以上是关于libmemcached的安装及測试的主要内容,如果未能解决你的问题,请参考以下文章

Kafka集群部署及測试

模式识别 - 特征归一化 及 測试 代码(Matlab)

基于webrtc的媒体库測试代码以及接口介绍

基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有測试代码(何志雄)

如何安装php memcache的拓展

编译libmemcached