大型网站架构设计-分布式缓存memcache简介及安装

2014-11-21 9:35:50     31 人阅读    

分布式缓存
             在高并发环境下,大量的读、写请求涌向数据库,磁盘的处理速度与内存显然不在一个量级,从减轻数据库的压力和提高系统响应速度两个角度来考虑,一般都会在数据库之前加一层 缓存。由于单台机器的内存资源和承载能力有限,并且如果大量使用本地缓存,也会使相同的 数据被不同的节点存储多份,对内存资源造成较大的浪费,因此才催生出了分布式缓存。
本节将详细介绍分布式缓存的典型代表memcache,以及分布式缓存的应用场景。最为典型 的场景莫过于分布式session。


memcache简介及安装
         memcache是danga.com的一个项目,它是一款开源的高性能的分布式内存对象缓存系统, 最早是给LiveJournal2提供服务的,后来逐渐被越来越多的大型网站所采用,用于在应用中减少 对数据库的访问,提高应用的访问速度,并降低数据库的负载。


         为了在内存中提供数据的高速查找能力,memcache使用key-value形式存储和访问数据, 在内存中维护一张巨大的HashTable,使得对数据查询的时间复杂度降低到0(1),保证了对数 据的高性能访问。内存的空间总是有限的,当内存没有更多的空间来存储新的数据时,memcache 就会使用LRU (LeastRecently Used)算法,将最近不常访问的数据淘汰掉,以腾出空间来存放 新的数据。memcache存储支持的数据格式也是灵活多样的,通过对象的序列化机制,可以将更 高层抽象的对象转换成为二进制数据,存储在缓存服务器中,当前端应用需要时,又可以通过 二进制内容反序列化,将数据还原成原有对象。
memcache 的安装
由于memcache使用了 libevent来进行高效的网络连接处理,因此在安装memcache之前, 需要先安装libevent。
下载libevent3,这里采用的是1.4.14版本的libevent。
wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-
stable.tar.gz  
ilonglong@ubuntu:~/tenp$ wget https://gtthub.con/downloads/ltbevent/llbevent/libe vent-1.4.14b-stable.tar.gz
--2014-03-19 04:52:41-- https://glthub*con/downloads/libevent/llbevent/libevent -1.4.14b- stable.ta r.gz
Resolving glthub*con (glthub.con)... 192.30.252.129
Connecting to glthub.com (github.com)f192.30.252.129|:443... connected*
HTTP request sent, awaiting response... 302 Found
Locationr http://cloud.github*corn/downloads/llbevent/libevent/libevent-l.4*14b-s table.tar.gz [following]
--2014-03-19 04:52:55-- http://cloud.github*cam/downloads/libevent/libevent/lib event-1.4*14b-stable.tar.gz
Resolving cloud.github.com (cloud.github.con)* 205.251.212.145, 205.251.212.82 ,54.230.126.248, ...
Connecting to cloud,glthub.con Ccloud.github.com)]265.251.212.145]:80... connect ed.
HTTP request sent, awaiting response... 302 Found
Location: httpI//218.108.192.119:80/lQ2W3E4R5T6Y7U8I9O0PlZ2X3C4V5B/cl〇ud.glthub. con/downloads/llbevent/libevent/llbevent-1»4.14b-stable.tar.gz [following] ”2014-03-19 04:53:04-- http://218.108.192.119/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/C loud,glthub.con/downloads/ltbevent/libevent/llbevent-1 * 4.14b-stable.tar.gz Connecting to 218*108*192.119:80…connected.
HTTP request sent, awaiting response... 200 OK Length: 474874 (464K) [application/gzip]
Saving to: 'llbevent-1.4*14b-stable.tar*gz'
解压:
tar -xf libevent-1.4.14b-stable.tar.gz


