Introduction
Distributed cache can be large and complicated when we use it extensively.
Simple-Spring-Memcached (SSM) implements some basic functions.
This project enables the cache to be locked and managed by spring bean containers. By using Java 5 annotations and Spring/AspectJ AOP on spymemcached or xmemcached client.
To use Simple-Spring-Memcached, you only need a little configuration and some annotations on the method.
Dependencies
Of course, you need at least one running memcache service. (Installation and usage instructions may be found on the memcached project page .)
Second, you need the Simple-Spring-Memcached JAR File
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
What you need to tell youApplicationContextThe Simple-Spring-Memcached configuration uses an import command
<import resource="simplesm-context.xml" />
This xml file can be obtained in the simple-spring-memcached-3.2.0.jar
Simple-Spring-Memcached also requires defining the AOP namespace to use the annotation of 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>
To tell Simple-Spring-Memcached your specific environment, you need to configure the Default client
<?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>
<?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>
The spymemcached or xmemcached client implements the consistentHashing algorithm and uses the consistentHashing parameter to determine which node is written data.
Usage
To understand the usage of memcache, first understand the key and 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>