SpringBoot RedisTemplate 如何缓存类?

2020-04-10 14:09:14 +08:00
 WhereverYouGo

期望同一个 Service 中注入两个 RedisTemplate,redis1 用来操作 String,redis2 用来操作 Object 。如何实现?
@Resource
private RedisTemplate redis1;
@Resource
private RedisTemplate<String, Object> redis2;

2839 次点击
所在节点    Java
21 条回复
Resource
2020-04-10 14:10:31 +08:00
正文也能 at 到我
WhereverYouGo
2020-04-10 14:12:12 +08:00
@Resource [笑哭],这也行。你这表情咋发的 0.0
Resource
2020-04-10 14:16:06 +08:00
@sweetsorrow211 #2 Chrome 扩展:v2ex plus
HonoSV
2020-04-10 14:19:46 +08:00
哈哈哈哈 楼上什么鬼
wanacry
2020-04-10 14:20:43 +08:00
liqingcan
2020-04-10 14:25:06 +08:00
上下文存在 2 个 RedisTemplate,应该只能通过名字来注入了吧
chendy
2020-04-10 14:28:09 +08:00
用`@Qualifer`区分一下喽
或者构造方法注入 + bean 名字区分,无需注解…(需要 java8,需要编译参数 /spring 插件
WhereverYouGo
2020-04-10 14:31:51 +08:00
@Resource #3 牛皮 奇怪的技能+1
WhereverYouGo
2020-04-10 14:59:20 +08:00
散了吧散了吧
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
可以直接操作 Object
EminemW
2020-04-10 15:10:44 +08:00
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
* Redis 配置
*
*/
@Slf4j
@Configuration
public class RedisConfig {

/**
* 配置 lettuce 连接池
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
public GenericObjectPoolConfig redisPool() {
return new GenericObjectPoolConfig<>();
}

/**
* 配置数据源
* @return
*/

@Bean("redisConf")
@Primary
@ConfigurationProperties(prefix = "spring.redis")
public RedisStandaloneConfiguration redisConfig() {
return new RedisStandaloneConfiguration();
}

@Bean("redis1Conf")
@ConfigurationProperties(prefix = "spring.redis1")
public RedisStandaloneConfiguration redisConfig1() {
return new RedisStandaloneConfiguration();
}

/**
* 配置连接工厂
* @param poolConfig
* @param redisConfig
* @return
*/

@Bean("factory")
@Primary
public LettuceConnectionFactory factory(GenericObjectPoolConfig poolConfig,@Qualifier("redisConf") RedisStandaloneConfiguration redisConfig) {
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build();
return new LettuceConnectionFactory(redisConfig, clientConfiguration);
}

@Bean("factory1")
public LettuceConnectionFactory factory1(GenericObjectPoolConfig poolConfig,@Qualifier("redis1Conf") RedisStandaloneConfiguration redisConfig) {
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build();
return new LettuceConnectionFactory(redisConfig, clientConfiguration);
}


/**
* 默认 redis0
* @param connectionFactory
* @return
*/
@Bean
@Primary
public StringRedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory) {
return getRedisTemplate(connectionFactory);
}

@Bean("redisTemplate1")
public StringRedisTemplate redisTemplate1(@Qualifier("factory1")LettuceConnectionFactory factory) {
return getRedisTemplate(factory);
}

@Bean
@Primary
public RedisUtil redis0Util(StringRedisTemplate redisTemplate) {
return new RedisUtil(redisTemplate);
}

@Bean("redis1")
public RedisUtil redis1Util(@Qualifier("redisTemplate1") StringRedisTemplate redisTemplate) {
return new RedisUtil(redisTemplate);
}

private StringRedisTemplate getRedisTemplate(LettuceConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(factory);
template.afterPropertiesSet();
return template;
}

}


//注入的时候
@Autowired
private RedisTemplate redis0;

@Autowired
@Qualifier("redis1")
private RedisTemplate redis1;
EminemW
2020-04-10 15:18:23 +08:00
我是自己封装了 RedisUtil 来用,
EminemW
2020-04-10 15:19:05 +08:00
//你注入的时候应该是这样
@Autowired
private RedisTemplate redis0;

@Autowired
@Qualifier("redisTemplate1")
private RedisTemplate redis1;
WhereverYouGo
2020-04-10 15:23:15 +08:00
@EminemW #12 老哥,稳....我研究下,感谢铁汁!
soulzz
2020-04-10 15:46:44 +08:00
@Autowired 进来直接用 redis 会死人的
昨天改代码到 2 点发现了一个问题
在多线程中直接用 RedisTemplate 测下来的结果还是单线程的耗时累加
建议用 redis 前考虑好使用场景,修改后再存数据库的基本就可以改用 guava,几行代码就解决很多的问题
所以这里强推谷歌的 guava,强的一批

至于你问的问题 @Bean("名字")再加 @Qualifer("名字")就解决了
Kyle18Tang
2020-04-10 16:13:38 +08:00
操作 String 默认不是有个 StringRedisTemplate 吗...自动配置里面本来就设置了 2 个 Template 的, 见 RedisAutoConfiguration 类.
liujan
2020-04-10 18:35:03 +08:00
顺便问下,对于那些没有实现序列化接口的类,有办法缓存到 redis 中吗
freezhan
2020-04-10 20:32:59 +08:00
@liujan 自定义 Json 序列化啊

redisTemplate.setKeySerializer(xxx);
redisTemplate.setValueSerializer(xxx);
1424659514
2020-04-11 09:18:58 +08:00
学到了
liujan
2020-04-13 10:24:19 +08:00
@freezhan 我用的类是一个第三方库中的,该类没有实现序列化接口,属性也一样。这种情况有办法吗
WhereverYouGo
2020-04-13 10:29:57 +08:00
@liujan #19 用这个试试
@ Resource
private RedisTemplate redisTemplate;
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
我没 implements Serializable 也可以的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/661146

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX