search
Homephp教程php手册php5.3安装libmemcached 扩展方法与简单实例

libmemcached 是一个 memcached 的库,客户端库,C 和 C++ 语言实现的客户端库,具有低内存占用率、线程安全、并提供对memcached功能的全面支持。它还采用 多种命令行工具: memcat , memflush , memrm , memstat ,并memslap (负载代)。程序库一直在设计,让不同的散列方法对密钥,分割的钥匙,并使用统一的散列分配

基于libmemcached的php memcache client有很多优势

•hash一致性存储
•multi get/set
•自动把key hash 为int,避开了memcache key字符串默认小于255Byte的限制
好久没有配置服务器,一直以为libmemcached的php memcached 已经纳入基本安装包了,到头来还需要自己编译.整个安装过程很多暗坑,尝试了几次才成功

安装分两步

•安装libmemcached,目标是so和header文件
•安装memcachedphp extensions
libmemcaced 分为两大版本0.x和1.x, 1.x的版本从2011-09-28开始,编译很麻烦,需要gcc4.0以上的专门配置,compile无比慢. 0.x版本就简单很多,最高版本是0.53,所以选择安装0.53

极品总是成对出现,php的memcahed也从2.1.0开始,要求libmemcached 必须是1.0.x版本

libmemcaced我用的0.53,那php-memcahed 就选择2.0.0. 开源软件的版本维护是一团烂帐

安装libmemcached

 代码如下 复制代码
    wget https://launchpad.net/libmemcached/1.0/0.53/+download/libmemcached-0.53.tar.gz
    tar xvfz libmemcached-0.53.tar.gz
    cd libmemcached-0.53
    ./configure --prefix=/opt/libmemcached
    make && make install


安装php扩展

 代码如下 复制代码

    wget http://pecl.php.net/get/memcached-2.0.0.tgz
    tar zvxf memcached-2.0.0.tgz
    cd memcached-2.0.0/
    ./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/opt/libmemcached/
    make && make install


make install 是 把memcached.so 安装到 ${php install dir}/extensions/no-debug-non-zts-20090626/,这个目录随着php的版本不同而不同

最后一步,修改php.ini,加上 extension=memcached.so

与面我们己安装好了来看看简单的实例

 代码如下 复制代码


#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;
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     string key = "key";
22     string value = "value";
23     size_t value_length = value.length();
24     size_t key_length = key.length();
25
26
27     //Save data
28     rc=memcached_set(memc,key.c_str(),key.length(),value.c_str(),value.length(),expiration,flags);
29     if(rc==MEMCACHED_SUCCESS)
30     {
31         cout 32     }
33
34     //Get data
35     char* result = memcached_get(memc,key.c_str(),key_length,&value_length,&flags,&rc);
36     if(rc == MEMCACHED_SUCCESS)
37     {
38         cout 39     }
40
41     //Delete data
42     rc=memcached_delete(memc,key.c_str(),key_length,expiration);
43     if(rc==MEMCACHED_SUCCESS)
44     {
45         cout 46     }
47
48     //free
49     memcached_free(memc);
50     return 0;
51 }
52
53

编译:g++ -o testmemcached testmemcached.cpp -lmemcached
运行:./testmemcached
结果:Save data:value sucessful!
      Get value:value sucessful!
      Delete key:key sucessful!


注意事项

需要注意的是, libmemcached 不是 libmemcache ,它们是两个不同的客户端库,前者现在的开发比较活跃,后者已经有很长时间没有更新了。

memcached 是一个高性能、分布式的内存对象缓存系统,通过内存的数据缓存来降低对数据库的访问,从而提高动态内容应用网站的速度。memcached 官方开发释放出来的,只是应用的服务器端程序,它发布了服务器端的连接读写协议,客户端的实现,则根据动态内容应用网站使用的动态脚本的不同,而有多种,具体列表,可查看官方网站。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software