Skip to content

Q&A画面の未解決タブのバッジにキャッシュを使う #8990

@Miya096jp

Description

@Miya096jp

概要

  • 管理者・メンター権限でログインした際に、Q&A画面の未解決タブに未解決の質問数のバッジが表示される。
  • 未解決の質問数のデータはapp/models/cache.rbでキャッシュされているが使用されておらず、代わりにviewヘルパーでQuestionモデルを直接クエリしているため、キャッシュを使用するように実装を変更する。

現在の実装

Q&A一覧画面のバッジ

Image

app/models/cache.rbで未解決の質問の数をキャッシュしているが未使用

class Cache
  class << self
    def not_solved_question_count
      Rails.cache.fetch 'not_solved_question_count' do
        Question.not_solved.count
      end
    end

helpers/page_tabs/questions_helper.rbで直接クエリしている

module PageTabs
  module QuestionsHelper
    def questions_page_tabs
      unsolved_badge = Question.unsolved_badge(current_user:, practice_id: params[:practice_id])
      tabs = []
   #1 tabs << { name: '未解決', link: questions_path(target: 'not_solved'), badge: unsolved_badge }
      ...
   render PageTabsComponent.new(tabs:, active_tab: question_active_tab)
    end

補足情報

app/models/cache.rbにはキャッシュをクリアするメソッドも記述されている。

class Cache
  class << self
...
    def delete_not_solved_question_count
      Rails.cache.delete 'not_solved_question_count'
    end

そしてそのメソッドはAnswerCacheDestroyerで使われており、

# *app/models/answer_cache_destroyer.rb
class AnswerCacheDestroyer
  def call(payload)
    _answer = payload[:answer]
    Cache.delete_not_solved_question_count
  end
end

AnswerCacheDestroyerは、newspaperで回答の保存・更新のタイミングで実行されている。

app/controllers/api/answers_controller.rb
  def create
    question = Question.find(params[:question_id])
    @answer = question.answers.new(answer_params)
    if @answer.save
      Newspaper.publish(:answer_save, { answer: @answer })
      render partial: 'questions/answer', locals: { question:, answer: @answer, user: current_user }, status: :created
    else
...

  def update
    if @answer.update(answer_params)
      Newspaper.publish(:answer_save, { answer: @answer })
      head :ok
    else
    ...

関連Issue

#8922
#9232

Metadata

Metadata

Assignees

Labels

Type

Projects

Status

完成

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions