Memcached结合Spring

2016-08-14  本文已影响745人  米刀灵

使用simple-spring-memcached
在POM中添加:

<dependencies>
   <dependency>
     <groupId>com.google.code.simple-spring-memcached</groupId>
     <artifactId>xmemcached-provider</artifactId>
     <version>3.6.1</version>
   </dependency> 
</dependencies>

在Spring配置文件中添加:

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  <import resource="simplesm-context.xml" />
  <aop:aspectj-autoproxy />
  /*simplesm-context.xml封装在simple-spring-memcached-*.jar文件当中,主要用来加载组件核心的Advice,供程序调度使用。
  而由于simple-spring-memcached主要是基于AOP的代理,所以加入<aop:aspectj-autoproxy />让代理机制起到作用。*/

  <bean name="defaultMemcachedClient" 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="127.0.0.1:11211" />
            </bean>
      </property>
      <property name="configuration">
            <bean class="com.google.code.ssm.providers.CacheConfiguration">
                  <property name="consistentHashing" value="true" />
            </bean>
      </property>
   </bean>
</beans>

接下来就可以使用注解读写缓存了:

public class UserDaoImpl implements IUserDao {  
    private static final String NAMESPACE="user";  
    private Map<String,User> users=new HashMap<String,User>();  
    @Override  
    public void saveUser(User user) {  
        users.put(user.getUserId(), user);  
    }  
    /** 
     * 当执行getById查询方法时,系统首先会从缓存中获取userId对应的实体 
     * 如果实体还没有被缓存,则执行查询方法并将查询结果放入缓存中 
     */  
    @Override  
    @ReadThroughSingleCache(namespace = NAMESPACE, expiration = 3600)  //3600秒=1小时
    public User getById(@ParameterValueKeyProvider String userId) {  
        System.out.println(userId);  
        return users.get(userId);  
    }  
    /** 
     * 当执行updateUser方法时,系统会更新缓存中userId对应的实体 
     * 将实体内容更新成@*DataUpdateContent标签所描述的实体 
     */  
    @UpdateSingleCache(namespace = NAMESPACE, expiration = 3600)  
    @Override  
    public void updateUser(@ParameterValueKeyProvider @ParameterDataUpdateContent User user) {  
        users.put(user.getUserId(), user);  
    }  
    /** 
     * 当执行deleteUser方法时,系统会删除缓存中userId对应的实体 
     */  
    @InvalidateSingleCache(namespace = NAMESPACE)  
    @Override  
    public void deleteUser(@ParameterValueKeyProvider String userId) {  
        users.remove(userId);  
    }  
}  
SingleCache MultiCache

和Spring Cache结合
于Ehcache一样,Memcached也可以和Spring Cache结合使用
在POM中添加:

<dependency>
  <groupId>com.google.code.simple-spring-memcached</groupId>
  <artifactId>spring-cache</artifactId>
  <version>3.6.1</version>
</dependency>
<dependency>
  <groupId>com.google.code.simple-spring-memcached</groupId>
  <artifactId>xmemcached-provider</artifactId>
  <version>3.6.1</version>
</dependency>   

在Spring配置文件中添加:

<?xml version="1.0" encoding="UTF-8"?>
<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:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/cache 
           http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

  <cache:annotation-driven />

  <bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
    <property name="caches">
      <set>
        <bean class="com.google.code.ssm.spring.SSMCache">
      <constructor-arg name="cache" index="0" ref="defaultCache" />
          <!-- 5 minutes -->
      <constructor-arg name="expiration" index="1" value="300" />
          <!-- @CacheEvict(..., "allEntries" = true) won't work because allowClear is false, 
           so we won't flush accidentally all entries from memcached instance -->
      <constructor-arg name="allowClear" index="2" value="false" />
    </bean>
      </set>
    </property>
  </bean>

  <bean name="defaultCache" class="com.google.code.ssm.CacheFactory" depends-on="cacheBase">
    <property name="cacheName" value="default" />
    <property name="cacheClientFactory">
      <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
    </property>
    <property name="addressProvider">
      <bean class="com.google.code.ssm.config.DefaultAddressProvider">
        <property name="address" value="127.0.0.1:11211" />
      </bean>
    </property>
    <property name="configuration">
      <bean class="com.google.code.ssm.providers.CacheConfiguration">
        <property name="consistentHashing" value="true" />
        <!-- spring can produce keys that contain unacceptable characters -->
        <property name="useBinaryProtocol" value="true" />
      </bean>
    </property>
  </bean>
</beans>

然后就可以使用Spring Cache的注解了。


参考:
https://github.com/ragnor/simple-spring-memcached/wiki/Getting-Started
http://greemranqq.iteye.com/blog/2168710
http://www.myexception.cn/software-architecture-design/414190.html
http://blog.csdn.net/javaman_chen/article/details/7682290

上一篇 下一篇

猜你喜欢

热点阅读