memcached 的总结

本文介绍Memcached在Linux下的安装步骤、启动配置及自启动脚本,并演示如何通过telnet命令进行基本操作。此外,还提供了Memcached与Java应用集成的示例,包括Spring配置文件的详细设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、memcached在Linux下安装
    首先安装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自启动脚本
    
  1.      
    • #!/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 --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等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值