Spring Cache集成Memcached

使用memcahed的客户端xmemcached实现Cache、CacheManager接口。

一、添加jar依赖


<dependency>
   <groupId>com.googlecode.xmemcached</groupId>
   <artifactId>xmemcached</artifactId>
   <version>2.0.0</version>
  </dependency>二、实现Cache接口

package org.springframework.cache.demo.memchache;

import Java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;

public class MemcachedCache1 implements Cache {
 
 private MemcachedClient memcachedClient;
 
 public MemcachedClient getMemcachedClient() {
  return memcachedClient;
 }
 public void setMemcachedClient(MemcachedClient memcachedClient) {
  this.memcachedClient = memcachedClient;
 }
 @Override
 public String getName() {
  return memcachedClient.getName();
 }
 @Override
 public Object getNativeCache() {
  return memcachedClient;
 }
 @Override
 public ValueWrapper get(Object key) {
  Object object = null;
  try {
   object = memcachedClient.get((String)key);
  } catch (TimeoutException e) {
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  } catch (MemcachedException e) {
   e.printStackTrace();
  }
  return (object != null ? new SimpleValueWrapper(object) : null);
 }
 
 @SuppressWarnings("unchecked")
 @Override
 public <T> T get(Object key, Class<T> type) {
  Object object = null;
  try {
   object = memcachedClient.get((String)key);
  } catch (TimeoutException e) {
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  } catch (MemcachedException e) {
   e.printStackTrace();
  }
  return (T)object;
 }
 @Override
 public void put(Object key, Object value) {
  try {
   memcachedClient.set((String) key, 86400, value);
  } catch (TimeoutException e) {
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  } catch (MemcachedException e) {
   e.printStackTrace();
  }
 
 }
 @Override
 public ValueWrapper putIfAbsent(Object key, Object value) {
  put(key, value);
  return new SimpleValueWrapper(value);
 }
 @Override
 public void evict(Object key) {
  try {
   memcachedClient.delete((String)key);
  } catch (TimeoutException e) {
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  } catch (MemcachedException e) {
   e.printStackTrace();
  } 
 }
 @Override
 public void clear() {
  try {
   memcachedClient.flushAll();
  } catch (TimeoutException e) {
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  } catch (MemcachedException e) {
   e.printStackTrace();
  }
 }
}
三、实现CacheManager


package org.springframework.cache.demo.memchache;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import net.rubyeye.xmemcached.MemcachedClient;

import org.springframework.cache.Cache;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;

public class MemcachedCacheManager extends AbstractTransactionSupportingCacheManager {
 private ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>();
 private Map<String, Integer> expireMap = new HashMap<String, Integer>(); // 缓存的时间
 private MemcachedClient memcachedClient; // xmemcached的客户端

public MemcachedCacheManager() {
 }
 
 @Override
  protected Collection<? extends Cache> loadCaches() {
    Collection<Cache> values = cacheMap.values();
    return values;
  }

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

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