Spring Cache集成Memcached

2021-06-10 10:57:18 浏览数 (1)

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

一、添加jar依赖

   com.googlecode.xmemcached

   xmemcached

   2.0.0

  二、实现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 get(Object key, Class 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();

  }

 }

}

三、实现CacheManage

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 cacheMap = new ConcurrentHashMap();

 private Map expireMap = new HashMap(); // 缓存的时间

 private MemcachedClient memcachedClient; // xmemcached的客户端

 public MemcachedCacheManager() {

 }

 

 @Override

  protected Collection loadCaches() {

    Collection values = cacheMap.values();

    return values;

  }

  @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 configMap) {

    this.expireMap = configMap;

  }

}

四、spring配置

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"

 xmlns:p="http://www.springframework.org/schema/p"

 xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache.xsd">

 

 

 

 

   

     

         

             

                 

                     

                 

             

         

   

   

 

 

 

   

   

     

     

   

   

 

 

 

 <bean id="memcachedClient" factory-bean="memcachedClientBuilder"

  factory-method="build" destroy-method="shutdown" />

 

另外一种实现方式,可以采用simple-spring-memcached实现,依赖jar包:

   com.google.code.simple-spring-memcached

   simple-spring-memcached

   3.5.0

 

 

   com.google.code.simple-spring-memcached

   xmemcached-provide

   3.5.0

  基于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); 

 }

 @SuppressWarnings("unchecked")

 @Override

 public T get(Object key, Class type) {

  try {

   return (T)this.cache.get((String)key, SerializationType.JAVA);

  } catch (TimeoutException e) {

   e.printStackTrace();

  } catch (CacheException e) {

   e.printStackTrace();

  }

  return null;

 }

 @Override

 public void put(Object key, Object value) {

  try { 

            this.cache.set((String)key, 86400, value, SerializationType.JAVA); 

        } catch (TimeoutException e) { 

            e.printStackTrace(); 

        } catch (CacheException 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 { 

            this.cache.delete((String)key); 

        } catch (TimeoutException e) { 

            e.printStackTrace(); 

        } catch (CacheException e) { 

            e.printStackTrace(); 

        } 

 }

 @Override

 public void clear() {

  try { 

            this.cache.flush(); 

        } catch (TimeoutException e) { 

            e.printStackTrace(); 

        } catch (CacheException e) { 

            e.printStackTrace(); 

        } 

 }

}

这里就不实现CacheManager接口使用org.springframework.cache.support.SimpleCacheManager配置:

<beans xmlns='http://www.springframework.org/schema/beans'

 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

 xmlns:context='http://www.springframework.org/schema/context'

 xmlns:c='http://www.springframework.org/schema/c'

 xmlns:p='http://www.springframework.org/schema/p'

 xmlns:cache='http://www.springframework.org/schema/cache'

 xmlns:aop="http://www.springframework.org/schema/aop"

 xsi:schemaLocation='

  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

        '>

       

 

 

 

 

 

 

   

   

     

   

   

 

 

 

 

 

   <bean

    class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />

 

 

   

   

   

 

 

   

   

   

 

 

 

CentOS 6.6下Memcached 源码安装配置  http://www.linuxidc.com/Linux/2015-09/123019.htm

Memcached 安装及启动脚本 http://www.linuxidc.com/Linux/2013-07/87641.htm

PHP中使用Memcached的性能问题 http://www.linuxidc.com/Linux/2013-06/85883.htm

Ubuntu下安装Memcached及命令解释 http://www.linuxidc.com/Linux/2013-06/85832.htm

Memcached的安装和应用 http://www.linuxidc.com/Linux/2013-08/89165.htm

使用Nginx Memcached的小图片存储方案 http://www.linuxidc.com/Linux/2013-11/92390.htm

Memcached使用入门 http://www.linuxidc.com/Linux/2011-12/49516p2.htm

0 人点赞