作者:gaoyong0713 | 来源:互联网 | 2023-09-11 14:19
LinuxDistribution:Ubuntu14Libevent:1.4.14b下载地址:libevent官网Memcached:1.4.25下载地址:memcached官网一,安装memca
Linux Distribution:Ubuntu 14
Libevent:1.4.14b
下载地址:libevent官网
Memcached:1.4.25
下载地址:memcached官网
一,安装memcached
Memcached的安装依赖Libevent,首先安装Libevent:
sean@sean:~$ tar -xzf libevent-1.4.14b-stable.tar.gz
sean@sean:~$ cd libevent-1.4.14b-stable/
sean@sean:~/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 make clean
Libevent将会被安装至/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
然后安装Memcached:
sean@sean:~$ tar -xzf memcached-1.4.25.tar.tar
sean@sean:~$ cd memcached-1.4.25/
sean@sean:~/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 make clean
Memcached将会被安装至/usr/local/bin下:
sean@sean:/usr/local/bin$ ll
total 544
drwxr-xr-x 2 root root 4096 4月 21 21:32 ./
drwxr-xr-x 10 root root 4096 2月 18 07:12 ../
-rwxr-xr-x 1 root root 545998 4月 21 21:32 memcached*
二,启动memcached服务
-m:分配给memcached的最大内存,以M为单位
-p:服务监听的TCP端口,默认值是11211
-d:服务当做守护进程启动
-vv:打印详细信息
-f:增长因子,默认值是1.25,指定的值必须比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 120 perslab 8738
slab class 3: chunk size 152 perslab 6898
slab class 4: chunk size 192 perslab 5461
slab class 5: chunk size 240 perslab 4369
slab class 6: chunk size 304 perslab 3449
slab class 7: chunk size 384 perslab 2730
slab class 8: chunk size 480 perslab 2184
slab class 9: chunk size 600 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 perslab 288
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 94
slab class 23: chunk size 13880 perslab 75
slab class 24: chunk size 17352 perslab 60
slab class 25: chunk size 21696 perslab 48
slab class 26: chunk size 27120 perslab 38
slab class 27: chunk size 33904 perslab 30
slab class 28: chunk size 42384 perslab 24
slab class 29: chunk size 52984 perslab 19
slab class 30: chunk size 66232 perslab 15
slab class 31: chunk size 82792 perslab 12
slab class 32: chunk size 103496 perslab 10
slab class 33: chunk size 129376 perslab 8
slab class 34: chunk 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 size 493552 perslab 2
slab class 40: chunk size 616944 perslab 1
slab class 41: chunk size 771184 perslab 1
slab class 42: chunk size 1048576 perslab 1
<26 server listening (auto-negotiate)
<27 server listening (auto-negotiate)
<28 send buffer was 212992, now 268435456
<32 send buffer was 212992, now 268435456
<31 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)
从日志信息中,我们可以了解到很多东西
首先就是memcached的内存管理机制,memcached会将内存分为数个大小相等的slab,每个slab又按固定大小分成数个chunk,随着slab编号的增加,slab中chunk的大小按一定比率增长
从日志中可以看到,memcached将内存分为了42个大小均为1M的slab,而每个slab又分为大小相同的数个chunk,比如slab1中每个chunk的大小为120B,slab1中总共有8738个这样的chunk,而slab42中每个chunk的大小为1M,所以slab42中只能有一个chunk,随着slab编号的增加,slab中chunk的大小按1.25的比率增长(slab1:120,slab2:150,150/120=1.25,正好是-f指定的增长因子的默认值)
slab5中每个chunk的大小为240B,当我要存放200B的数据时,则会选择slab5中的一个chunk存放数据,如果slab5中的chunk都被使用了怎么办?没关系,找到一个最近最少使用(LRU:least recently used)的chunk,将新数据存入其中,即使slab6中有空闲的chunk,因此当memcached中保存数据的大小基本一致时,增长因子过大将导致memcahed的内存使用率并不是很高
查看进程发现服务启动成功:
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 --color=auto memcached
三,基本memcached命令
由于memcached服务监听9999端口,我们可以通过telnet的方式连接至memcached
sean@sean:~$ telnet 127.0.0.1 9999
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
之后可以通过输入相应的命令进行操作
新增:add key flag expire length
add name 1 0 4
sean
STORED
flag必须为一个正整数,将对象、数组序列化后存入缓存,当需要取出时,还需反序列化,这时可根据flag的值进行相应的反序列化,比如flag的值是1,不需要反序列化,值为2,反序列化对象,值为3,反序列化数组
length为缓存的字节长度
查询:get key
get name
VALUE name 1 4
sean
END
删除:delete key
delete name
DELETED
替换(key必须存在才能替换):replace key flag expire length
设置(key不存在执行新增,key存在执行替换):set key flag expire length
查看状态:stat
stats
STAT pid 18178
STAT uptime 1160
STAT time 1463495507
STAT version 1.4.25
STAT libevent 1.4.14b-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.040000
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 6
STAT cmd_set 7
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 4
STAT 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 auth_errors 0
STAT bytes_read 264
STAT bytes_written 203
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT time_in_listen_disabled_us 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 73
STAT curr_items 1
STAT total_items 6
STAT expired_unfetched 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)能够计算出缓存命中率