Spring3.1 Cache注解
依赖jar包:
Xml代码 收藏代码
com.google.code.simple-spring-memcached
simple-spring-memcached
3.5.0
com.google.code.simple-spring-memcached
xmemcached-provider
3.5.0
com.googlecode.xmemcached
xmemcached
2.0.0
applicationContext-cache-memcached.xml
Xml代码 收藏代码
<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.cpframework.core.cache.memcached.MemcachedCache">
<property name="cache" ref="defaultCache"/>
</bean>
<bean class="org.cpframework.core.cache.memcached.MemcachedCache">
<property name="cache" ref="commonCache"/>
</bean>
</set>
</property>
</bean>
<!-- memcached client defaultCache -->
<bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory">
<bean
class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="192.168.xx.xx:11211" />
</bean>
</property>
<property name="configuration">
<bean class="com.google.code.ssm.providers.CacheConfiguration">
<property name="consistentHashing" value="true" />
</bean>
</property>
<property name="cacheName" value="default"/>
</bean>
<!-- memcached client commonCache -->
<bean name="commonCache" class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory">
<bean
class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="192.168.xx.xx:11211" />
</bean>
</property>
<property name="configuration">
<bean class="com.google.code.ssm.providers.CacheConfiguration">
<property name="consistentHashing" value="true" />
</bean>
</property>
<property name="cacheName" value="commonCache"/>
</bean>
MemcachedCache.java
Java代码 收藏代码
package org.cpframework.core.cache.memcached;
import java.util.concurrent.TimeoutException;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import com.google.code.ssm.api.format.SerializationType;
import com.google.code.ssm.providers.CacheException;
public class MemcachedCache implements Cache{
private com.google.code.ssm.Cache cache;
public com.google.code.ssm.Cache getCache() {
return cache;
}
public void setCache(com.google.code.ssm.Cache cache) {
this.cache = cache;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return this.cache.getName();
}
@Override
public Object getNativeCache() {
// TODO Auto-generated method stub
return this.cache;
}
@Override
public ValueWrapper get(Object key) {
// TODO Auto-generated method stub
Object object = null;
try {
object = this.cache.get((String)key, SerializationType.JAVA);
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CacheException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (object != null ? new SimpleValueWrapper(object) : null);
}
@Override
public void put(Object key, Object value) {
// TODO Auto-generated method stub
try {
this.cache.set((String)key, 86400, value, SerializationType.JAVA);
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CacheException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void evict(Object key) {
// TODO Auto-generated method stub
try {
this.cache.delete((String)key);
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CacheException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void clear() {
// TODO Auto-generated method stub
try {
this.cache.flush();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CacheException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}