memached,分布式缓存,在互联网应用中应用相当普遍,性能极佳,下面简单说一下memcached的使用及监控。
1) 确认libevent已安装
# rpm -qa | grep libevent # yum install libevent-devel
(如果是RHEL5请查看我的另外一遍博文:http://www.cnblogs.com/javapro/archive/2012/09/25/2701462.html)
2) 安装memcached
# tar zxvf memcached-1.4.15.tar.gz # cd memcached-1.4.15 # ./configure --prefix=/usr/local/memcached-1.4.15 # make && make test # make install # make clean
3) 使用
# memcached -d -u memcached -p 12000 -f 1.25 -m 1024
memcached -d -u memcached -p 12001 -f 1.25 -m 1024
memcached -d -u memcached -p 12002 -f 1.25 -m 1024
memcached -d -u memcached -p 12003 -f 1.25 -m 1024
-p 侦听端口号
-t 线程数(与CPU个数一致)
-f 成长因子,可以有效使用内存
-m 最大使用内存
-d 后台驻留
键名:appname:modulename:entity:id
>> 使用phpMemcachedAdmin进行监控
1) 安装php
# tar zxvf php-5.3.17.tar.gz # cd php-5.3.17 # ./configure --prefix=/usr/local/php-5.3.17 --enable-mbstring --with-apx2=/usr/local/httpd-2.0.64/bin/apxs # make && make test # make install # make clean
2) 安装phpMemcachedAdmin
# tar zxvf phpMemcachedAdmin-1.2.2-r262.zip # mv phpMemcachedAdmin-1.2.2-r262 /usr/local/httpd-2.0.64/htdocs/memcached
3) 修改apache配置
# vi /usr/local/httpd-2.0.64/conf/httpd.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.php index.html
<Directory “/usr/local/httpd-2.0.64/htdocs/memcached”>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
>> Spring整合memcached
<bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean"> <property name="servers" value="10.1.1.116:12000,10.1.1.116:12001,10.1.1.116:12002,10.1.1.116:12003" /> <property name="protocol" value="BINARY" /> <property name="transcoder"> <bean class="net.spy.memcached.transcoders.SerializingTranscoder"> <property name="compressionThreshold" value="1024"/> </bean> </property> <property name="opTimeout" value="1000" /> <property name="timeoutExceptionThreshold" value="1998" /> <property name="hashAlg"> <value type="net.spy.memcached.DefaultHashAlgorithm">KETAMA_HASH</value> </property> <property name="locatorType" value="CONSISTENT" /> <property name="failureMode" value="Redistribute" /> <property name="useNagleAlgorithm" value="false" /> </bean>
>> add和set的区别
add和set方法的不同之处是add方法不允许key值相同,如果第二次add的key相同,则存储失败,而set方法允许key相同,如果相同,则替换该key对应的value。