Spring-data-redis統合の2つの方法
17506 ワード
Spring-data-redis統合には2つの方法があります.1つはJavaコード統合です.もう1つはxml方式の統合です.まずxml統合方式です.
次はkeyGeneratorのコードです.
以上の構成が完了すると、残りは具体的なredis apiテンプレートをカプセル化し、自分のプロジェクトの必要に応じてカプセル化し、ノード情報をテンプレートに注入すれば使用できます.
2つ目はJavaコードの統合方法です.
<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"/>
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
// ,
<property name="defaultExpiration" value="${redis.cache.DefaultExpiration}"/>
bean>
<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テンプレートをカプセル化し、自分のプロジェクトの必要に応じてカプセル化し、ノード情報をテンプレートに注入すれば使用できます.
2つ目は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 redisTemplate(RedisConnectionFactory cf) {
RedisTemplate redis = new RedisTemplate();
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;
}
}