springboot2.x +redis使用和源码分析二(RedisTemplate)

本文详细介绍了SpringBoot中RedisTemplate的构建及功能,包括基本使用、Pipelined支持、事务处理、Lua语言集成以及RedisTemplate在消息队列中的应用。通过示例代码展示了如何利用RedisTemplate实现高效、原子性的操作,并探讨了RedisTemplate在分布式锁和秒杀场景的应用。
摘要由CSDN通过智能技术生成

目录

序言:本文讲述RedisTemplate对象如何构建以及该对象对于redis提供的功能的支持

1:定义RedisTemplate

2:基本使用Ddemo

3:RedisTemplate对Pipelined支持

4:RedisTemplate对事务支持

5:RedisTemplate对Lua语言支持

5.1:使用内置字符串形式

5.2:以文件的形式

5.3:使用Lua的一些应用场景

6:RedisTemplate对于redis中MQ功能支持

6.1:简单demo代码

6.2:走读MessageListenerAdapter源码

6.3:redis-MQ应用场景

 Demo代码:https://github.com/fangyuan94/redisDemo


6.2:走读MessageListenerAdapter源码

 Demo代码:https://github.com/fangyuan94/redisDemo


序言:本文讲述RedisTemplate对象如何构建以及该对象对于redis提供的功能的支持

在实际需求中我们会将用户的基础信息存放到redis作为缓存,在项目中我们定义PersonInfo用于存储用户信息

 

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PersonInfo implements Serializable {

    private static final long serialVersionUID = -5666930682610937456L;

    @NotNull
    private String userId;

    @NotNull
    private String name;

    @Max(100)
    private Integer age;

    @NotNull
    private String sex;
}

如何优雅的操作person信息

1:定义RedisTemplate

@Configuration
@AutoConfigureAfter(RedisCustomizerConfiguration.class)
public class RedisConfiguration {

    @Bean
    public RedisTemplate<String, PersonInfo> personInfoRedisTemplate(ObjectProvider<RedisConnectionFactory> redisConnectionFactory){

        RedisTemplate<String, PersonInfo> personInfoRedisTemplate = new RedisTemplate<String, PersonInfo>();

        personInfoRedisTemplate.setConnectionFactory(redisConnectionFactory.getObject());
        //字符串序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //设置JDK序列化器
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(PersonInfo.class.getClassLoader());
        //设置key value序列化器
        personInfoRedisTemplate.setKeySerializer(stringRedisSerializer);
        personInfoRedisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
        personInfoRedisTemplate.setHashKeySerializer(stringRedisSerializer);
        personInfoRedisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);

        return personInfoRedisTemplate;
    }
}

2:基本使用Ddemo

    //向redis中写数据(五种基本类型操作,和redis命令行操作基本一致)
    @RequestMapping("addPersonInfo")
    public Map<String,Object> addPersonInfo(){

        String key = "personInfo";
        PersonInfo personInfo = new PersonInfo();
        personInfo.setUserId("1");
        personInfo.setAge(18);
        personInfo.setName("小花");
        personInfo.setSex("女");
        //操作string
        personInfoRedisTemplate.opsForValue().set(key+"_str",personInfo);
        //操作list
        personInfoRedisTemplate.opsForList().leftPush(key+"list",personInfo);
        //操作set
        personInfoRedisTemplate.opsForSet().add(key+"_set",personInfo);
        //操作有序set
        personInfoRedisTemplate.opsForZSet().add(key+"_ZSet",personInfo,100);
        //操作hash散列
        personInfoRedisTemplate.opsForHash().put(key+"_Map",personInfo.getUserId(),personInfo);

        Map<String,Object> map = new HashMap<>(3);
        map.put("success",true);
        map.put("data","1");
        return map;
    }

    @RequestMapping("getPersonInfo")
    public Map<String,Object> getPersonInfo(){

        String key = "personInfo";

        //操作string
        PersonInfo personInfo1 = personInfoRedisTemplate.opsForValue().get(key+"_str");
        //操作list
        PersonInfo personInfo2 = personInfoRedisTemplate.opsForList().leftPop(key+"list");
        //操作set
        PersonInfo personInfo3 =personInfoRedisTemplate.opsForSet().pop(key+"_set");
        //操作有序set
        Set<PersonInfo> personInfos = personInfoRedisTemplate.opsForZSet().range(key+"_ZSet",0,100);
        //操作hash散列
        PersonInfo personInfo5 = (PersonInfo) personInfoRedisTemplate.opsForHash().get(key+"_Map","1");

        Map<String,Object> map = new HashMap<>(3);
        map.put("str",personInfo1);
        map.put("list",personInfo2);
        map.put("set",personInfo3);
        map.put("zSet",personInfos);
        map.put("hash",personInfo5);
        return map;
    }

3:RedisTemplate对Pipelined支持

使用redis中pipelined可以优化批量处理需求的性能,不过pipelined不具有原子性,当执行到某一条命令时失败时会丢弃此条命令

测试Demo:

/**
     * RedisTemplate 对于pipeline支持
     * 提供SessionCallback与RedisCallback两种 作用一样
     * @return
     */
    @RequestMapping("pipelineTest")
    public  Map<String,Object> pipelineTest(){

        //测试数据
       final List<PersonInfo> personInfosTest = new ArrayList<>();

        for (int i=0;i<50;i++){
            PersonInfo personInfo = new PersonInfo();
            personInfo.setUserId(""+i);
            personInfo.setAge(i);
            personInfo.setName("小花"+i);
            personInfo.setSex("女");
            personInfosTest.add(personInfo);
       }

        final String k
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值