simple-spring-memcached
Introduction分布式缓存可以大的,错综复杂的当我们直接广泛的使用的时候。
Simple-Spring-Memcached (SSM)试着简单的实现了一些基础的用途。
这个项目使cache能够被spring的bean容器锁管理。通过在 spymemcached 或者xmemcached client上使用java5的注解和Spring/AspectJ AOP 。
使用 Simple-Spring-Memcached仅仅需要一点配置和在方法上面加上一些注解。
Dependencies
当然,首先你至少需要一个运行的memcache 服务。 (Installation and usage instructions may be found on the memcached project page.)
其次,你需要Simple-Spring-Memcached JAR文件
For spymemcached add dependency:
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>spymemcached-provider</artifactId>
<version>3.2.0</version>
</dependency>
For xmemcached add dependency:
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>xmemcached-provider</artifactId>
<version>3.2.0</version>
</dependency>
Configuration
你需要告诉你的ApplicationContextSimple-Spring-Memcached的配置通过一个import指令
<import resource="simplesm-context.xml" />
这个xml文件可以在 simple-spring-memcached-3.2.0.jar中得到
Simple-Spring-Memcached同时也需要定义AOP的命名空间来使用aop的注解
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy />
</beans>
为了告诉Simple-Spring-Memcached你具体环境,你需要配置默认的client
[*]spymemcached:
<?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">
<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.spymemcached.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>
</beans>
[*]xmemcached:
<?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">
<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>
</beans>
spymemcached or xmemcached 客户端实现consistentHashing算法,通过consistentHashing 参数来确定哪一个节点被写入数据。
Usage
明白memcache的用法首先的明白key,value。
Order of cache advice
Since 3.2.0 SSM cache advice runs before transaction advice. The default SSM cache advice order value is 0. The order can be set in the application context file to any custom value:
<beanclass="com.google.code.ssm.Settings">
<propertyname="order"value="500"/>
</bean>
页:
[1]