Spring Cache注解+Memcached

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();  
    }  
}  

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值