SpringBoot RedisTemplate 如何缓存类? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Masonnn
V2EX    Java

SpringBoot RedisTemplate 如何缓存类?

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

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

    21 条回复    2020-04-13 12:03:39 +08:00
    Resource
        1
    Resource  
       2020-04-10 14:10:31 +08:00
    正文也能 at 到我
    Masonnn
        2
    Masonnn  
    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 插件
    Masonnn
        8
    Masonnn  
    OP
       2020-04-10 14:31:51 +08:00
    @Resource #3 牛皮 奇怪的技能+1
    Masonnn
        9
    Masonnn  
    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, clienConfiguration);
    }


    /**
    * 默认 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;
    Masonnn
        13
    Masonnn  
    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 我用的类是一个第三方库中的,该类没有实现序列化接口,属性也一样。这种情况有办法吗
    Masonnn
        20
    Masonnn  
    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     Solana     908 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 22:57 PVG 06:57 LAX 14:57 JFK 17:57
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86