Skip to content

Fix: cache Repository instances per file path in GetRepository()#27

Open
HashidaTKS wants to merge 1 commit intomasterfrom
fix/repository-factory
Open

Fix: cache Repository instances per file path in GetRepository()#27
HashidaTKS wants to merge 1 commit intomasterfrom
fix/repository-factory

Conversation

@HashidaTKS
Copy link
Copy Markdown
Owner

問題点

BetInformation.GetRepository()ActualRaceAndOddsData.GetRepository()TheoreticalRaceAndOddsData.GetRepository()RaceResult.GetRepository() のいずれも呼び出し毎に new Repository(path) でインスタンスを生成していた。

同一ファイルパスに対して複数のインスタンスが存在するため:

  • インスタンスレベルの lock(this) が機能せず、並行アクセス時にデータ競合が発生しうる
  • 毎回 XmlSerializerWrapper やディレクトリ作成が実行され、不要なオーバーヘッドが発生する

改善内容

各モデルクラスに ConcurrentDictionary<string, TRepository> _repositoryCache を追加し、GetOrAdd で同一パスには同一インスタンスを返すよう修正。

対象:

  • BetInformation
  • ActualRaceAndOddsData
  • TheoreticalRaceAndOddsData
  • RaceResult

Test plan

  • 同一 RaceData に対して GetRepository() を複数回呼び出した場合、同じインスタンスが返ることを確認
  • 異なる RaceData に対しては別インスタンスが返ることを確認
  • 通常の読み書き動作が変わらないことを確認

🤖 Generated with Claude Code

…stantiation

GetRepository() was creating a new Repository instance on every call, meaning
multiple instances could exist for the same file path. This undermines instance-level
locking and wastes memory. Use ConcurrentDictionary.GetOrAdd to ensure each file
path maps to exactly one Repository instance throughout the application lifetime.

Applied to: BetInformation, ActualRaceAndOddsData, TheoreticalRaceAndOddsData, RaceResult

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant