当 checkout 页货币切换导致 404 GraphQL 请求时,你会检查哪些缓存键?

解读

在国内 Magento 2 项目中,checkout 页是全程 HTTPS 且禁用全页缓存(FPC) 的,但 GraphQL 请求仍可能命中多层级缓存:

  1. 浏览器/ CDN 对 OPTIONS/POST 的缓存
  2. Magento 自身的GraphQL Schema & Resolver 缓存
  3. Redis 里Quote、Currency、Store 级缓存
  4. Varnish/ Nginx 对 /graphql 的路由缓存
    货币切换(?currency=CNY)本质上是Store View 级切换,GraphQL 请求头里必须带 Store 与 Currency 两个维度,否则后端会找不到对应路由而 404。
    因此排查顺序是:先确认请求维度 → 再逐级清缓存键。

知识点

  • GraphQL 路由匹配键:graphql:schema:<store_code>:<currency>
  • Quote 缓存键:quote:<quote_id>:<store_id>:<currency>
  • Currency 价格索引缓存键:catalog_product_price_<website_id>_<customer_group_id>_<currency>
  • Store 配置缓存键:store_config_<store_code>_<currency>
  • Resolver 结果缓存键:resolver:<resolver_class>:<hash_of_query+variables+store+currency>
  • Varnish 缓存键:X-Magento-Tags: graphql-currency-<currency>, graphql-store-<store_code>
  • Redis 标签清理脚本:redis-cli --raw --scan --pattern '*currency*CNY*' | xargs redis-cli del

答案

我会按以下顺序检查并清理与 currency 维度强相关的缓存键:

  1. GraphQL Schema 缓存键
    graphql:schema:<store_code>:<currency>
    若该键缺失或 store+currency 组合未生成,会导致 Magento 找不到对应路由而 404。
  2. Quote 缓存键
    quote:<quote_id>:<store_id>:<currency>
    货币切换后旧 currency 的 quote 缓存未失效,GraphQL 查询 cart 字段时直接 404。
  3. Currency 价格索引缓存键
    catalog_product_price_<website_id>_<customer_group_id>_<currency>
    价格索引未预热时,GraphQL resolver 可能返回空结果,前端路由守卫误判为 404。
  4. Store 配置缓存键
    store_config_<store_code>_<currency>
    若 currency 不在 allowed_currencies 列表,Store 配置缓存未更新,导致 GraphQL 路由直接拒绝。
  5. Resolver 结果缓存键
    resolver:<resolver_class>:<hash_of_query+variables+store+currency>
    命中旧 currency 的缓存片段时,返回的 store 与当前不一致,触发 404。
  6. Varnish/ CDN 缓存键
    通过 X-Magento-Tags 清理 graphql-currency-* 与 graphql-store-* 标签,确保边缘节点不再返回旧路由。

实操命令(国内常用 Redis 集群):

# 清 GraphQL Schema
redis-cli del "graphql:schema:${STORE_CODE}:${CURRENCY}"

# 批量清 Currency 相关键
redis-cli --scan --pattern "*currency*${CURRENCY}*" | xargs -L 100 redis-cli del

# 清 Varnish 标签
varnishadm "ban obj.http.X-Magento-Tags ~ graphql-currency-${CURRENCY}"

拓展思考

  1. 多货币+多站点+HTTPS 反向代理场景下,CDN 回源 Host 头必须与 Magento 的 base_url 一致,否则即使缓存键正确,路由匹配也会 404。
  2. 国内支付宝/微信的跨境支付插件常在 checkout_cart_save_after 事件里重新生成 quote_id,若未同步更新Quote 缓存键,GraphQL 查询 cart(cart_id:...) 会 404。
  3. 使用 Hyvä React Checkout 时,前端会把 currency 写进 localStorage,若用户手动清掉 Cookie 却保留旧 currency,首次 GraphQL 请求会带错 Currency 头,此时需在前端拦截并强制刷新缓存键。