memcached 的总结

来源:互联网 发布:java list移除元素 编辑:程序博客网 时间:2024/06/03 14:26
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   
  2. #   
  3. # memcached:    MemCached Daemon   
  4. #   
  5. # chkconfig:    - 90 25    
  6. # description:  MemCached Daemon   
  7. #   
  8. # Source function library.   
  9. . /etc/rc.d/init.d/functions   
  10. . /etc/sysconfig/network   
  11.     
  12. start()    
  13. {   
  14.         echo -n $"Starting memcached: "  
  15.         daemon /usr/local/src/memcached-1.4.14/memcached -u daemon -d -m 1024 -l 127.0.0.1 -p 58728  
  16.         echo   
  17. }   
  18.     
  19. stop()    
  20. {   
  21.         echo -n $"Shutting down memcached: "  
  22.         killproc memcached    
  23.         echo   
  24. }   
  25.     
  26. [ -f /usr/local/src/memcached-1.4.14/memcached ] || exit 0  
  27.     
  28. # See how we were called.   
  29. case "$1" in   
  30.   start)   
  31.         start   
  32.         ;;   
  33.   stop)   
  34.         stop   
  35.         ;;   
  36.   restart|reload)   
  37.         stop   
  38.         start   
  39.         ;;   
  40.   condrestart)   
  41.         stop   
  42.         start   
  43.         ;;   
  44.   *)   
  45.         echo $"Usage: $0 {start|stop|restart|reload|condrestart}"  
  46.         exit 1  
  47. esac   
  48. exit 0  
    来源: <http://www.cnblogs.com/technet/archive/2011/09/11/2173485.html>
     

  49.  
        新建/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等操作。
0 0