欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ubuntu16.04环境下libmemcached客户端安装

程序员文章站 2022-07-14 12:38:36
...

一. 安装

1. 下载libmemcached,当前最新版本:libmemcached-1.0.18.tar.gz,官网:https://launchpad.net/libmemcached/

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz

2. 解压libmemcached-1.0.18.tar.gz

tar -xvf libmemcached-1.0.18.tar.gz

3. 进入目录libmemcached-1.0.18

cd libmemcached-1.0.18

4. 配置、编译、安装

./configure
make
sudo make install

二. 测试Demo

2.1 分别执行如下命令,启动3个memcached服务器端

memcached -p 11211 -m 64M -u root -v
memcached -p 11212 -m 64M -u root -v
memcached -p 11213 -m 64M -u root -v

 

2.2 客户端测试测代码:memcached_client.c

 

#include <libmemcached/memcached.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define TOTAL_NUM	10000

int main(int argc, char **argv){

	//配置
	memcached_st *memc = NULL;
	memcached_return rc = 0;
	memcached_server_st *servers;		//服务器列表
	
	memc = memcached_create(NULL);
	
	servers = memcached_server_list_append(NULL, (char *)"127.0.0.1",11211, &rc);
	servers = memcached_server_list_append(servers, (char *)"127.0.0.1",11212, &rc);
	servers = memcached_server_list_append(servers, (char *)"127.0.0.1",11213, &rc);
	
	rc = memcached_server_push(memc, servers);
	
	memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
	
	//写入
	char key[16];
	size_t key_len = 0;
	int i =0;
	for(i = 0; i < TOTAL_NUM; i++){
		key_len = snprintf(key, 16, "key%d", i);
		//key-value
		rc = memcached_set(memc, key, key_len, key, key_len, (time_t)1800, (uint32_t)0);		
	
		if(rc == MEMCACHED_SUCCESS){
			static int no;
			if((no++ % 1000) == 0) printf("key:%s value:%s\n", key, key);			
		}else{
			printf("set key(%s) failed, reason:%s\n", key, memcached_strerror(memc, rc));	
		}
	}
	
	//读取
	char *result_str = NULL;
	size_t result_str_len = 0;
	uint32_t flags;
	
	for(i = 0; i < TOTAL_NUM; i++){
		key_len = snprintf(key, 16, "key%d", i);
		result_str = memcached_get(memc, key, key_len, &result_str_len, &flags, &rc);
		
		if(rc == MEMCACHED_SUCCESS){
			static int num;
			if((num++ % 1000) == 0) printf("key:%s value:%s\n", key, result_str);		
		}else{
			fprintf(stderr, "get key %s failed.\n", key);
		}
	}
	
	memcached_free(memc);
	
	return 0;
}

 

2.3 编译

 

gcc memcached_client.c -o memcached_client -lmemcached

2.4 运行

./memcached_client

2.5 结果输出,测试成功。

key:key0 value:key0
key:key1000 value:key1000
key:key2000 value:key2000
key:key3000 value:key3000
key:key4000 value:key4000
key:key5000 value:key5000
key:key6000 value:key6000
key:key7000 value:key7000
key:key8000 value:key8000
key:key9000 value:key9000
key:key0 value:key0
key:key1000 value:key1000
key:key2000 value:key2000
key:key3000 value:key3000
key:key4000 value:key4000
key:key5000 value:key5000
key:key6000 value:key6000
key:key7000 value:key7000
key:key8000 value:key8000
key:key9000 value:key9000

三. 安装及运行问题解决

3.1 error while loading shared libraries: libmemcached.so.11: cannot open shared object file: No such file or directory

解决方法:libmemcached.so.11在目录/usr/local/lib下面,因为程序运行过程不会检索/usr/local/lib目录下的so文件,执行如下命令,将/usr/local/lib/libmemcached.so.11 链接到 /usr/lib/libmemcached.so.11

ln -s /usr/local/lib/libmemcached.so.11 /usr/lib/libmemcached.so.11