V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
WhereverYouGo
V2EX  ›  Java

SpringBoot RedisTemplate 如何缓存类?

  •  
  •   WhereverYouGo · 2020-04-10 14:09:14 +08:00 · 2821 次点击
    这是一个创建于 1467 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    21 条回复    2020-04-13 12:03:39 +08:00
    Resource
        1
    Resource  
       2020-04-10 14:10:31 +08:00
    正文也能 at 到我
    WhereverYouGo
        2
    WhereverYouGo  
    OP
       2020-04-10 14:12:12 +08:00
    @Resource [笑哭],这也行。你这表情咋发的 0.0
    Resource
        3
    Resource  
       2020-04-10 14:16:06 +08:00
    @sweetsorrow211 #2 Chrome 扩展:v2ex plus
    HonoSV
        4
    HonoSV  
       2020-04-10 14:19:46 +08:00
    哈哈哈哈 楼上什么鬼
    wanacry
        5
    wanacry  
       2020-04-10 14:20:43 +08:00
    liqingcan
        6
    liqingcan  
       2020-04-10 14:25:06 +08:00
    上下文存在 2 个 RedisTemplate,应该只能通过名字来注入了吧
    chendy
        7
    chendy  
       2020-04-10 14:28:09 +08:00
    用`@Qualifer`区分一下喽
    或者构造方法注入 + bean 名字区分,无需注解…(需要 java8,需要编译参数 /spring 插件
    WhereverYouGo
        8
    WhereverYouGo  
    OP
       2020-04-10 14:31:51 +08:00
    @Resource #3 牛皮 奇怪的技能+1
    WhereverYouGo
        9
    WhereverYouGo  
    OP
       2020-04-10 14:59:20 +08:00
    散了吧散了吧
    ValueOperations<String, Object> operations = redisTemplate.opsForValue();
    可以直接操作 Object
    EminemW
        10
    EminemW  
       2020-04-10 15:10:44 +08:00   ❤️ 1
    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
        11
    EminemW  
       2020-04-10 15:18:23 +08:00
    我是自己封装了 RedisUtil 来用,
    EminemW
        12
    EminemW  
       2020-04-10 15:19:05 +08:00
    //你注入的时候应该是这样
    @Autowired
    private RedisTemplate redis0;

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

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

    redisTemplate.setKeySerializer(xxx);
    redisTemplate.setValueSerializer(xxx);
    1424659514
        18
    1424659514  
       2020-04-11 09:18:58 +08:00
    学到了
    liujan
        19
    liujan  
       2020-04-13 10:24:19 +08:00 via iPhone
    @freezhan 我用的类是一个第三方库中的,该类没有实现序列化接口,属性也一样。这种情况有办法吗
    WhereverYouGo
        20
    WhereverYouGo  
    OP
       2020-04-13 10:29:57 +08:00
    @liujan #19 用这个试试
    @ Resource
    private RedisTemplate redisTemplate;
    ValueOperations<String, Object> operations = redisTemplate.opsForValue();
    我没 implements Serializable 也可以的
    liujan
        21
    liujan  
       2020-04-13 12:03:39 +08:00 via iPhone
    @sweetsorrow211 好,我试试,多谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5485 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 06:57 · PVG 14:57 · LAX 23:57 · JFK 02:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.