Introduction, Installation and application method of Memcached Caching system _ Server other

Source: Internet
Author: User
Tags delete key memcached rand

This article describes the introduction, installation and application methods of memcached caching system. Share to everyone for your reference, specific as follows:

A. What is memcached?

Memcached is a high-performance, distributed memory object caching system, generic into nature, but intended for use in Spee Ding up dynamic Web applications by alleviating database load.

Memcached is a high-performance, distributed memory object caching system, widely used. By caching database query results, the number of database visits is reduced to improve the speed and scalability of dynamic Web applications.

It can handle any number of connections and use non-blocking network IO. Because its working mechanism is to open up a space in memory, and then establish a hashtable,memcached self management of these hashtable. The built-in memory block allocation and hash table algorithm are also used to ensure that virtual memory does not come up for trouble.

Memcached Official website: http://www.danga.com/memcached

Two. Installation of memcached:

Note: memcached used libevent This library for socket processing, so also need to install libevent. Official website: http://www.monkey.org/~provos/libevent/

1. Install Libevent First:

[Root@localhost software]# tar zxvf libevent-1.4.11-stable.tar.gz
[root@localhost libevent-1.4.11-stable]#./ CONFIGURE–PREFIX=/USR
[root@localhost libevent-1.4.11-stable]# make
[root@localhost libevent-1.4.11-stable]# make Install

2. Test whether the Libevent is installed successfully

[Root@localhost libevent-1.4.11-stable]# Ls-al/usr/lib |  grep libevent lrwxrwxrwx 1 root 07-10 13:10 libevent-1.1a.so.1-> libevent-1.1a.so.1.0.2-rwxr-xr-x 1 root Root 31596 2007-01-07 libevent-1.1a.so.1.0.2 lrwxrwxrwx 1 root, 07-21 03:33 libevent-1.4.so.2-> libevent-  1.4.so.2.1.3-rwxr-xr-x 1 root 308088 07-21 03:33 libevent-1.4.so.2.1.3-rw-r--r--1 root root 394474 07-21 03:33  LIBEVENT.A lrwxrwxrwx 1 root 07-21 03:33 libevent_core-1.4.so.2-> libevent_core-1.4.so.2.1.3-rwxr-xr-x 1 root 109490 07-21 03:33 libevent_core-1.4.so.2.1.3-rw-r--r--1 root root 148742 07-21 03:33 Xr-xr-x 1 root 866 07-21 03:33 libevent_core.la lrwxrwxrwx 1 root root 07-21 03:33 libevent_core.so-> L ibevent_core-1.4.so.2.1.3 lrwxrwxrwx 1 root 07-21 03:33 libevent_extra-1.4.so.2->. 1.3-rwxr-xr-x 1 root root 246870 07-21 03:33 libevent_extra-1.4.so.2.1.3-rw-r--r--  1 root 307370 07-21 03:33 libevent_extra.a-rwxr-xr-x 1 root 873 07-21 03:33 libevent_extra.la-lrwxrwxrwx 1 root 07-21 03:33 libevent_extra.so-> libevent_extra-1.4.so.2.1.3-rwxr-xr-x 1 root 831 07-21 03:

 libevent.la lrwxrwxrwx 1 root 07-21 03:33 libevent.so->

Install OK.

3. Install memcached, also need to install the designated Libevent installation location

[Root@localhost software]# tar zxvf memcached-1.4.0.tar.gz
[root@localhost memcached-1.4.0]#./configure– WITH-LIBEVENT=/USR
[root@localhost memcached-1.4.0]# make
[root@localhost memcached-1.4.0]# make Intall

4. Test for successful installation of memcached

[Root@localhost memcached-1.4.0]# Ls-al/usr/local/bin | grep memcached
-rwxr-xr-x 1 root 188225 07-21 03:35 memcached

Install OK.

Three. How to start the Memcached service:

Only need to start a memcached monitoring process, the guardianship process does not require a configuration file, as long as the command line with three or four parameters can be:

Copy Code code as follows:
[Root@localhost bin]# memcached-d-M 100-u root-l 127.0.0.1-p 11211-c 256-p/tmp/memcached.pid

