img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

由于代码中用了布隆过滤器,因此这里引用了hutool工具包

        <!-- redis的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

<!-- hutool工具包-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.11</version>
        </dependency>

2.核心配置类

核心是存对象的Redistemplate配置,

还包括了Redisson的配置,lua脚本的配置。

package com.tianju.fresh.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.redisson.config.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


/\*\*
 \* Redisson的配置,lua脚本的配置,redis序列化存对象的配置
 \*/
@Configuration
public class RedisConfig {

    /\*\*
 \* Redisson的配置,
 \* Redisson框架,分布式环境下redis数据一致性
 \* @return
 \*/
    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379")
                .setPassword("5672");
        return Redisson.create(config);
    }

    /\*\*
 \* lua脚本的配置类,让减库存-1操作原子化
 \* @return
 \*/
    @Bean
    public RedisScript<Long> redisScript(){
        DefaultRedisScript redisScript = new DefaultRedisScript<>();
        redisScript.setResultType(Long.class);
        // lua脚本的位置
        redisScript.setLocation(
                new ClassPathResource("/lua/goods-unstock.lua") // 关联lua脚本
        );
        return redisScript;
    }

    /\*\*
 \* redis序列化的相关配置,可以存对象
 \* @return
 \*/
    @Bean
    public RedisTemplate redisTemplateInit(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置序列化Key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        /\*\*
 \*
 \* 设置Hash类型存储时,对象序列化报错解决
 \*/
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

}


3.封装RedisUtil工具类

在实际应用redis中形成的自定义的Redis工具类,主要把Redistemplate 和 StringRedistemplate 依赖注入,然后调用这两个的方法;

其中Redistemplate 用来存java对象,而StringRedistemplate 用于普通的操作;

package com.tianju.fresh.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;


@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void  saveObjectToRedis(String key,Object json){
        redisTemplate.opsForValue().set(key,json);
    }

    /\*\*
 \* 从redis里面获取json对象,如果没有,返回null
 \* @param key
 \* @return
 \*/
    public Object getJsonFromRedis(String key){
        return redisTemplate.opsForValue().get(key);
    }

    /\*\*
 \* 删除redis里面的值,键
 \* @param key 删除的键
 \* @return
 \*/
    public Boolean deleteKey(String key){
        return redisTemplate.delete(key);
    }

    /\*\*
 \* 存到Redis里面的 set 中
 \* @param key 存的键
 \* @param value 存到set的值
 \*/
    public void saveSetToRedis(String key,String... value){
        for (String s:value){
            if (s!=null && !s.equals("")){
                stringRedisTemplate.opsForSet().add(key, s);
            }
        }
    }

    /\*\*
 \* 判断是否在redis的set中
 \* @param key
 \* @param val
 \* @return
 \*/
    public Boolean isInSet(String key,String val){
        return stringRedisTemplate.opsForSet().isMember(key, val);
    }

    /\*\*
 \* 获得set中的值
 \* @param key 键
 \* @return
 \*/
    public Set<String> getSet(String key){
        return stringRedisTemplate.opsForSet().members(key);
    }

    /\*\*
 \* 从redis里面的set删除数据
 \* @param key
 \* @param val
 \*/
    public void removeFromSet(String key,String val){
        stringRedisTemplate.opsForSet().remove(key, val);
    }



    /\*\*
 \* 获得存到redis里面的键对应的string类型的值
 \* @param key 键
 \* @return
 \*/
    public String getStringValue(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    /\*\*
 \* 保存到redis里面string
 \* @param key
 \* @param timeout 过期时间,传的是多少s之后过期
 \* @param val
 \*/
    public void saveStringValue(String key,String val,Integer... timeout){
        if (timeout==null){
            stringRedisTemplate.opsForValue().set(key,val);
        }else {
            stringRedisTemplate.opsForValue().set(key,val,timeout[0], TimeUnit.SECONDS);
        }
    }

    /\*\*
 \* 看某个键是否存在于Redis中
 \* @param key 待检验的键
 \* @return
 \*/
    public Boolean isKeyInRedis(String key){
        return stringRedisTemplate.hasKey(key);
    }

}


4.工具类的使用

下面以一个缓存预热的代码为例,使用工具类时依赖注入,然后调用工具类里面的方法

package com.tianju.fresh.autho.bloomFilter;

import com.tianju.fresh.entity.Customer;
import com.tianju.fresh.mapper.CustomerMapper;
import com.tianju.fresh.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/\*\*
 \* 将用户名,手机号码,邮箱存到redis里面,
 \* 作用:
 \* 1.登陆的时候先到redis里面查询;
 \* 2.用户注册的时候先从redis里面看用户名是否重复
 \*/
@Component
@Slf4j
public class LoginKeyPreHot {

    private final String LOGIN\_KEY = "loginKey";

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private RedisUtil redisUtil;

    /\*\*
 \* 每天半夜 2点 跟新布隆过滤器和缓存
 \*/
    @Scheduled(cron = "0 0 2 \* \* ?")
    public void perHot(){
        // 1.先清除缓存中的数据
        // 2.然后更新一下redis里面的loginKey;


![img](https://img-blog.csdnimg.cn/img_convert/f916191cf99a1d02b71f1ff4535d7933.png)
![img](https://img-blog.csdnimg.cn/img_convert/10bbc41c7a0c7d80f7b1638665f45c60.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

K2oGGU-1715282657775)]
[外链图片转存中...(img-1fd3eayc-1715282657775)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

更多推荐