Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.
Memcache is a high-performance distributed memory object caching system that can be used to store data in a variety of formats by maintaining a unified, huge hash table in memory. The simple thing is to call the data into memory and then read from memory, which greatly improves the reading speed.
working principle :
① client accesses the application for the first time, it extracts the data into the database (RDBMS), returns it to the client, and saves the extracted data to memcached.
So how does memcached's speed and efficiency manifest itself? As we all know, RDBMS is a file-type database and is ultimately saved as a file on disk. And memcached is not the same, it is a key:value relational database, is stored in memory. Then I don't have to do it again. The memory reads and writes faster than the disk reads and writes, the former is 10 of the 6 times the latter.
memcached Support Linux system, also support the Windows system, the need for a different installation package, in view of the less use of Windows, so I summed up today, in CentOS The entire process of installing memcached, on the one hand is to record, to prevent future unfamiliar, on the other hand also for just use memcached friends, provide an introductory tutorial.
Below to get to the Chase:
First of all,memcached is based on libevent, so make sure your system is installed libevent,libevent is a library, it will be Linux epoll, The BSD class operating system Kqueue and other event processing functions are encapsulated into a unified interface. The Performance of O (1) can be played even if the number of connections to the server increases. Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris and other operating systems.
since memcached and Libevent are written by C, first make sure that your system has GCC installed, if
not installed, you can install it using Yum
command:
yum-y Install gcc
yum-y Install gcc-c++
then download libevent
then upload centos, give permission, then unzip
# tar xzvf libevent-2.0.21-stable.tar.gz # #解压
# CD Libevent-2.0.21-stable
#./configure--prefix=/usr
# make
# make install
after installation, you can check if there are libevent and other files (Ls-al/usr/lib | grep libevent) under/usr/lib .
then download memcached
# tar XZVF memcached-1.4.15.tar.gz
# CD memcached-1.4.15
#./configure--with-libevent=/usr
# make
# make install
Installation Results (ls-al/usr/local/bin/memcached):
650) this.width=650; "src=" http://www.linuxidc.com/upload/2016_03/160327171291043.jpg "alt=" 160327171291043.jpg "/ >
Some parameter descriptions about the memcache start:
memcached Command parameter explanation:
-p <num> monitored ports
-L <ip_addr> connected IP address, default is native
- D start memcached service
- d Restart Restart memcached service
-D Stop|shutdown Close the running memcached service
- D install memcached service
- D Uninstall uninstall memcached service
- u <username> Run as <username> (only valid when run as root)
-M <num> maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-C <num> Maximum number of simultaneous connections, default is 1024x768
-F <factor> block size growth factor, default is 1.25
-n <bytes> Minimum allocated space, key+value+flags default is
- h Display Help
then execute the command to start memcached
#/usr/local/bin/memcached-d-M 1024-u root-p 11211-p/tmp/memcached.pid
You can then use telnet to test the connection status of the Memcache.
if the Telnet service is not installed , you can perform
Yum Install Telnet-server
Installation Services
Yum Install Telnet
installation Commands
then edit the vi/etc/xinetd.d/telnet file to activate Telnet, which is disabled by default, as follows:
650) this.width=650; "src=" http://www.linuxidc.com/upload/2016_03/160327171291042.jpg "alt=" 160327171291042.jpg "/ >
Then restart the service, execute the command service xinetd restart
then you can test the memcached connection, execute the command
telnet 127.0.0.1 11211
Input stats, will output some of the Memcache connection information, including PID
If you need to exit, execute the QUIT command, as follows:
650) this.width=650; "src=" http://www.linuxidc.com/upload/2016_03/160327171291044.jpg "alt=" 160327171291044. JPG "/>It is important to exit telnet and execute the QUIT command, as shown in the memcached process ID, if we need to close the memcached service, we can execute the command, kill-9 the process number.
Finally, we are looking at how to use the JAVA API, simple to test the next memcached, note if the memcached port number, need to open up, otherwise, there will be no connection exception, test environment we can put the firewall shutdown test:
Use the jar package as follows:
650) this.width=650; "src=" http://www.linuxidc.com/upload/2016_03/160327171291041.jpg "alt=" 160327171291041. JPG "/>test, the program core code and running results are as follows:
Package Com.test.client;import Java.net.inetsocketaddress;import net.spy.memcached.memcachedclient;/**
* @author Qindongliang
*
* Java Pop technology Group: 324714439
* Test memcached Client Connection
*
* **/public class TestClient {public static void main (string[] args) throws exception{
memcachedclient client=new memcachedclient (New inetsocketaddress ("192.168.75.130", 11211)); 60 is the timeout number, which is the default in seconds
client.set ("Test", 60, "1111 test memcache succeeded");
System.out.println (Client.get ("Test"));
Client.shutdown ();//close connection}
}
At this point, we have installed the test successfully, and finally I put memcached Java API used to upload the jar to facilitate testing.
memcached Tutorial: http://www.runoob.com/memcached/memcached-install.html
This article is from "Linux rookie" blog, please be sure to keep this source http://asura1992.blog.51cto.com/8159058/1953635
Installing memcached+memcached tutorials under CentOS