1、memcached在Linux下安装
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口, 我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
首先安装libevent
#tar -zxvf libevent-2.0.12-stable.tar.gz
#cd /libevent
#./configuer --prefix=/usr/local/libevent
#make
#make install
然后安装memcached
#tar -zxvf memcached-1.4.14.tar.gz
#cd memcached
#./configure --with-libevent=/usr/local/libevent
#make
#make install
2、启动memcached
#/usr/local/bin/memcached -d -m 1024 -l 127.0.0.1 -p 20001 -u root -c 1024 -p memcached.pid
-d选项
是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口, 我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
3、连接memcached服务器
#telnet 127.0.0.1 20001
#set key1 0 60 4
zhou
#get key1
#add userId 0 0 5
zhou
#replace userId 0 0 5
wu
#delete wu
#gets userId
#cas userId 0 0 5 6
wang
#stats
#flush_all
#quit
4、memcached自启动脚本
-
- #!/bin/sh
- #
- # memcached: MemCached Daemon
- #
- # chkconfig: - 90 25
- # description: MemCached Daemon
- #
- # Source function library.
- . /etc/rc.d/init.d/functions
- . /etc/sysconfig/network
- start()
- {
- echo -n $"Starting memcached: "
- daemon /usr/local/src/memcached-1.4.14/memcached -u daemon -d -m 1024 -l 127.0.0.1 -p 58728
- echo
- }
- stop()
- {
- echo -n $"Shutting down memcached: "
- killproc memcached
- echo
- }
- [ -f /usr/local/src/memcached-1.4.14/memcached ] || exit 0
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart|reload)
- stop
- start
- ;;
- condrestart)
- stop
- start
- ;;
- *)
- echo $"Usage: $0 {start|stop|restart|reload|condrestart}"
- exit 1
- esac
- exit 0
新建/etc/init.d/memcached文件
#chmod u+x memcached
#chkconfig --add memcached
#chkconfig --add memcached
#chkconfig --level 235 memcached on
#service memcached start/stop/restart
#/etc/init.d/memcached start/stop/restart
5、Java客户端Xmemcached与spring集成
Xmemcached在多线程下效果更好。
spring配置文件如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="xmemcachedDao" class="com.iqiyi.itv.terminal4.memcache.dao.XMemcachedDao">
<property name="memCachedClientList">
<list>
<ref bean="memcachedClient1" />
</list>
</property>
</bean>
<bean name="memcachedClientBuilder1" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg>
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>192.168.58.130</value> <!--虚拟机IP-->
</constructor-arg>
<constructor-arg>
<value>20001</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
<constructor-arg>
<list>
<value>1</value>
</list>
</constructor-arg>
<property name="connectionPoolSize" value="8"></property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder">
<property name="compressionThreshold" value="1048576"></property>
</bean>
</property>
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
</property>
</bean>
<bean name="memcachedClient1" factory-bean="memcachedClientBuilder1"
factory-method="build" destroy-method="shutdown">
<property name="opTimeout" value="30000"></property>
</bean>
<beans>
获取到
memcachedClient就可以进行add、set、delete、replace等操作。