掘金 后端 ( ) • 2024-05-27 14:44

在线商城项目后端地址crazyshopping-back-end-gitee: crazyshopping后端

技术栈 springboot3.1.4 redis3

在自己进行压力测试的过程中,发现当数据库中引入一万张图片的时候,网站显示所有商品会有明显的延迟,现在引入redis数据库,对热点数据,所有商品信息进行缓存,便于高并发环境下用户的访问。

但是在网上学习的过程中,发现了许多问题,例如,网上找到的许多代码已经不适于新版本的spring,所以在此整理一下springboot整合redis的配置过程及运行结果。

在application.properties中引入如下配置

spring.data.redis.host=127.0.0.1  
spring.data.redis.port=6379  
spring.data.redis.database=0  
spring.data.redis.lettuce.pool.max-idle=16  
spring.data.redis.lettuce.pool.max-active=8  
spring.data.redis.lettuce.pool.min-idle=8

在pom.xml中引入如下配置

<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>  
<dependency>  
<groupId>org.apache.commons</groupId>  
<artifactId>commons-pool2</artifactId>  
</dependency>

至此,基本的配置完成,下面是代码配置及使用

建立RedisConfig配置类

@Configuration  
public class RedisConfig {  
    @Bean  
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {  
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();  
        redisTemplate.setConnectionFactory(factory);  
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();  
        redisTemplate.setKeySerializer(stringRedisSerializer); // key的序列化类型  
        ObjectMapper om = new ObjectMapper();  
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,  
        ObjectMapper.DefaultTyping.NON_FINAL);  
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om);  
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // value的序列化类型  
        redisTemplate.setHashKeySerializer(stringRedisSerializer);  
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);  
        redisTemplate.afterPropertiesSet();  
        return redisTemplate;  
    }  
}

建立ReidsUtil工具类

下面只给出目前在使用的核心方法,其余代码可在网上自行查阅redis工具类的代码配置

public class RedisUtil {  
  
    @Autowired  
    private RedisTemplate<String,Object> redisTemplate;  
    public void setGoodsList(String key, List<Goods> goodsList) {  
    redisTemplate.opsForValue().set(key, goodsList);  
    }  
  
    public List<Goods> getGoodsList(String key) {  
    return (List<Goods>) redisTemplate.opsForValue().get(key);  
    }  
  
    public void deleteGoodsList(String key) {  
    redisTemplate.delete(key);  
    }
    
    public boolean expire(String key, long time) {  
    return redisTemplate.expire(key, time, TimeUnit.SECONDS);}
    
    public Object get(String key) {  
    return key == null ? null : redisTemplate.opsForValue().get(key);  
    }
    
    public void set(String key, Object value) {  
    redisTemplate.opsForValue().set(key, value);  
    }
    
}

然后在我们后端的核心获取商品信息代码中整合redis

public RespGoodsBean getAllGoods() {  
    String key = "allGoods";  //建立key
    List<Goods> goodsList = redisUtil.getGoodsList(key);//先查询redis中有没有全部商品信息  
    Integer total = 0;  
    if (goodsList == null) {  //没有全部商品的话就从数据里中获取全部商品
    goodsList = goodsMapper.selectList(null);  
    total = Math.toIntExact(goodsMapper.selectCount(null));  
  
    for (Goods goods : goodsList) {  
    this.setGoodsImgNameList(goods);  //插入商品图片分类
    this.setSort(goods);  
    }  
  
    redisUtil.setGoodsList(key, goodsList);  //将商品信息缓存进入redis数据库
    redisUtil.expire(key,1); } 
    else {  
    total = goodsList.size();  
}  
  
    RespGoodsBean respGoodsBean = new RespGoodsBean();  
    respGoodsBean.setGoodsList(goodsList);  
    respGoodsBean.setTotal(total);  
    return respGoodsBean;  
}

成功配置redis