-D: (Run as a daemon) option is to start a daemon
-M: (max memory to using for items in megabytes (default:64 MB) is the amount of memory allocated to the memcache, in megabytes, I am 100MB,
-U: (Assume identity of <username> (just when run as root) is the user running memcache, I am here root,
-L: (interface to listen on) is the server IP address that is listening, and if there are multiple addresses, this specifies the IP address of the server 127.0.0.1,
-P: Is the port where the memcache is set up, which is set at 11211, preferably over 1024 ports,
-C: option is the maximum number of concurrent connections to run, the default is 1024, here set 256, according to the load of the server to set,
-P: (Save PID in <file> used with-d option) is to set the PID file to save Memcache, which is saved in the/tmp/memcached.pid

Note: You can also start multiple daemons, but the ports cannot be duplicated.

Four. Install Memcached PHP Extensions:

There are two ways to use memcached in PHP:

One is to install the memcached extension of PHP. This extension is written in C and is highly efficient and needs to be installed on the server.

The other is directly using the client's Php-memcached-client class library.

The following is the use of PECL memcache special extensions, because after all, is written in C, high efficiency, and installation deployment is also more convenient.

1. Select the Memcache version that you want to download in Http://pecl.php.net/package/memcache. I downloaded is: memcache-2.2.5.tgz version.

2. Install Memcache

[Root@localhost software]# tar zxvf memcache-2.2.5.tgz
[root@localhost software]# cd memcache-2.2.5
[ Root@localhost memcache-2.2.5]#/usr/bin/phpize
[root@localhost memcache-2.2.5]#./configure–enable-memcache– With-php-config=/usr/bin/php-config–with-zlib-dir
[root@localhost memcache-2.2.5]# make
[root@localhost memcache-2.2.5]# make Install

This step will have a hint like this: Installing Shared extensions:/usr/local/php/modules

3. Put the/etc/php.ini in the

Extension_dir = "./"

Modified to:

Extension_dir = "/usr/lib/php/modules"

4. and add: extension=memcache.so

You can also execute the following shell command to modify the php.ini file:

Copy Code code as follows:
Sed-i ' S#extension_dir = './' #extension_dir = '/usr/local/webserver/php/lib/php/extensions/ no-debug-non-zts-20060613/"/nextension =" memcache.so "/n# '/usr/local/webserver/php/etc/php.ini

Five. Install the C + + memcached client library: libmemcached

Download: http://download.tangent.org/libmemcached-0.32.tar.gz

1. Install libmemcached

[Root@localhost src]# tar zxvf libmemcached-0.32.tar.gz
[root@localhost src]# cd libmemcached-0.32
[ Root@localhost libmemcached-0.32]#./configure--prefix=/usr
[root@localhost libmemcached-0.32]# make && Make install

2. Check installation Results

[Root@localhost src]# ls/usr/lib/libmemcache*//library file
[root@localhost src]# ls/usr/include/libmemcached/*/header file
[root@localhost src]# ls/usr/bin/mem*//Command line tools

Six. Application:

1. Start Memcache Service

Copy Code code as follows:
[Root@localhost bin]# memcached-d-M 100-u root-l 127.0.0.1-p 11211-c 256-p/tmp/memcached.pid

2. Restart the WEB server

[root@localhost bin]# Service httpd restart

3. Create Demo test program

<?php
//connection
$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211);
Echo ' Memcache Version is: '. $mem->getversion (). ' <br/> ';
Save
The Data $mem->set (' key1 ', ' This is the ', ', ' 0, ');
$val = $mem->get (' Key1 ');
echo "Get Key1 Value:". $val. " <br/> "
//close connection
$mem->close ();
? >

4. memcached client and Tokyotyrant DB operations access

<?php
$memcache = new Memcache ();
$memcache->addserver (' 127.0.0.1:1978 ');
function Microtime_float ()
{
  list ($usec, $sec) = Explode ("", Microtime ());
  Return ((float) $usec + (float) $sec);
}
$time _start = Microtime_float ();
$data = ' abc123 ';
for ($i = 0; $i <= 20000 $i + +) {
  $key = (string) rand (1,100);
  $memcache->set ($key, $data);
for ($i = 0; $i <= 20000 $i + +) {
  $key = (string) rand (1,100);
  echo $data = $memcache->get ($key). ' -'. $i. ' <br/> ';
}
$time _end = Microtime_float ();
$time = $time _end-$time _start;
echo $time;
$memcache->close ();
? >

5. Combination of C + + and Memcached code

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <libmemcached/memcached.h
  > int main (int argc, char *argv[]) {Memcached_st *memc;
  Memcached_return RC;
  Memcached_server_st *servers;
  Char value[8191];
  Connection Server MEMC = Memcached_create (NULL);
  Servers = Memcached_server_list_append (NULL, "127.0.0.1", 11211, &AMP;RC);
  rc = Memcached_server_push (MEMC, servers);
  Memcached_server_free (servers);
  Store data strcpy (value, "This is C-value");
  rc = Memcached_set (MEMC, "Key1", 4, Value, strlen (value), (time_t) 180, (uint32_t) 0);
  if (rc = = memcached_success) {printf ("Save key:key1 data:/"%s/"success./n", value);
  }//Get data char Return_key[memcached_max_key];
  size_t Return_key_length;
  Char *return_value;
  size_t Return_value_length;
  Char *keys[]= {"Key1"};
  size_t key_length[]= {4};
  uint32_t flags;
  rc = Memcached_mget (MEMC, Keys, key_length, 1); Return_value = Memcached_fetch (MEMC, Return_key, &return_keY_length, &return_value_length, &flags, &AMP;RC);
  if (rc = = memcached_success) {printf ("Fetch key:%s data:%s/n", Return_key, Return_value);
  }//Delete data rc = Memcached_delete (MEMC, "Key1", 4, (time_t) 0);
  if (rc = = memcached_success) {printf ("Delete Key key1 success./n");
  }//Free memory Memcached_free (MEMC);
return 0;

 }

Compiling source code:

[Root@localhost html]# gcc-o cmem cmem.c-lmemcached
[root@localhost html]#./cmem//Execute
Save key:key1 data: "This is c the success.
Fetch Key:key1 Data:this is C-value
Delete key key1 success.

6. C + + and Memcached distributed binding code

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <libmemcached/memcached.h
  > int main (int argc, char *argv[]) {Memcached_st *memc;
  Memcached_return RC;
  Memcached_server_st *servers;
  Char value[8191];
  Connect multi Server MEMC = Memcached_create (NULL);
  Servers = Memcached_server_list_append (NULL, "localhost", 11211, &AMP;RC);
  Servers = memcached_server_list_append (servers, "localhost", 11212, &AMP;RC);
  rc = Memcached_server_push (MEMC, servers);
  Memcached_server_free (servers);
  Save Multi Data size_t i;
  Char *keys[]= {"Key1", "Key2", "Key3"};
  size_t key_length[]= {4, 4, 4};
  Char *values[] = {"This is C-value", "This is c second value", "This is C third value"};
  size_t val_length[]= {21, 22, 21}; For (i=0 i <3; i++) {rc = Memcached_set (MEMC, Keys[i], key_length[i], values[i], Val_length[i], (time_t) 180, (UInt32
  _t) 0); if (rc = = memcached_success) {printf ("Save key:%s data:/"%s/"success./n", Keys[i], values[i]);
  }//fetch Multi Data char Return_key[memcached_max_key];
  size_t Return_key_length;
  Char *return_value;
  size_t Return_value_length;
  uint32_t flags;
  rc = Memcached_mget (MEMC, Keys, Key_length, 3); while (Return_value = Memcached_fetch (MEMC, Return_key, &return_key_length, &return_value_length, &flags
  ) {if (rc = = memcached_success) {printf ("Fetch key:%s data:%s/n", Return_key, Return_value); }//delete Multi data for (i=0 I <3; i++) {rc = Memcached_set (MEMC, Keys[i], key_length[i], values[i], Val_
  Length[i], (time_t) 180, (uint32_t) 0);
  rc = Memcached_delete (MEMC, Keys[i], Key_length[i], (time_t) 0);
  if (rc = = memcached_success) {printf ("Delete%s success/n", Keys[i], values[i]);
  }//free Memcached_free (MEMC);
return 0;

 }

Compiling source code:

[Root@localhost html]# gcc-o cmultmem cmultmem.c-lmemcached
[root@localhost html]#./cmultmem//Execute
Save key: Key1 data: "This is C-value" success.
Save Key:key2 Data: "This is c second value" success.
Save Key:key3 Data: "This is c third value" success.
Fetch Key:key2 Data:this is c second value
fetch Key:key3 data:this are c third value
fetch Key:key1 Data:this is C The-A-value
delete key1 success
Delete key2 success
Delete Key3

The above C + + code is excerpted from: Night passers-by

7. View the Memcache process

[root@localhost html]# ps aux | grep memcached
Root   11382 0.0 0.7 55124 1896?    SSL 13:06  0:00 memcached-d-M 100-u root-l 127.0.0.1-p 11211-c 256-p
root   11395 0. 0 0.2  3912  664 pts/1  r+ 13:08 0:00  grep memcached

8. End the Memcache process

[Root@localhost html]# kill ' Cat/tmp/memcached.pid '

I hope this article will help you to memcached caching program.

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.