Linux Distribution:ubuntu 14
libevent:1.4.14b
Download Address: Libevent official website
memcached:1.4.25
Download Address: memcached official website
One, install memcached
Memcached installation relies on libevent, first install Libevent:
sean@sean:~$ TAR-XZF libevent-1.4.14b-stable.tar.gz
sean@sean:~$ cd libevent-1.4.14b-stable/
libevent-1.4.14b-stable$ sudo./configure-prefix/usr
sean@sean:~/libevent-1.4.14b-stable$ sudo
make sean@sean:~/libevent-1.4.14b-stable$ sudo make install
sean@sean:~/libevent-1.4.14b-stable$ sudo
Libevent will be installed under/usr/lib
sean@sean:/usr/lib$ cd/usr/lib/
sean@sean:/usr/lib$ ll | grep libevent*
Binary file libevent-1.4.so.2.2.0 Matches
libevent.la:dlname= ' libevent-1.4.so.2 '
libevent.la:library_names= ' libevent-1.4.so.2.2.0 Libevent-1.4.so.2 libevent.so '
Binary file libevent.so matches
Then install memcached:
sean@sean:~$ TAR-XZF Memcached-1.4.25.tar.tar
sean@sean:~$ cd memcached-1.4.25/
memcached-1.4.25$ sudo./configure-with-libevent=/usr
sean@sean:~/memcached-1.4.25$ sudo make
sean@sean:~/ memcached-1.4.25$ sudo make install
sean@sean:~/memcached-1.4.25$ sudo
Memcached will be installed under/usr/local/bin:
sean@sean:/usr/local/bin$ ll Total
544
drwxr-xr-x 2 root 4096 April 21:32./
Drwxr-xr-x root 4096 February 18 07:12. /
-rwxr-xr-x 1 root 545998 April 21:32 memcached*
Two, start memcached service
-M: Maximum memory allocated to memcached, in M
-P: The TCP port on which the service listens, the default value is 11211
-D: Service started as daemon
-VV: Print more information
-F: Growth factor, the default value is 1.25, the specified value must be greater than 1
sean@sean:/usr/local/bin$./memcached-m 64-p 9999-d-vv sean@sean:/usr/local/bin$ Slab class 1:chunk size 96
Perslab 10922 Slab class 2:chunk size-Perslab 8738 Slab class 3:chunk size 152 Perslab 6898 Slab class 4:chunk size Perslab 5461 Slab class 5:chunk size, Perslab 4369 Class 6: Chunk size 304 Perslab 3449 Slab class 7:chunk size 384 perslab 2730 Slab class 8:chunk size Perslab 2184 Slab class 9:chunk size Perslab 1747 slab class 10:chunk size 752 Perslab 1394 Slab class 11:chunk size 944 Perslab 1110 Slab class 12:chunk size 1184 Perslab 885 Slab class 13:chunk size 1480 Perslab 708 Slab class 14:chunk size 1856 Perslab 564 Slab class 15:chunk size 2320 Perslab 451 Slab class 16:chunk size 2904 Perslab 361 Slab class 17:chunk size 3632 Persla B-Slab Class 18: Chunk size 4544 Perslab 230 Slab class 19:chunk size 5680 Perslab 184 Slab class 20:chunk size 7104 Perslab 147 Slab class 21:chunk size 8880 Perslab 118 Slab class 22:chunk size 11104 Perslab Slab class 23:chunk size 13880 Perslab Slab class 24:chunk size 17352 perslab Slab s 25:chunk size 21696 Perslab Slab class 26:chunk size 27120 Perslab-Slab class 27:chunk Siz E 33904 Perslab Slab class 28:chunk size 42384 Perslab-slab class 29:chunk size 52984 pers Lab Slab class 30:chunk size 66232 Perslab Slab class 31:chunk size 82792 Perslab (Slab) Class 32:chunk size 103496 Perslab Slab class 33:chunk size 129376 Perslab 8 Slab class 34:chun K Size 161720 Perslab 6 slab class 35:chunk size 202152 Perslab 5 slab class 36:chunk size 252696 Perslab 4 Slab CLAss 37:chunk size 315872 Perslab 3 slab class 38:chunk size 394840 Perslab 2 Slab class 39:chunk S ize 493552 Perslab 2 Slab class 40:chunk size 616944 Perslab 1 slab class 41:chunk size 771184 pe Rslab 1 Slab class 42:chunk size 1048576 perslab 1 <26 server Listening (auto-negotiate) <27 server Listening (auto-negotiate) <28 send buffer is 212992, now 268435456 <32 send buffer is 212992, now 268435456 <3 1 server Listening (UDP) <35 server listening (UDP) <30 server listening (UDP) <34 server listening (UDP) <29 Server listening (UDP) <33 server listening (UDP) <28 server listening (UDP) <32 server listening (UDP)
From the log information, we can learn a lot of things
The first is the memcached memory management mechanism, memcached will divide the memory into several slab of equal size, each slab by a fixed size into several chunk, with the increase of slab number, slab of chunk in the size of a certain rate of growth
As you can see from the log, memcached divides memory into 42 slab of 1M, and each slab is divided into several chunk of the same size, such as SLAB1, with a total of 8,738 chunk in each 120B,SLAB1, The size of each chunk in the slab42 is 1M, so there can only be one chunk in the slab42, and slab in slab grows at a rate of 1.25 as the chunk number increases (slab1:120,slab2:150,150/120= 1.25, exactly the default value of the growth factor specified by-f)
The size of each chunk in the SLAB5 is 240B, when I want to store 200B data, I will select a chunk in SLAB5 to store the data, if SLAB5 in chunk are used. Never mind, find a chunk that has recently been least used (Lru:least recently used) to deposit new data into it, even if there is an idle slab6 in the chunk, so when the size of the data saved in the memcached is basically consistent, High growth factor leads to memcahed memory usage
View Process Discovery Service started successfully:
sean@sean:/usr/local/bin$ ps-ef|grep memcached
Sean 17944 8981 0 21:28? 00:00:00./memcached-m 64-p 9999-u root-d-vvv
Sean 17951 14900 0 21:28 pts/15 00:00:00 grep--col Or=auto memcached
three, basic memcached command
Because the memcached service listens on port 9999, we can connect to memcached via Telnet
sean@sean:~$ telnet 127.0.0.1 9999
trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
You can then operate by entering the appropriate command
Added: Add key flag expire length
Add Name 1 0 4
Sean
STORED
Flag must be a positive integer that serializes the object, array, and cache. When it needs to be removed, it also needs to be deserialized, which can be deserialized according to the value of the flag, for example, the value of flag is 1, the value is 2, the deserialized object, the value is 3, the array is deserialized
Length is the byte of the cache
Query: Get key
Get name
VALUE name 1 4
Sean
End
Delete: Delete key
Delete name
DELETED
Substitution (key must exist to replace): Replace key flag expire length
Settings (key does not exist to perform a new addition, key exists to perform the substitution): Set key flag expire length
View Status: Stat
Stats STAT PID 18178 STAT uptime 1160 STAT time 1463495507 STAT version 1.4.25 STAT libevent 1.4.14b-stable STAT ize STAT rusage_user 0.000000 STAT rusage_system 0.040000 STAT curr_connections STAT total_connections STAT Ction_structures STAT Reserved_fds STAT cmd_get 6 STAT cmd_set 7 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 4 S TAT get_misses 2 STAT delete_misses 0 STAT delete_hits 2 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_ Hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT Aut H_errors 0 STAT bytes_read 264 STAT bytes_written 203 STAT limit_maxbytes 67108864 STAT Accepting_conns 1 STAT Listen_disa Bled_num 0 STAT Time_in_listen_disabled_us 0 STAT Threads 4 STAT conn_yields 0 STAT hash_power_level STAT hash_bytes 52 4288 STAT hash_is_expanding 0 STAT malloc_fails 0 STAT bytes 1 STAT curr_items STAT total_items 6 STAT Expired_unfetche D 0 STAT Evicted_unfetchEd 0 STAT Evictions 0 STAT reclaimed 0 STAT crawler_reclaimed 0 STAT crawler_items_checked 0 STAT lrutail_reflocked 0 End
get_hits/(get_hits + get_misses) to compute cache hit ratio