碰上获取redis不同库的信息,首先想到配置多个redisTemplate连接,可能人笨了点花费几个小时也没有成功,然后只能想到最土的方法封装一个redis的连接类,每次调用redis之前切库,直接上代码:
redis配置类
package com.steward.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.JedisPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import java.lang.reflect.Method;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
@Bean
public KeyGenerator wiselyKeyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@Bean
public CacheManager cacheManager(
@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
@Bean
public RedisTemplate<String, String> redisTemplate(
RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
测试类
package com.steward.controller.redis;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
private static Logger logger=Logger.getLogger(RedisController.class);
@Autowired
private RedisTemplate<String, String> redisTemplate;
@PostMapping("/users/redisTst/{random}")
public @ResponseBody String redisTst() {
redisTemplate=initRedis(12,redisTemplate);
String redis12=redisTemplate.opsForValue().get("00001");
logger.info("redis12 : "+redis12);
redisTemplate=initRedis(5,redisTemplate);
String redis5= redisTemplate.opsForValue().get("39E40B10-5EE9-431B-A015-5ECDA7AFE173");
logger.info("redis5 : "+redis5);
redisTemplate=initRedis(12,redisTemplate);
String redis12_2=redisTemplate.opsForValue().get("00001");
logger.info("redis12_2 : "+redis12_2);
return "success";
}
//自己改进后的封装
/**
* redis 数据库切换
*
*/
public static RedisTemplate<String, String> initRedis(Integer indexDb, RedisTemplate<String, String> redisTemplate){
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName("192.168.0.71");
redisConnectionFactory.setPort(6379);
redisConnectionFactory.setDatabase(indexDb);
redisConnectionFactory.setPassword("klcgj");
redisConnectionFactory.afterPropertiesSet();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
结果:
redis 库切换成功,也能获取数据