Php5.3 how to install libmemcached extension and simple instance _ PHP Tutorial

Source: Internet
Author: User
Php5.3 install libmemcached extension method and simple instance. Libmemcached is a memcached Library, client library, and client library implemented in C and C ++ languages. it has low memory usage, thread security, and provides comprehensive support for memcached functions. Libmemcached is a memcached Library, client library, and client library implemented in C and C ++ languages. it has low memory usage, thread security, and provides comprehensive support for memcached functions. It also uses a variety of command line tools: memcat, memflush, memrm, memstat, and memslap (load generation ). The library has been designed to enable different hash methods to pair keys, split keys, and use uniform hash allocation.

Php memcache client based on libmemcached has many advantages

• Hash consistent storage
• Multi get/set
• The key is automatically hashed to int, avoiding the limit that the memcache key string is less than 255Byte by default.
I haven't configured the server for a long time. I always thought that libmemcached's php memcached has been included in the basic installation package. In the end, I still need to compile it myself. The entire installation process is a lot of dark holes and I tried it several times before it was successful.

Two steps for installation

• Install libmemcached with the goal of the so and header files
• Install memcachedphp extensions
Libmemcaced is divided into two major versions: 0. x and 1.x, 1. the version of x starts from, and compilation is very troublesome. it requires special configuration of gcc4.0 or above, and compile is extremely slow. 0. version x is much simpler. The maximum version is 0.53. Therefore, install version 0.53.

Php memcahed starts from 2.1.0, and libmemcached must be 1.0.x.

Libmemcaced I used 0.53, so php-memcahed chose 2.0.0. version maintenance of open-source software is a mess.

Install libmemcached

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


Install php extension

The code is as follows:

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

In the last step, modify php. ini and add extension = memcached. so.

We have installed a simple instance.

The code is as follows:


# 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 <"Save data:" < 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 <"Get value:" < 39}
40
41 // Delete data
42 rc = memcached_delete (memc, key. c_str (), key_length, expiration );
43 if (rc = MEMCACHED_SUCCESS)
44 {
45 cout <"Delete key:" < 46}
47
48 // free
49 memcached_free (memc );
50 return 0;
51}
52
53

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


Notes

It should be noted that libmemcached is not libmemcache, and they are two different client libraries. The former is currently active in development, and the latter has not been updated for a long time.

Memcached is a high-performance, distributed memory object cache system that uses the memory data cache to reduce access to the database, thus improving the speed of dynamic content application websites. Officially developed and released by memcached, it is only the server-side program of the application. it releases the server-side connection read/write protocol. the implementation of the client depends on the dynamic scripts used by the dynamic content application website, there are a variety of, specific list, you can view the official website.

Http://www.bkjia.com/PHPjc/629821.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/629821.htmlTechArticlelibmemcached is a memcached Library, client Library, C and C ++ language client library, it has low memory usage and thread security, and provides comprehensive support for memcached functions ....

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.