You need to enable JavaScript to run this app. 如何在Rails食品菜单应用中实现Memcache与Redis两级缓存机制
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rails食品菜单应用中实现Memcache与Redis两级缓存机制

食品菜单应用两级缓存实现说明

核心查询逻辑

  • 整体采用Memcache + Redis的两级缓存架构,降低数据库直连压力
  • 最高优先级查询Memcache层匹配当前门店ID的菜单缓存,若缓存命中、查询无异常则直接返回结果
  • 若Memcache无匹配数据、或出现Memcache查询异常,自动降级切换到Redis缓存层校验对应菜单数据,命中则直接返回
  • 若Redis无匹配数据、或出现Redis查询异常,最终从数据库加载菜单数据,同时按需向上层缓存回填对应数据

实现参考代码

pos_items = Rails.cache
  .fetch("pos_menu_items_for_location_#{@location_id}", expires_in: 90.days) do
    pos_items = RedisCache::PosMenu.fetch(location_id: @location_id) do
      pos_items = PosMenuReader.new(location_id: @location_id).call
      RedisCache::PosMenu.update(location_id: @location_id, menu: pos_items) unless pos_items == "working"
      if pos_items == "working" 
        wait_for_worker_to_finish = true
      else 
        Rails.logger.info("DEBUG::PosMenu::#{pos_menu_key} Fetched from db.")
        pos_items
      end
    end
  end

内容的提问来源于stack exchange,提问作者Akhil G Krishnan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.19 23:06:03