| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

Spring Cache注解+Memcached

Spring3.1 Cache注解

依赖jar包:

<!-- simple-spring-memcached -->      <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>      <dependency>        <groupId>com.googlecode.xmemcached</groupId>        <artifactId>xmemcached</artifactId>        <version>2.0.0</version>      </dependency>

applicationContext-cache-memcached.xml

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

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

原文  http://hanqunfeng.iteye.com/blog/2175075

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1421067911140.html
缓存服务器 memcached