配置、编译、安装libevent:
./configure
longlong@ubuntu:-/temp/llbevent-1.4.14b-stable? ./configure
checking for a BSD-compatible install.* * /usr/btn/lnstall -c
checking whether build environment is sane*., yes
checking for a thread-safe nkdir -p*.. /bin/mkdir -p
checking for gawk... no
checking for nawk.,. nawk
checking whether make sets $(NAKE)**. yes
checking build system type.* * i686-pc-linux-gnu
checking host system type..* 1686-pc-linux-gnu
checking for gcc.“ gcc
checking whether the C compiler works.* * yes
checking for C compiler default output file nane... a.out
make
longlong@ubuntu:~/tenp/libevent-l.4.14b-stable$ make echo V* event-config.h1 > event-config.h
echo 1 * Generated by autoconf; post-processed by libevent.' » event-conflg*h echo 1 * Do not edit this file*' » event-config.h
echo 1 * Do not rely on macros in this file existing in later versions.1» event -config.h
echo 1 *f' » event-config.h
echo 1#ifndef _EVENT_CONFIG_H_' >> event-config.h echo 'ffdeflne _EVENT_CONFIG_H_' » event-config.h sed -e 's/Sdeftne /#deftne _EVENT_/' \
-e 's/Sundef /#undef EVENT/1 \
-e 's/ffifndef /#lfndef _EVENT_/' < config.h » event-config.h echo "#endlf" » event-config.h
make all-recursive 
sudo make install
longlong@ubuntu:~/tenp/llbevent-1.4.14b-stable$ sudo make Install [sudo] password for longlong: make Install-recursive
nake[l]: Entering directory '/h〇ne/longlong/tenp/libevent-1.4.14b-stable'
Making install in .
nake[2]: Entering directory '/h〇ne/longlong/tenp/llbevent-1.4.14b-stable' nake[3]: Entering directory '/h〇ne/longlong/tenp/llbevent-1.4.14b-stable' test -z "/usr/local/bin" || /bin/nkdir -p "/usr/local/bln"
/usr/bin/lnstall -c event_rpcgen.py '/usr/local/bin* test -z "/usr/local/lib" || /bln/mkdlr -p "/usr/local/lib"
/bin/bash ./libtool --node=install /usr/bin/install -c llbevent.la libeven _core.la libevent_extra.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libevent-1.4.so.2.2.0 /usr/local/lil /llbevent-1.4.so.2.2.0
libtool: Install: (cd /usr/local/lib && { In -s -f libevent-1.4.so.2.2.0 libevei t-1.4.so.2 || { rn -f llbevent-1.4.so.2 && In -s llbevent-1.4.so.2.2.0 llbevent 1.4.so.2; }; })
libtool: install: (cd /usr/local/lib && { In -s -f libevent-1.4.so.2.2.0 libevei t.so || { rn -f llbevent.so && In -s libevent-1.4.so.2.2.0 llbevent.so; }; }) libtool: Install: /usr/bin/install -c .llbs/libevent«lal /usr/local/lib/llbeven
下载memcache,并解压:
wget http://www.memcached.Org/files/memcached-1.4.17.tar.gz
longlong@ubuntu:~/tenp$ wget http://www.nencached.org/ftles/nencached-l.4.17.tar
• gz
--2014-03-19 05:02:12-- http://www.mencached.Org/flles/memcached-l.4.17.tar.gz
Resolving www.nencached.org (www.nencached.org)... 69.46.88.68
Connecting to www.memcached.org (www.memcached.org)|69.46.88.68|:80... connected
HTTP request sent, awaiting response... 302 Found
Location: http://218.108.192.145:80/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/www.memcached .org/flles/memcached-1.4.17.tar.gz [following]
--2014-03-19 05:02:24-- http://218.108.192.145/1Q2W3E4R5T6Y7U8I906P1Z2X3C4V5B/W ww.memcached.org/flles/memcached-1.4.17.tar.gz Connecting to 218.108.192.145:80... connected.
HTTP request sent, awaiting response... 266 OK Length: 326970 (319K) [applicatlon/x-tar]
Saving to: 'memcached-1.4.17.tar.gz'
100%[======================================>] 326,970 1.78M/S in 0.2s
2014-03-19 05:02:24 (1.78 MB/s) - nencached-1.4.17.tar.gz' saved [326970/326970
tar -xf memcached-1.4.17.tar.gz


