存储+调优:存储-memcached

什么是memcached?
    高性能的分布式内存缓存服务器。通过缓存数据库的查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。

在memcached中存什么?
尽快被保存
访问频率高

1.数据保存方式 chunk
2.数据有效期
3.数据过期慵懒检查
4.数据的存取 set get
5。通过应用程序访问memcache


数据存储方式
chunk


数据过期方式
• Lazy Expiration
memcached内部不会监视记录是否过期,而是在get时查看记录的时间戳,检查记录是否过期。这种技术被称为lazy(惰性)expiration。因此,memcached不会在过期监视上耗费CPU时间。

• LRU
memcached会优先使用已超时的记录的空间,但即使如此,也会发生追加新记录时空间不足的情况,此时就要使用名为 Least Recently Used(LRU)机制来分配空间。顾名思义,这是删除“最近最少使用”的记录的机制。因此,当memcached的内存空间不足时(无法从slab class 获取到新的空间时),就从最近未被使用的记录中搜索,并将其空间分配给新的记录。从缓存的实用角度来看,该模型十分理想。

[root@localhost tmp]# tar xf libevent-2.0.15-stable.tar.gz 
[root@localhost tmp]# cd libevent-2.0.15-stable
[root@localhost libevent-2.0.15-stable]# ./configure && make && make install
[root@localhost tmp]# tar xf memcached-1.4.5.tar.gz 
[root@localhost tmp]# cd memcached-1.4.5
[root@localhost memcached-1.4.5]# ./configure && make && make install
[root@localhost memcached-1.4.5]# ./memcached -h
[root@localhost memcached-1.4.5]# ./memcached -p 11211 -l 172.16.1.6 -u root -m 64 -c 10 -vvv -f 1.2 -n 60
slab class   1: chunk size        96 perslab   10922
slab class   2: chunk size       120 perslab    8738
slab class   3: chunk size       144 perslab    7281
slab class   4: chunk size       176 perslab    5957
slab class   5: chunk size       216 perslab    4854
slab class   6: chunk size       264 perslab    3971
slab class   7: chunk size       320 perslab    3276
slab class   8: chunk size       384 perslab    2730
slab class   9: chunk size       464 perslab    2259
slab class  10: chunk size       560 perslab    1872
slab class  11: chunk size       672 perslab    1560
slab class  12: chunk size       808 perslab    1297
slab class  13: chunk size       976 perslab    1074
slab class  14: chunk size      1176 perslab     891
slab class  15: chunk size      1416 perslab     740
slab class  16: chunk size      1704 perslab     615
slab class  17: chunk size      2048 perslab     512
slab class  18: chunk size      2464 perslab     425
slab class  19: chunk size      2960 perslab     354
slab class  20: chunk size      3552 perslab     295
slab class  21: chunk size      4264 perslab     245
slab class  22: chunk size      5120 perslab     204
slab class  23: chunk size      6144 perslab     170
slab class  24: chunk size      7376 perslab     142
slab class  25: chunk size      8856 perslab     118
slab class  26: chunk size     10632 perslab      98
slab class  27: chunk size     12760 perslab      82
slab class  28: chunk size     15312 perslab      68
slab class  29: chunk size     18376 perslab      57
slab class  30: chunk size     22056 perslab      47
slab class  31: chunk size     26472 perslab      39
slab class  32: chunk size     31768 perslab      33
slab class  33: chunk size     38128 perslab      27
slab class  34: chunk size     45760 perslab      22
slab class  35: chunk size     54912 perslab      19
slab class  36: chunk size     65896 perslab      15
slab class  37: chunk size     79080 perslab      13
slab class  38: chunk size     94896 perslab      11
slab class  39: chunk size    113880 perslab       9
slab class  40: chunk size    136656 perslab       7
slab class  41: chunk size    163992 perslab       6
slab class  42: chunk size    196792 perslab       5
slab class  43: chunk size    236152 perslab       4
slab class  44: chunk size    283384 perslab       3
slab class  45: chunk size    340064 perslab       3
slab class  46: chunk size    408080 perslab       2
slab class  47: chunk size    489696 perslab       2
slab class  48: chunk size    587640 perslab       1
slab class  49: chunk size    705168 perslab       1
slab class  50: chunk size    846208 perslab       1
slab class  51: chunk size   1048576 perslab       1
<26 server listening (auto-negotiate)
<27 send buffer was 110592, now 268435456
<27 server listening (udp)
<27 server listening (udp)
<27 server listening (udp)
<27 server listening (udp)


