spring: 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 # 获取连接最大等待时间
你可能想看: