memcached Installation and Configuration

Source: Internet
Author: User
Tags cas memcached

? memcached is a project of danga.com. It is an open-source, high-performance distributed memory object caching system. The earliest is to provide services to LiveJournal. It was gradually used by more and more large sites. Used to reduce access to the database in the application. Increase your app's access speed. and reduce the load on the database.

In order to provide fast lookup capabilities for data in memory, memcached stores and access data in the form of Key-value. Maintain a huge hashtable in memory. This reduces the time complexity of data queries to O (1), guaranteeing high performance access to data. Memory space is always limited, when memory does not have a lot of other space to store the new data, memcached will use the LRU algorithm to eliminate the recent infrequently visited data to make room for the new data.

The data formats supported by Memcached storage are also flexible and diverse. Through the serialization mechanism of the object. Can convert higher-level abstract objects into binary data, stored in the cache server, the current end of the application needs, but also can be deserialized through binary content, the data is restored to the original object.

Because memcached uses libevent for efficient network connection processing, you need to install libevent before installing memcached.
All the installation packages can be downloaded here.

Installing Libevent
Version number libevent-2.0.21-stable.tar.gz (if placed under the/root folder)

[root @zzh  ~]# mkdir libevent  [Root @zzh  ~]# TAR-ZVXF libevent-2.0.21-stable.tar.gz  [Root @zzh  ~] # CD libevent-2.0.21-stable  [Root @zzh  libevent-2.0 . 21 -stable]#./configure--prefix=/root/libevent  [Root @zzh  libevent-2.0 . 21 -stable]# make  [Root @zzh  libevent-2.0 . 21 -stable]# make install   

Test Libevent Whether the installation was successful (no certainty.) For reference only)

[Email protected] libevent-2.0.-stable]# Ls-al/root/libevent/lib | grep libeventlrwxrwxrwx.1Root root + 4Month5  A: tolibevent-2.0. so. 5libevent-2.0. so. 5. 1. 9-rwxr-xr-x.1Root root968722 4Month5  A: tolibevent-2.0. so. 5. 1. 9-rw-r--r--.1Root root1571586 4Month5  A: toLibevent. Alrwxrwxrwx.1Root root - 4Month5  A: tolibevent_core-2.0. so. 5libevent_core-2.0. so. 5. 1. 9-rwxr-xr-x.1Root root585265 4Month5  A: tolibevent_core-2.0. so. 5. 1. 9-rw-r--r--.1Root root978314 4Month5  A: toLibevent_core. A-rwxr-xr-x.1Root root980 4Month5  A: toLibevent_core. Lalrwxrwxrwx.1Root root - 4Month5  A: toLibevent_core. solibevent_core-2.0. so. 5. 1. 9lrwxrwxrwx.1Root root - 4Month5  A: tolibevent_extra-2.0. so. 5libevent_extra-2.0. so. 5. 1. 9-rwxr-xr-x.1Root root404884 4Month5  A: tolibevent_extra-2.0. so. 5. 1. 9-rw-r--r--.1Root root593344 4Month5  A: toLibevent_extra. A-rwxr-xr-x.1Root root987 4Month5  A: toLibevent_extra. Lalrwxrwxrwx.1Root root - 4Month5  A: toLibevent_extra. solibevent_extra-2.0. so. 5. 1. 9-rwxr-xr-x.1Root root945 4Month5  A: toLibevent. Lalrwxrwxrwx.1Root root - 4Month5  A: tolibevent_pthreads-2.0. so. 5libevent_pthreads-2.0. so. 5. 1. 9-rwxr-xr-x.1Root root18462 4Month5  A: tolibevent_pthreads-2.0. so. 5. 1. 9-rw-r--r--.1Root root18662 4Month5  A: toLibevent_pthreads. A-rwxr-xr-x.1Root root1008 4Month5  A: toLibevent_pthreads. Lalrwxrwxrwx.1Root root - 4Month5  A: toLibevent_pthreads. solibevent_pthreads-2.0. so. 5. 1. 9lrwxrwxrwx.1Root root + 4Month5  A: toLibevent. solibevent-2.0. so. 5. 1. 9

Installing memcached

[root@zzh ~]# mkdir memcached[root@zzh ~]# tar -zvxf memcached-1.4.7.tar.gz[root@zzh ~]# cd memcached-1.4.7/[root@zzh memcached-1.4.7]# ./configure --prefix=/root/memcached --with-libevent=/root/libevent[root@zzh memcached-1.4.7]# make[root@zzh memcached-1.4.7]# make install

Start memcached

[root@zzh ~]# cd memcached/bin/[root@zzh bin]# memcached -d -m 10 -u root -l 10.10.195.112 -p 11211 -c 32 -P /tmp/memcached.pid

Description of references:
-D: Start a daemon,
-M: The amount of memory allocated to Memcache. Unit is MB, default is 64MB,
-u: User executing memcache
-L: Listening server IP address
-P: Sets the port on which the memcache listens. Default is 11211 Note:-P (p is lowercase)
-C: Set the maximum number of concurrent connections, default is 1024
-P: Set the PID file to save Memcache:-P (P is uppercase)
-F: Block size growth factor. Default is 1.25
-N: Minimum allocation space. Key+value+flags default is 48
-H: Display Help

View the memcached process

[[email protected] bin]# Ps-aux | grep memcachedWarning:bad syntax, perhaps a bogus'-'? see/usr/share/doc/procps-3.2.8/faqroot13904  0.0  0.0 326708   912? Ssl A: -   0:xx./memcached- D-MTen-U Root- L 10.10.195.112-P11211-C +-p/tmp/memcached.pidroot13912  0.0  0.0 103248   852pts/2S+ A: -   0:xxgrep memcached

Close the memcached process (using the Kill PID command)

[root@zzh bin]# kill `cat /tmp/memcached.pid`

Or

[root@zzh bin]# kill 13904

Connection memcached

telnet127.0.0.111211

Memcache has a number of frequently used commands: Set, add, Replace,append, Preappend, CAs, get, incr, DECR, delete, etc., to be able to access relevant documents.

Javaclient and Memcached
? memcached's official Memcached-java-client tool includes the Java package for the Memcached protocol, which makes it easier to communicate with the ring-city server.
? case

Importcom. Danga. MemCached. Memcachedclient;Importcom. Danga. MemCached. Sockiopool;Importcom. Schooner. MemCached. Memcacheditem;public class memcachedtest{public static void Init () {String servers[] = {"10.10.195.112:11211"};Sockiopool pool = Sockiopool. getinstance();Pool. Setservers(servers);Pool. Setfailover(true);Pool. Setinitconn(Ten);Pool. Setminconn(5);Pool. Setmaxconn( -);Pool. Setmaintsleep( -);Pool. Setnagle(false);Pool. Setsocketto( the);Pool. Setalivecheck(true);Pool. Sethashingalg(Sockiopool. Consistent_hash);Pool. Initialize();} public static void Main (string[] args) {init ();Memcachedclient mem = new Memcachedclient ();Mem. Add("UserName","Zzzzh");Mem. prepend("UserName","Hello");Mem. Append("UserName","!");System. out. println(Mem. Get("UserName"));Mem. Set("Key",1);System. out. println(Mem. Get("Key"));Memcacheditem item = Mem. Gets("Key");Mem. CAs("Key", (Integer) item. GetValue()+1, item. Getcasunique());System. out. println(Mem. Get("Key"));}}

Output Result:

hellozzzzh!12

References
1. "Large distributed site architecture design and practice" Chen Kang

memcached Installation and Configuration

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.