Memcache整理

###一.概述 详见官方网站java

###二.服务端安装spring

详见服务器

###三.启动与中止并发

  1. 启动ide

    #cd /usr/local/memcached-1.4.5
     # ./memcached -d -m 10 -u root -l 10.11.15.222 -p 12000 -c 256 -P /tmp/memcached.pid
     -d  选项是启动一个守护进程,
     -m  是分配给Memcache使用的内存数量,单位是MB,我这里是10MB
     -u  是运行Memcache的用户,我这里是root
     -l  是监听的服务器IP地址,我这里指定了服务器的IP地址10.11.15.222
     -p  是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口
     -c  是最大运行的并发链接数,默认1024,这里设置了256,按照服务器的负载量来设定
     -P  是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid
  2. 中止memcached

    找到memcached进程:ps -ef | grep memcached
    
     杀死进程:sudo kill -9 PID

###四.模拟add、get网站

  • telnet 链接:telnet 127.0.0.1 11211ui

  • 执行命令:具体命令详见附录1google

###五.遍历memcached数据.net

主要经过命令 stats itemsstats cachedump操做,具体详见

###六.java客户端操做

###七.simple-spring-memcached整理

  1. 添加 simple-spring-memcached 依赖

    <!--spymemcached 客户端-->
     <dependency>
     	<groupId>com.google.code.simple-spring-memcached</groupId>
     	<artifactId>spymemcached-provider</artifactId>
     	<version>3.2.0</version>
     </dependency>
     <!--xmemcached 客户端-->
     <dependency>
     	<groupId>com.google.code.simple-spring-memcached</groupId>
     	<artifactId>xmemcached-provider</artifactId>
     	<version>3.2.0</version>
     </dependency>
  2. 导入simplesm-context.xml

    <import resource="simplesm-context.xml" />
  3. 开启spring的aspectj

    <aop:aspectj-autoproxy />
  4. 配置defaultMemcachedClient

  5. 完整代码以下:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!--simplesm-context.xml封装在simple-spring-memcached-*.jar文件当中-->
     <import resource="../simplesm-context.xml" />
    
     <aop:aspectj-autoproxy />
    
     <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
     	<property name="cacheClientFactory">
     		<bean name="cacheClientFactory"
     			class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
     	</property>
     	<property name="addressProvider">
     		<bean class="com.google.code.ssm.config.DefaultAddressProvider">
     			<property name="address" value="127.0.0.1:11211" />
     		</bean>
     	</property>
     	<property name="configuration">
     		<bean class="com.google.code.ssm.providers.CacheConfiguration">
     			<property name="consistentHashing" value="true" />
     		</bean>
     	</property>
     </bean>
     <bean class="com.google.code.ssm.Settings">
     	<property name="order" value="500" />
     </bean>

    </beans>

  6. 项目中使用

    经过注解方式使用,具体详见1 详见2

###附录1

Command Description Example
get Reads a value get mykey
set Set a key unconditionally set mykey 0 60 5
add Add a new key add newkey 0 60 5
replace Overwrite existing key replace key 0 60 5
相关文章
相关标签/搜索