配置、编译、安装memcache:
./configure
longlong@ubuntu:~/tenp/nencached-1.4.17$ ./configure
checking build system type... 1686-pc-linux-gnu
checking host system type... 1686-pc-llnux-gnu
checking target system type... i686-pc-llnux-gnu
checking for a BSD-compatible Install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe nkdir -p... /bln/mkdir -p
checking for gawk... no
checking for nawk... nawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g.“ yes 
make
longlong@ubuntu:-/tenp/nencached-1.4.17$ make make all-recursive
nake[l]: Entering directory '/home/longlong/tenp/mencached-1.4.17'
Making all in doc
nake[2]: Entering directory '/home/longlong/tenp/mencached-1.4.17/doc' make all-an
nake[3]: Entering directory '/home/longlong/tenp/mencached-1.4.17/doc' nake[3]: Nothing to be done for %all-an'.
nake[3]: Leaving directory %/h〇ne/longlong/tenp/nemcached-l.4.17/doc'
nake[2]: Leaving directory '/h〇ne/longlong/tenp/nemcached-l.4.17/doc'
nake[2]: Entering directory '/home/longlong/tenp/nencached-1.4.17'
gcc -Std=gnu99 -DHAVE_CONFIG_H -I. -DNDEBUG -g -02 -pthread -pthread -Wall -V
error -pedantic -Wnissing-prototypes -Wnlsslng-declarations -Wredundant-decls •卜
T mencached-memcached.o -MD -MP -MF .deps/nencached-nencached.Tpo -c -o mencachc
d-memcached.o * test -f 'memcached.c' || echo '''nemcached.c
nv -f .deps/mencached-nemcached.Tpo .deps/memcached-nemcached.Po
gcc -std=gnu99 -DHAVE_CONFIG_H -I. >DNDEBUG -g -〇2 -pthread -pthread -Wall -V
error -pedantic -Wnissing-prototypes -Wnlsslng-declarations -Wredundant-decls •卜
T memcached-hash.o -MD -MP -MF .deps/memcached-hash.Tpo -c -o memcached-hash.o ’
sudo make install
longlong@ubuntu:~/tenp/nencached-1.4.17$ sudo make install make install-recursive
nake[l]: Entering directory */hone/longlong/tenp/memcached-1.4.17'
Making install in doc
nake[2]: Entering directory */hone/longlong/tenp/memcached-1.4.17/doc' make install-an
nake[3]: Entering directory */h〇ne/longlong/tenp/mencached-1.4.17/doc' nake[4]: Entering directory */hone/longlong/tenp/memcached-1.4.17/doc' nake[4]: Nothing to be done for 'install-exec-an'.
test -z "/usr/local/share/nan/nanl" || /bin/nkdir -p "/usr/local/share/nan/nanl" /us「/bin/install -c -m 644 memcached.l '/usr/local/share/nan/nanl1 nake[4]: Leaving directory '/home/longlong/tenp/mencached-1.4.17/doc' nake[3]: Leaving directory '/home/longlong/tenp/mencached-1.4.17/doc' nake[2]: Leaving directory '/home/longlong/tenp/mencached-1.4.17/doc' nake[2]: Entering directory */hone/longlong/tenp/memcached-1.4.17' nake[3]: Entering directory '/hone/longlong/tenp/mencached-1.4.17' test -z "/usr/local/bln" || /bln/mkdlr -p "/usr/local/bin" 


启动与关闭memcache
     启动memcache服务:
/usr/local/bin/memcached -d -m 10 -u root -l 192.168.136.135 -p 11211 -c 32 -P /tmp/memcached.pid
参数的含义如下:
-d表示启动的是一个守护进程;
-m指定分配给memcache的内存数量,单位是MB,这里指定的是10 MB。
-u指定运行memcache的用户,这里指定的是root;
-l指定监听的服务器的IP地址;
-p设置memcache监听的端口,这里指定的是11211;
-c指定最大允许的并发连接数,这里设置为32;
-P指定memcache的pid文件保存的位置。
longlong@ubuntu:~/tenp/mencached-1.4.17$ /usr/local/bin/memcached -d -n 10 -u ro ot -1 192.168.136.135 -p 11211 -c 256 -P /tmp/memcached.pid longlong@ubuntu:~/tenp/mencached-1.4.17$ ps >aux | grep nencached Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.htnl longlong 8132 1.0 0.0 47996 816 ? Ssl 05:57 0:00 /usr/local/bln/
nencached -d -n 10 -u root -1 192.168.136.135 -p 11211 -c 256 -P /tnp/nencached. pid I
关闭memcache服务:
kill 'cat /tmp/memcached.pid
longlong@ubuntu:~/tenp/nencached-1.4.17$ kill 'cat /tnp/nencached.pid'
longlong@ubuntu:~/tenp/nencached-1.4.17$ ps -aux | grep nencached
Warning: bad ps syntax, perhaps a bogus •-•? See http://procps.sf.net/faq.htnl


原文地址:http://www.itmmd.com/201411/200.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

大型网站架构设计-memcache AP丨与分布式   上一篇
下一篇  

精彩回复
发表评论
姓名:       

《程序员app》专门为程序员量身定做!