Redis缓存优化:电商系统性能提升方案
缓存商品功能实现
商品信息属于高频访问但低频更新的数据,适合采用缓存策略。Redis作为内存数据库,可显著降低数据库压力。
采用旁路缓存模式(Cache-Aside),优先从缓存读取数据,未命中时查询数据库并回填缓存。商品信息以Hash结构存储,Key设计为product:{category_id},Field为商品ID,Value为商品详情JSON。
// 商品缓存示例代码
public Product getProductWithCache(Long productId) {
String cacheKey = "product:" + productId;
String cachedProduct = redisTemplate.opsForValue().get(cacheKey);
if (cachedProduct != null) {
return JSON.parseObject(cachedProduct, Product.class);
}
Product dbProduct = productMapper.selectById(productId);
if (dbProduct != null) {
redisTemplate.opsForValue().set(cacheKey,
JSON.toJSONString(dbProduct),
30, TimeUnit.MINUTES);
}
return dbProduct;
}
缓存更新采用双删策略保证一致性。先删除缓存再更新数据库,最后延迟再次删除缓存。延迟时间通过消息队列或定时线程池实现,通常设置为500ms-1s。
购物车功能设计
购物车数据具有用户独享、高频读写特性,采用Redis Hash结构存储。Key设计为cart:{user_id},Field为商品ID,Value为商品数量与规格的JSON。
// 购物车操作示例
public void addToCart(Long userId, CartItem item) {
String cartKey = "cart:" + userId;
String itemJson = redisTemplate.opsForHash().get(cartKey, item.getProductId());
if (itemJson != null) {
CartItem existing = JSON.parseObject(itemJson, CartItem.class);
existing.setQuantity(existing.getQuantity() + item.getQuantity());
redisTemplate.opsForHash().put(cartKey, item.getProductId(), JSON.toJSONString(existing));
} else {
redisTemplate.opsForHash().put(cartKey, item.getProductId(), JSON.toJSONString(item));
}
}
采用惰性过期策略,用户每次访问购物车时延长TTL。设置默认过期时间为7天,避免长期未登录用户数据堆积。
性能优化方案
引入多级缓存架构,本地缓存(Caffeine)缓存热点商品,Redis缓存全量商品数据。通过布隆过滤器防止缓存穿透,缓存空值应对商品不存在场景。
购物车实现分片存储,当单个用户购物车商品超过500件时,按商品类别分片到多个Redis Key。采用Lua脚本保证购物车操作的原子性。
-- 购物车数量修改Lua脚本
local key = KEYS[1]
local field = ARGV[1]
local delta = tonumber(ARGV[2])
local current = redis.call('HGET', key, field)
if current then
local item = cjson.decode(current)
item.quantity = item.quantity + delta
redis.call('HSET', key, field, cjson.encode(item))
return item.quantity
else
return 0
end
数据同步策略
商品变更时通过canal监听数据库binlog,发送变更事件到MQ。消费者接收消息后更新缓存,解决业务代码双删遗漏问题。
购物车结算完成后,采用异步写回策略。先快速响应用户,再通过队列将数据持久化到数据库。异常情况通过定时任务补偿,确保最终一致性。
5G.okacbd101.asia/PoSt/1123_239095.HtM
5G.okacbd102.asia/PoSt/1123_868635.HtM
5G.okacbd103.asia/PoSt/1123_610461.HtM
5G.okacbd104.asia/PoSt/1123_528784.HtM
5G.okacbd105.asia/PoSt/1123_238048.HtM
5G.okacbd106.asia/PoSt/1123_505547.HtM
5G.okacbd107.asia/PoSt/1123_801332.HtM
5G.okacbd108.asia/PoSt/1123_561898.HtM
5G.okacbd109.asia/PoSt/1123_927410.HtM
5G.okacbd110.asia/PoSt/1123_404757.HtM
5G.okacbd101.asia/PoSt/1123_384656.HtM
5G.okacbd102.asia/PoSt/1123_861161.HtM
5G.okacbd103.asia/PoSt/1123_465804.HtM
5G.okacbd104.asia/PoSt/1123_824228.HtM
5G.okacbd105.asia/PoSt/1123_565001.HtM
5G.okacbd106.asia/PoSt/1123_114249.HtM
5G.okacbd107.asia/PoSt/1123_973238.HtM
5G.okacbd108.asia/PoSt/1123_883371.HtM
5G.okacbd109.asia/PoSt/1123_842202.HtM
5G.okacbd110.asia/PoSt/1123_506091.HtM
5G.okacbd101.asia/PoSt/1123_900632.HtM
5G.okacbd102.asia/PoSt/1123_544239.HtM
5G.okacbd103.asia/PoSt/1123_096498.HtM
5G.okacbd104.asia/PoSt/1123_949103.HtM
5G.okacbd105.asia/PoSt/1123_405983.HtM
5G.okacbd106.asia/PoSt/1123_646695.HtM
5G.okacbd107.asia/PoSt/1123_154176.HtM
5G.okacbd108.asia/PoSt/1123_358712.HtM
5G.okacbd109.asia/PoSt/1123_124656.HtM
5G.okacbd110.asia/PoSt/1123_259183.HtM
5G.okacbd101.asia/PoSt/1123_528109.HtM
5G.okacbd102.asia/PoSt/1123_757733.HtM
5G.okacbd103.asia/PoSt/1123_746864.HtM
5G.okacbd104.asia/PoSt/1123_921497.HtM
5G.okacbd105.asia/PoSt/1123_005533.HtM
5G.okacbd106.asia/PoSt/1123_500637.HtM
5G.okacbd107.asia/PoSt/1123_784093.HtM
5G.okacbd108.asia/PoSt/1123_171026.HtM
5G.okacbd109.asia/PoSt/1123_818390.HtM
5G.okacbd110.asia/PoSt/1123_708517.HtM
5G.okacbd101.asia/PoSt/1123_247429.HtM
5G.okacbd102.asia/PoSt/1123_009093.HtM
5G.okacbd103.asia/PoSt/1123_505962.HtM
5G.okacbd104.asia/PoSt/1123_639968.HtM
5G.okacbd105.asia/PoSt/1123_678138.HtM
5G.okacbd106.asia/PoSt/1123_749367.HtM
5G.okacbd107.asia/PoSt/1123_500210.HtM
5G.okacbd108.asia/PoSt/1123_287078.HtM
5G.okacbd109.asia/PoSt/1123_499159.HtM
5G.okacbd110.asia/PoSt/1123_974436.HtM
5G.okacbd101.asia/PoSt/1123_900849.HtM
5G.okacbd102.asia/PoSt/1123_325565.HtM
5G.okacbd103.asia/PoSt/1123_270326.HtM
5G.okacbd104.asia/PoSt/1123_071397.HtM
5G.okacbd105.asia/PoSt/1123_601816.HtM
5G.okacbd106.asia/PoSt/1123_614481.HtM
5G.okacbd107.asia/PoSt/1123_113799.HtM
5G.okacbd108.asia/PoSt/1123_073526.HtM
5G.okacbd109.asia/PoSt/1123_481573.HtM
5G.okacbd110.asia/PoSt/1123_233744.HtM
5G.okacbd111.asia/PoSt/1123_446221.HtM
5G.okacbd112.asia/PoSt/1123_849022.HtM
5G.okacbd113.asia/PoSt/1123_546247.HtM
5G.okacbd114.asia/PoSt/1123_871185.HtM
5G.okacbd115.asia/PoSt/1123_383861.HtM
5G.okacbd116.asia/PoSt/1123_641187.HtM
5G.okacbd117.asia/PoSt/1123_742940.HtM
5G.okacbd118.asia/PoSt/1123_307735.HtM
5G.okacbd119.asia/PoSt/1123_363643.HtM
5G.okacbd120.asia/PoSt/1123_858334.HtM
5G.okacbd111.asia/PoSt/1123_061864.HtM
5G.okacbd112.asia/PoSt/1123_067835.HtM
5G.okacbd113.asia/PoSt/1123_278977.HtM
5G.okacbd114.asia/PoSt/1123_460498.HtM
5G.okacbd115.asia/PoSt/1123_332029.HtM
5G.okacbd116.asia/PoSt/1123_387505.HtM
5G.okacbd117.asia/PoSt/1123_463867.HtM
5G.okacbd118.asia/PoSt/1123_014366.HtM
5G.okacbd119.asia/PoSt/1123_657693.HtM
5G.okacbd120.asia/PoSt/1123_373888.HtM
叮咚买菜公司氛围 125人发布