Spring Cache集成Memcached(2)

@Override
  public Cache getCache(String name) {
    Cache cache = cacheMap.get(name);
    if (cache == null) {
      Integer expire = expireMap.get(name);
      if (expire == null) {
        expire = 0;
        expireMap.put(name, expire);
      }
      cache = new MemcachedCache(name, expire.intValue(), memcachedClient);
      cacheMap.put(name, cache);
    }
    return cache;
  }

public void setMemcachedClient(MemcachedClient memcachedClient) {
    this.memcachedClient = memcachedClient;
  }

public void setConfigMap(Map<String, Integer> configMap) {
    this.expireMap = configMap;
  }

}
四、spring配置


<beans xmlns=""
 xmlns:xsi="" xmlns:cache=""
 xmlns:p=""
 xsi:schemaLocation="


">
 <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
 <cache:annotation-driven cache-manager="cacheManager"/>

<bean />
 
  <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value --> 
    <bean> 
        <property> 
            <set> 
                <bean> 
                    <property ref="memcachedClient" /> 
                </bean> 
            </set> 
        </property> 
    </bean>
   
 <!-- 配置memcached的缓存服务器 -->
 <bean>
  <constructor-arg>
   <list>
    <bean>
     <constructor-arg value="192.168.1.90" />
     <constructor-arg value="11211" />
    </bean>
   </list>
  </constructor-arg>
  <property value="userCache"/>
 </bean>
 <bean factory-bean="memcachedClientBuilder"
  factory-method="build" destroy-method="shutdown" />
 
</beans>另外一种实现方式,可以采用simple-spring-memcached实现,依赖jar包:


<dependency>
   <groupId>com.google.code.simple-spring-memcached</groupId>
   <artifactId>simple-spring-memcached</artifactId>
   <version>3.5.0</version>
  </dependency>
  <dependency>
   <groupId>com.google.code.simple-spring-memcached</groupId>
   <artifactId>xmemcached-provider</artifactId>
   <version>3.5.0</version>
  </dependency>基于xmemcached的spring集成。

也需要实现Cache接口:


package org.springframework.cache.demo.memchache;

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 MemcachedCache2 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() {
  return this.cache.getName();
 }

@Override
 public Object getNativeCache() {
  return this.cache;
 }

@Override
 public ValueWrapper get(Object key) {
    Object object = null; 
        try { 
            object = this.cache.get((String)key, SerializationType.JAVA); 
        } catch (TimeoutException e) { 
            e.printStackTrace(); 
        } catch (CacheException e) { 
            e.printStackTrace(); 
        } 
        return (object != null ? new SimpleValueWrapper(object) : null); 
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/15011.html