php5.3 installation libmemcached extension method and simple example

Source: Internet
Author: User
Tags delete key key string memcached php memcached versions

Libmemcached-based PHP memcache client has many advantages

Hash Consistent storage
Multi Get/set
• Automatically the key hash is int, avoiding the Memcache key string default less than 255Byte limit
For a long time did not configure the server, always thought that libmemcached PHP memcached has been included in the basic installation package, in the end also need to compile their own. The whole installation process a lot of dark pits, tried several times before succeeding

Installation in two steps

• Install libmemcached, target so and header files
• Install memcachedphp extensions
Libmemcaced is divided into two major versions 0.x and 1.x, 1.x version from 2011-09-28 onwards, compiling very troublesome, need gcc4.0 above the specialized configuration, compile extremely slow. 0.x version is much simpler, the highest version is 0.53, so choose Install 0.53

Acura always appear in pairs, PHP memcahed also starting from 2.1.0, request libmemcached must be 1.0.x version

libmemcaced I use 0.53, that php-memcahed choose 2.0.0. Open source software version maintenance is a lousy account.

Install libmemcached

The code is as follows Copy Code
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


Installing PHP extensions

The code is as follows Copy Code

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 is memcached.so installed to ${php install dir}/extensions/no-debug-non-zts-20090626/, which varies with PHP versions

The final step, modify PHP.ini, plus extension=memcached.so

With the face we have installed to see the simple example

The code is as follows Copy Code


#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
Ten Memcached_st *MEMC;
One Memcached_return RC;
Memcached_server_st *server;
time_t expiration;
uint32_t flags;
15
MEMC = Memcached_create (NULL);
Server = Memcached_server_list_append (NULL, "localhost", 11211,&AMP;RC);
Rc=memcached_server_push (Memc,server);
Memcached_server_list_free (server);
20
string key = "Key";
String value = "Value";
size_t value_length = Value.length ();
size_t key_length = Key.length ();
25
26
//save data
Rc=memcached_set (Memc,key.c_str (), Key.length (), Value.c_str (), Value.length (), expiration,flags);
if (rc==memcached_success)
30 {
cout<< "Save data:" <<value<< "sucessful!" <<endl;
32}
33
//get data
char* result = Memcached_get (Memc,key.c_str (), KEY_LENGTH,&AMP;VALUE_LENGTH,&AMP;FLAGS,&AMP;RC);
if (rc = = memcached_success)
37 {
cout<< "Get Value:" <<result<< "sucessful!" <<endl;
39}
40
//delete data
Rc=memcached_delete (Memc,key.c_str (), key_length,expiration);
if (rc==memcached_success)
44 {
cout<< "Delete key:" <<key<< "sucessful!" <<endl;
46}
47
//free
Memcached_free (MEMC);
0 return;
51}
52
53

Compiling: g++-o testmemcached testmemcached.cpp-lmemcached
Run:./testmemcached
Result: Save data:value sucessful!
Get Value:value sucessful!
Delete Key:key sucessful!


Attention Matters

It is important to note that libmemcached is not libmemcache, they are two different client libraries, the former is currently active and the latter has not been updated for a long time.

Memcached is a high-performance, distributed memory object caching system, which can reduce the access to the database through the memory data cache, so as to improve the speed of the dynamic Content application website. Memcached official development released, just the application of the server-side program, it released the server-side connection read-write protocol, the implementation of the client, according to the dynamic content of the application site to use the different dynamic scripts, and a variety of specific lists, can view the official website.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.