Memcache整理

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

###二.服务端安装

详见

###三.启动与停止

  1. 启动

     #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进程:ps -ef | grep memcached
    
     杀死进程:sudo kill -9 PID
    

###四.模拟add、get

  • telnet 连接:telnet 127.0.0.1 11211

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

###五.遍历memcached数据

主要通过命令 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

CommandDescriptionExample
getReads a valueget mykey
setSet a key unconditionallyset mykey 0 60 5
addAdd a new keyadd newkey 0 60 5
replaceOverwrite existing keyreplace key 0 60 5

转载于:https://my.oschina.net/u/205170/blog/151240

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值