Introduction installation and application of Memcached cache system
- 2020-05-15 02:58:03
- OfStack
This article illustrates the introduction, installation, and application of the Memcached caching system. I will share it with you for your reference as follows:
1. What is memcached?
memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding 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 and reducing the number of database visits, we can improve the speed and scalability of dynamic Web applications.
It can handle any number of connections, using a non-blocking network called IO. Because it works by carving out a chunk of space in memory and then creating an HashTable, Memcached manages the HashTable itself. You also use built-in block allocation and hash table algorithms to ensure that virtual memory doesn't mess around.
Memcached's official website: http: / / www danga. com/memcached
2. Installation of memcached:
Note: memcached used libevent in the library for Socket processing, so you also need to install libevent. 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 libevent is installed successfully
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
Install OK.
3. To install memcached, the installation location of libevent shall be specified in the installation
[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 whether memcached was successfully installed
[root@localhost memcached-1.4.0]# ls -al /usr/local/bin | grep memcached
-rwxr-xr-x 1 root root 188225 07-21 03:35 memcached
Install OK.
3. How to start memcached service:
All you need to do is start an memcached guardianship process. The guardianship process does not need a configuration file, just add 34 parameters to the command line:
[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 use for in in megabytes (default: 64 MB)) is the amount of memory allocated to Memcache, MB, 100MB,
-u :(assume identity of
<
username
>
(only when run as root)) is the user running Memcache, root,
-l :(interface to listen on) is the IP address of the listening server. If there are multiple addresses, the IP address of the server 127.0.0.1 is specified here.
-p: is the port where Memcache is set to listen, 11211 is set here, preferably a port above 1024,
-c: the option is the maximum number of concurrent connections to run, the default is 1024, 256 is set here, set according to the load of the server,
-P :(save PID in
<
file
>
, only used with d option) is set to save Memcache pid file, which is saved in /tmp/ memcached.pid
Note: multiple daemons can also be started, but the ports cannot be repeated.
4. Install the PHP extension of Memcached:
There are two ways to use Memcached in PHP:
One is to install the memcached extension of PHP. This extension, written in c, is efficient and needs to be installed on the server.
The other is to use the client's php-memcached-client class library directly.
Here's a special extension for Memcache that USES PECL, since it's written in C, which is, after all, efficient and easy to install and deploy.
1. In http: / / pecl php. net package/memcache memcache choose corresponding to download version. I downloaded memcache-2.2.5.tgz.
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 be like this tip: Installing shared extensions: / usr local/php/modules
3. Put /etc/ php.ini
extension_dir = "./"
Is amended as:
extension_dir = "/usr/lib/php/modules"
4. And add: extension= memcache.so
The following shell command can also be used to modify the php.ini file:
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
5. Install C/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 the installation results
[root@localhost src]# ls /usr/lib/libmemcache* // The library files
[root@localhost src]# ls /usr/include/libmemcached/* // The header file
[root@localhost src]# ls /usr/bin/mem* // Command line tool
6. Application:
1. Start memcache service
[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 the demo test program
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
0
4. Memcached client access with tokyotyrant DB operation
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
1
5. C/C++ combined with Memcached
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
2
Compile the source code:
[root@localhost html]# gcc -o cmem cmem.c -lmemcached
[root@localhost html]# ./cmem // perform
Save key:key1 data:"This is c first value" success.
Fetch key:key1 data:This is c first value
Delete Key key1 success.
6. C/C++ and Memcached distributed combination code
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
4
Compile the source code:
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
5
Above c/c++
7. View the Memcache process
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
6
8. End the Memcache process
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 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 root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root 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 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> 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 root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
7
I hope this article has helped you with your memcached cache programming.