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:
When the ① 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.
② second visit, because the data has been cached, no need to go to the database query, directly from the memcached fetch.
So how fast and efficient memcached is reflected in it. 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. http://blog.rekfan.com/?p=172
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 Linux Epoll, BSD class operating system Kqueue and other event processing functions 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
Then download libevent
Then upload CentOS to/usr/local, 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
Then download memcache upload
# tar XZVF memcached-1.4.15.tar.gz
# CD memcached-1.4.15
#./configure--WITH-LIBEVENT=/USR
# make
# make Install
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 1024
-F <factor> block size growth factor, default is 1.25
-N <bytes> Minimum allocated space, key+value+flags default is 48
-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, activate Telnet, the default is disabled, the screenshot is as follows:
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 and take the screenshot below:
It is important to exit Telnet, execute the QUIT command, the memcached process ID shown in the figure above, and if we need to close the memcached service, we can execute the command, kill-9 the process number.
Disable the Firewall service iptables stop before using Java code testing
Package Com.able.java;
Import java.net.InetSocketAddress;
Import net.spy.memcached.MemcachedClient;
Import Org.junit.Test;
public class Memcachetest {
@Test public
void Test () throws exception{
memcachedclient memcachedclient=new Memcachedclient (New Inetsocketaddress ("192.168.40.234", 11211));
Memcachedclient.set ("Test", 60, "The Woo-wise ferret");
Object object=memcachedclient.get ("test");
System.out.println (Object.ToString ());
}
}
2015-03-16 17:00:14.153 INFO net.spy.memcached.MemcachedConnection:Added {QA sa=/192.168.40.234:11211, #Rops =0, #Wops =0, #iq =0, Toprop=null, Topwop=null, towrite=0, interested=0} to connect queue
2015-03-16 17:00:14.153 INFO net.spy.memcached.MemcachedConnection:Connection state changed for Sun.nio.ch.selectionkeyimpl@5fdba6f9
Woo-wise Ferret