Spring Cache集成Redis(2)

@Override
 public void clear() {
  redisTemplate.execute(new RedisCallback<String>() {
   public String doInRedis(RedisConnection connection) throws DataAccessException {
    connection.flushDb();
    return "ok";
   }
  });
 }

@SuppressWarnings("unchecked")
 @Override
 public <T> T get(Object key, Class<T> type) {
  final String keyf = (String) key;
  Object object = null;
  object = redisTemplate.execute(new RedisCallback<Object>() {
   public Object doInRedis(RedisConnection connection) throws DataAccessException {

byte[] key = keyf.getBytes();
    byte[] value = connection.get(key);
    if (value == null) {
     return null;
    }
    return toObject(value);
   }
  });
  return (T) object;
 }

@Override
 public ValueWrapper putIfAbsent(Object key, Object value) {
  put(key, value);
  return new SimpleValueWrapper(value);
 }
}
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:cache="http://www.springframework.org/schema/cache"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans


">

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

<bean />
 
    <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value --> 
    <bean> 
        <property> 
            <set> 
                <bean> 
                    <property ref="redisTemplate" /> 
                    <property value="userCache"/> 
                </bean> 
            </set> 
        </property> 
    </bean>
   
    <!-- redis 相关配置 --> 
    <bean> 
        <property value="300" />       
        <property value="3000" /> 
        <property value="true" /> 
    </bean> 
    <bean 
        
        p:host-name="192.168.1.90" p:port="6379" p:pool-config-ref="poolConfig" 
        p:database="0" />
    <bean> 
        <property ref="connectionFactory" /> 
    </bean> 
</beans>
除了以上方式,在spring-data项目中增加了对redis的支持spring-data-redis,单独引入依赖:


<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-redis</artifactId>
   <version>1.4.2.RELEASE</version>
  </dependency>使用spring-data-redis提供的RedisTemplate对redis操作,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:c='http://www.springframework.org/schema/c'
 xmlns:p='http://www.springframework.org/schema/p'
 xmlns:cache='http://www.springframework.org/schema/cache'
 xsi:schemaLocation='
  http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/cache/spring-cache.xsd
        '>

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

<bean />

<!-- 声明cacheManager -->
 <bean
  c:template-ref="redisTemplate" />

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

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