From 729f8ddd75442d0bb6b5fc2ecfbcae7cadf88382 Mon Sep 17 00:00:00 2001 From: Matteo Piovanelli Date: Fri, 7 Jul 2023 09:58:17 +0200 Subject: [PATCH] Cache CultureRecords by both Id and Name (#8708) --- .../Services/DefaultCultureManager.cs | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Orchard/Localization/Services/DefaultCultureManager.cs b/src/Orchard/Localization/Services/DefaultCultureManager.cs index 49344b987..16c6ade47 100644 --- a/src/Orchard/Localization/Services/DefaultCultureManager.cs +++ b/src/Orchard/Localization/Services/DefaultCultureManager.cs @@ -62,12 +62,36 @@ namespace Orchard.Localization.Services { return _workContextAccessor.GetContext().CurrentCulture; } + protected Dictionary GetAllCulturesById() { + return _cacheManager.Get("all_culture_records_by_id", true, context => { + context.Monitor(_signals.When("culturesChanged")); + + return _cultureRepository.Table + .ToDictionary(cr => cr.Id); + }); + } public CultureRecord GetCultureById(int id) { - return _cultureRepository.Get(id); + var cultures = GetAllCulturesById(); + CultureRecord result; + cultures.TryGetValue(id, out result); + + return result; } + protected Dictionary GetAllCulturesByName() { + return _cacheManager.Get("all_culture_records_by_name", true, context => { + context.Monitor(_signals.When("culturesChanged")); + + return _cultureRepository.Table + .ToDictionary(cr => cr.Culture); + }); + } public CultureRecord GetCultureByName(string cultureName) { - return _cultureRepository.Get(cr => cr.Culture == cultureName); + var cultures = GetAllCulturesByName(); + CultureRecord result; + cultures.TryGetValue(cultureName, out result); + + return result; } public string GetSiteCulture() {