Spring Cache集成Memcached(3)

@SuppressWarnings("unchecked")
 @Override
 public <T> T get(Object key, Class<T> 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配置:


<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns='https://www.springframework.org/schema/beans'
 xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'
 xmlns:context='https://www.springframework.org/schema/context'
 xmlns:c='http://www.springframework.org/schema/c'
 xmlns:p='https://www.springframework.org/schema/p'
 xmlns:cache='https://www.springframework.org/schema/cache'
 xmlns:aop=""
 xsi:schemaLocation='
 
       
       
       
        '>
       
 <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
 <cache:annotation-driven cache-manager="cacheManager" />
 <bean />

<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
 <bean>
  <property>
   <set>
    <bean>
     <property ref="memcachedCacheClient" />
    </bean>
   </set>
  </property>
 </bean>

<!-- memcached client defaultCache -->
 <bean>
  <property>
   <bean
    />
  </property>
  <property>
   <bean>
    <property value="192.168.1.90:11211" />
   </bean>
  </property>
  <property>
   <bean>
    <property value="true" />
   </bean>
  </property>
  <property value="userCache" />
 </bean>

</beans>

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

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