spring-data-redis集成的两种方式

     spring-data-redis集成有两种方式:一种是Java代码集成;另一种是xml方式集成。首先是xml集成方式:

<!-- redis缓存配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
   <property name="maxIdle" value="${redis.cache.MaxIdle}" />
   <property name="minIdle" value="${redis.cache.MinIdle}"/>
   <property name="maxTotal" value="${redis.cache.MaxTotal}" />
   <property name="maxWaitMillis" value="${redis.cache.MaxWaitMillis}" />
   <property name="testOnBorrow" value="true" />
</bean>

<bean id= "clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode" >
   <constructor-arg value="${redis.host}" />
   <constructor-arg value="${redis.port}" type="int" />
</bean>

<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration" >
   <property name="maxRedirects" value="${redis.cache.MaxRedirects}" >
   </property>
   <property name="clusterNodes" >
      <set>                           //这里可放置多个set,每个set就是一个节点
         <ref bean="clusterRedisNodes1" />  
      </set>
   </property>
</bean>

<bean id="jedisConnectionFactory"
     class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
   <constructor-arg index="0" ref="redisClusterConfiguration" />
   <constructor-arg index="1" ref="jedisPoolConfig"/>
</bean >



<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
   <property name="connectionFactory"   ref="jedisConnectionFactory" />

</bean>

<bean id="keyGenerator" class="com.wisely.util.DefaultKeyGenerator"/>


<!-- cache manager -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
   <constructor-arg name="redisOperations" ref="redisTemplate" />   //这一行至关重要,当时这里花费了好多时间
   <property name="defaultExpiration" value="${redis.cache.DefaultExpiration}"/>
</bean>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager="cacheManager" key-generator="keyGenerator"/>
一下是keyGenerator的代码:

public class DefaultKeyGenerator implements KeyGenerator {

    @Override
    public Object generate(Object o, Method method, Object... objects) {
        StringBuilder sb = new StringBuilder();
        sb.append(o.getClass().getName());
        sb.append(method.getName());
        for (Object obj : objects) {
            sb.append(obj.toString());
        }
        return sb.toString();
    }
}
当配置好了以上后,剩下就是封装具体的redis  api模板了,你根据自己项目的需要封装,把节点信息注入模板中就可以使用了。

    第二种是Java代码集成方式:

@Configuration
@EnableCaching
@ComponentScan(basePackages = {"com.wisely.persistence.repository", "com.wisely.service"})
public class RedisCacheConfig extends CachingConfigurerSupport {

    private static final String SPLIT_COLON = ":";
    @Resource
    private Config config; //这里引用的包为com.typesafe.config.Config;

    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        String redisUrl = config.getString("redis.host") + SPLIT_COLON + config.getString("redis.port");
        RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(Arrays.asList(redisUrl));
        clusterConfig.setMaxRedirects(config.getInt("redis.cache.MaxRedirects"));
        JedisPoolConfig poolConfig = new JedisPoolConfig();    
         poolConfig.setMaxWaitMillis(config.getInt("redis.cache.MaxWaitMillis"));// 获取连接时的最大等待毫秒数
        poolConfig.setMaxTotal(config.getInt("redis.cache.MaxTotal")); // 最大连接数
         poolConfig.setMinIdle(config.getInt("redis.cache.MinIdle")); // 最小空闲连接数
         poolConfig.setMaxIdle(config.getInt("redis.cache.MaxIdle")); // 最大空闲连接数
         return new JedisConnectionFactory(clusterConfig, poolConfig);

    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
        RedisTemplate<String, String> redis = new RedisTemplate<String, String>();
        redis.setConnectionFactory(cf);
        return redis;
    }

    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(o.getClass().getName());
                sb.append(method.getName());
                for (Object obj : objects) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setDefaultExpiration(config.getLong("redis.cache.DefaultExpiration"));
        return cacheManager;
    }

}


©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页