博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
memcached的客户端
阅读量:6238 次
发布时间:2019-06-22

本文共 2702 字,大约阅读时间需要 9 分钟。

memcached有很多版本的Client,具体可参照:

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

官网:

 

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
2 #include
3 #include
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 "<
<<" successful!!"<

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

转载于:https://www.cnblogs.com/coderyoyo/archive/2011/09/23/libmemcached.html

你可能感兴趣的文章
使用jQuery和ajax代替iframe
查看>>
Keepalived + nginx实现高可用性和负载均衡
查看>>
Git整理
查看>>
12 个 CSS 高级技巧汇总
查看>>
Hibernate中 Restrictions.or()和Restrictions.disjunction()区别
查看>>
org.apache.commons.net.ftp包开发FTP客户端,实现断点续传,中文支持
查看>>
springmvc笔记--配置文件简述
查看>>
git 提交本地代码
查看>>
判断图中两个结点间是否有特定长度的路径
查看>>
Yii2.0 rules验证规则集合的详细介绍
查看>>
世界最好编程语言之父来华,约吗?
查看>>
RSA和MD5加密
查看>>
Swift 中的利刃,函数和闭包
查看>>
iOS开发之顶部状态栏statusBar颜色变化小结
查看>>
sql高级用法
查看>>
mysql安装
查看>>
Web前端开发规范(二)
查看>>
uva 621 - Secret Research
查看>>
MAC快捷键
查看>>
动态代理模式
查看>>