diff --git a/GanjooRazor/Pages/Index.cshtml.cs b/GanjooRazor/Pages/Index.cshtml.cs index c97b5c5a..cdd09af6 100644 --- a/GanjooRazor/Pages/Index.cshtml.cs +++ b/GanjooRazor/Pages/Index.cshtml.cs @@ -4,10 +4,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.ViewFeatures; -using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Ganjoor; using RMuseum.Models.Ganjoor.ViewModels; @@ -34,11 +32,6 @@ namespace GanjooRazor.Pages /// private readonly IConfiguration _configuration; - /// - /// IMemoryCache - /// - private readonly IMemoryCache _memoryCache; - /// /// IAppUserService instance /// @@ -58,18 +51,15 @@ namespace GanjooRazor.Pages /// constructor /// /// - /// /// /// /// public IndexModel(IConfiguration configuration, - IMemoryCache memoryCache, IAppUserService appUserService, IGanjoorService ganjoorService, HttpClient httpClient) { _configuration = configuration; - _memoryCache = memoryCache; _appUserService = appUserService; _ganjoorService = ganjoorService; _httpClient = httpClient; @@ -476,20 +466,9 @@ namespace GanjooRazor.Pages return audiodesc; } - private async Task preparePoets(bool includeBio) + private async Task preparePoets() { - var cacheKey = $"/api/ganjoor/poets?includeBio={includeBio}"; - if(!_memoryCache.TryGetValue(cacheKey, out List poets)) - { - var resPoets = await _ganjoorService.GetPoets(true, false); - if(string.IsNullOrEmpty(resPoets.ExceptionString)) - { - poets = new List(resPoets.Result); - _memoryCache.Set(cacheKey, poets); - } - } - - Poets = poets; + Poets = new List((await _ganjoorService.GetPoets(true, false)).Result); } /// @@ -516,7 +495,7 @@ namespace GanjooRazor.Pages return Redirect(pageUrlRes.Result); } - await preparePoets(false); + await preparePoets(); if (!IsHomePage) { diff --git a/GanjooRazor/Pages/Search.cshtml.cs b/GanjooRazor/Pages/Search.cshtml.cs index cc7c567b..3fe032b1 100644 --- a/GanjooRazor/Pages/Search.cshtml.cs +++ b/GanjooRazor/Pages/Search.cshtml.cs @@ -11,7 +11,6 @@ using GanjooRazor.Utils; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Ganjoor.ViewModels; @@ -26,11 +25,6 @@ namespace GanjooRazor.Pages public class SearchModel : PageModel { - /// - /// IMemoryCache - /// - protected readonly IMemoryCache _memoryCache; - /// /// ganjoor service /// @@ -46,12 +40,10 @@ namespace GanjooRazor.Pages /// /// constructor /// - /// /// /// - public SearchModel(IMemoryCache memoryCache, IGanjoorService ganjoorService, HttpClient httpClient) + public SearchModel(IGanjoorService ganjoorService, HttpClient httpClient) { - _memoryCache = memoryCache; _ganjoorService = ganjoorService; _httpClient = httpClient; } @@ -73,37 +65,14 @@ namespace GanjooRazor.Pages [BindProperty] public LoginViewModel LoginViewModel { get; set; } - private async Task preparePoets(bool includeBio) + private async Task preparePoets() { - var cacheKey = $"/api/ganjoor/poets?includeBio={includeBio}"; - if (!_memoryCache.TryGetValue(cacheKey, out List poets)) - { - var resPoets = await _ganjoorService.GetPoets(true, false); - if (string.IsNullOrEmpty(resPoets.ExceptionString)) - { - poets = new List(resPoets.Result); - _memoryCache.Set(cacheKey, poets); - } - } - - Poets = poets; + Poets = new List((await _ganjoorService.GetPoets(true, false)).Result); } private async Task preparePoet() { - var cacheKey = $"/api/ganjoor/poet/{PoetId}"; - - if (!_memoryCache.TryGetValue(cacheKey, out GanjoorPoetCompleteViewModel poet)) - { - var resPoets = await _ganjoorService.GetPoetById(PoetId); - if (string.IsNullOrEmpty(resPoets.ExceptionString)) - { - poet = resPoets.Result; - _memoryCache.Set(cacheKey, poet); - } - } - - Poet = poet; + Poet = (await _ganjoorService.GetPoetById(PoetId)).Result; } public async Task OnGetAsync() @@ -117,7 +86,7 @@ namespace GanjooRazor.Pages //todo: use html master layout or make it partial // 1. poets - await preparePoets(false); + await preparePoets(); var poetName = Poets.SingleOrDefault(p => p.Id == PoetId); if (poetName != null) diff --git a/GanjooRazor/appsettings.Development.json b/GanjooRazor/appsettings.Development.json index 51737579..b93ce93a 100644 --- a/GanjooRazor/appsettings.Development.json +++ b/GanjooRazor/appsettings.Development.json @@ -4,7 +4,8 @@ "LogLevel": { "Default": "Information", "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Microsoft.Hosting.Lifetime": "Information", + "Microsoft.EntityFrameworkCore.Database.Command": "Information" } } } diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 027a4cbe..c3bec0cf 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -39,7 +39,10 @@ namespace RMuseum.Services.Implementation { try { - var res = + var cacheKey = $"/api/ganjoor/poets?websitePoets={websitePoets}&includeBio={includeBio}"; + if (!_memoryCache.TryGetValue(cacheKey, out GanjoorPoetViewModel[] poets)) + { + var res = await (from poet in _context.GanjoorPoets join cat in _context.GanjoorCategories.Where(c => c.ParentId == null) @@ -60,13 +63,18 @@ namespace RMuseum.Services.Implementation .AsNoTracking() .ToListAsync(); - StringComparer fa = StringComparer.Create(new CultureInfo("fa-IR"), true); - res.Sort((a, b) => fa.Compare(a.Nickname, b.Nickname)); + StringComparer fa = StringComparer.Create(new CultureInfo("fa-IR"), true); + res.Sort((a, b) => fa.Compare(a.Nickname, b.Nickname)); + poets = res.ToArray(); + _memoryCache.Set(cacheKey, poets); + } + + return new RServiceResult ( - res.ToArray() - ); ; + poets + ); } catch (Exception exp) { @@ -83,11 +91,21 @@ namespace RMuseum.Services.Implementation { try { - var poet = await _context.GanjoorPoets.Where(p => p.Id == id).AsNoTracking().FirstOrDefaultAsync(); - if (poet == null) - return new RServiceResult(null); - var cat = await _context.GanjoorCategories.Where(c => c.ParentId == null && c.PoetId == id).AsNoTracking().FirstOrDefaultAsync(); - return await GetCatById(cat.Id); + var cacheKey = $"/api/ganjoor/poet/{id}"; + + if(!_memoryCache.TryGetValue(cacheKey, out GanjoorPoetCompleteViewModel poetCat)) + { + var poet = await _context.GanjoorPoets.Where(p => p.Id == id).AsNoTracking().FirstOrDefaultAsync(); + if (poet == null) + return new RServiceResult(null); + var cat = await _context.GanjoorCategories.Where(c => c.ParentId == null && c.PoetId == id).AsNoTracking().FirstOrDefaultAsync(); + poetCat = (await GetCatById(cat.Id)).Result; + if(poetCat != null) + { + _memoryCache.Set(cacheKey, poetCat); + } + } + return new RServiceResult(poetCat); } catch (Exception exp) {