[root@node1 ~]# telnet 172.16.1.6 11211
Trying 172.16.1.6...
Connected to 172.16.1.6 (172.16.1.6).
Escape character is '^]'.

<27 server listening (udp)
<27 server listening (udp)
<27 server listening (udp)
<27 server listening (udp)
<28 new auto-negotiating client connection


[root@node1 ~]# telnet 172.16.1.6 11211
Trying 172.16.1.6...
Connected to 172.16.1.6 (172.16.1.6).
Escape character is '^]'.
set nothing 0 180 10   ----- 0:标识  180:过期时间 10:多少字节
1234567890
STORED
get nothing
VALUE nothing 0 10
1234567890
END


[root@node1 ~]# yum install httpd php
[root@node1 ~]# rpm -ivh php-pecl-memcache-2.1.2-1.el5.rf.i386.rpm 
[root@node1 ~]# cp test.php /var/www/html/
[root@node1 ~]# vim /var/www/html/test.php 
<?php
$memcache = new Memcache;
$memcache->connect('172.16.1.6',11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";

var_dump($get_result);
?>

[root@node1 ~]# service httpd start

[root@localhost ~]# elinks --dump http://172.16.1.1/test.php
   Server's version: 1.4.5
   Store data in the cache (data will expire in 10 seconds)
   Data from the cache:
   object(stdClass)#3 (2) { ["str_attr"]=> string(4) "test" ["int_attr"]=>
   int(123) }


apache + tomcat + memcached

apache 作为分发器
tomcat 提供web服务
mc     保证session同步


ip  192.168.1.106
apache:
    
    yum install httpd httpd-devel gcc -y
    
ajp1.3:
    tar zxvf tomcat-connectors-1.2.32-src.tar.gz  -C /tmp
    cat /tmp/tomcat-connectors-1.2.32-src/BUILD.txt 
    cd native
     ./configure --with-apxs=/usr/sbin/apxs 
    make && make install
    cp  ../conf/httpd-jk.conf /etc/httpd/conf.d/jk.conf
    vim /etc/httpd/conf.d/jk.conf
    JkMount   /*.jsp     wlb            
    JkMount   /manager/*     wlb
    
    cp  ../conf/workers.properties /etc/httpd/conf
    vim /etc/httpd/conf/workers.properties
    worker.list=wlb,jkstatus
    worker.wlb.type=lb
    worker.wlb.balance_workers=tomcat1,tomcat2
    worker.tomcat1.type=ajp13
    worker.tomcat1.host=192.168.1.111
    worker.tomcat1.port=8009
    worker.tomcat1.lbfactor=1
    worker.tomcat2.type=ajp13
    worker.tomcat2.host=192.168.1.112
    worker.tomcat2.port=8009
    worker.tomcat2.lbfactor=1

    /etc/init.d/httpd  restart

ip  192.168.1.111 192.168.1.112
jdk:
    service httpd stop
    chmod +x jdk-6u27-linux-i586.bin 
    ./jdk-6u27-linux-i586.bin 
    mv jdk1.6.0_27/ /usr/local/jdk
    vim /etc/bashrc
        export JAVA_HOME=/usr/local/jdk
        export JAVA_BIN=/usr/local/jdk/bin/
        export PATH=$JAVA_HOME/bin:$PATH
        export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
        export JAVA_HOME JAVA_BIN PATH CLASSPATH
    source /etc/bashrc 
    java -version
    mv apache-tomcat-6.0.32 /usr/local/tomcat

tomcat:
    tar zxf apache-tomcat-6.0.32.tar.gz 
    mv apache-tomcat-6.0.32 /usr/local/tomcat
    cd /usr/local/tomcat/bin 
    ./startup.sh 

cat >/usr/local/tomcat/webapps/ROOT/index.jsp <<EOF
<html>
<body bgcolor="red">
<center>
<%= request.getSession().getId() %>
<h1>Tomcat 1</h1>
</body>
</html>

存储+调优:存储-memcached-LMLPHP

05-26 23:16