当前位置:首页 > Java 框架原理百科 > 正文

Java优学网SpringBoot整合Redis教程:轻松实现商品信息缓存,提升应用性能

Java优学网SpringBoot整合Redis教程:轻松实现商品信息缓存,提升应用性能

spring: redis:

Java优学网SpringBoot整合Redis教程:轻松实现商品信息缓存,提升应用性能

host: localhost
port: 6379
password: 
database: 0
timeout: 2000ms

@Service public class ProductService {

// 商品信息获取逻辑
public Product getProductById(Long id) {
    String cacheKey = "product:" + id;
    String cached = redisTemplate.opsForValue().get(cacheKey);
    if (cached != null) {
        return JSON.parseObject(cached, Product.class);
    }
    
    // 缓存未命中,查询数据库
    Product product = productMapper.selectById(id);
    if (product != null) {
        redisTemplate.opsForValue().set(cacheKey, 
            JSON.toJSONString(product), 30, TimeUnit.MINUTES);
    }
    return product;
}

}

spring: redis:

lettuce:
  pool:
    max-active: 20    # 最大连接数
    max-idle: 10      # 最大空闲连接
    min-idle: 5       # 最小空闲连接
    max-wait: 2000ms  # 获取连接最大等待时间

Java优学网SpringBoot整合Redis教程:轻松实现商品信息缓存,提升应用性能

你可能想看:

相关文章:

文章已关闭评论!