moving caching poets to service level

This commit is contained in:
Hamid Reza Mohammadi 2021-05-16 10:15:28 +04:30
parent fe0fc4bedc
commit d71fc54de5
4 changed files with 38 additions and 71 deletions

View File

@ -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
/// </summary>
private readonly IConfiguration _configuration;
/// <summary>
/// IMemoryCache
/// </summary>
private readonly IMemoryCache _memoryCache;
/// <summary>
/// IAppUserService instance
/// </summary>
@ -58,18 +51,15 @@ namespace GanjooRazor.Pages
/// constructor
/// </summary>
/// <param name="configuration"></param>
/// <param name="memoryCache"></param>
/// <param name="appUserService"></param>
/// <param name="ganjoorService"></param>
/// <param name="httpClient"></param>
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<GanjoorPoetViewModel> poets))
{
var resPoets = await _ganjoorService.GetPoets(true, false);
if(string.IsNullOrEmpty(resPoets.ExceptionString))
{
poets = new List<GanjoorPoetViewModel>(resPoets.Result);
_memoryCache.Set(cacheKey, poets);
}
}
Poets = poets;
Poets = new List<GanjoorPoetViewModel>((await _ganjoorService.GetPoets(true, false)).Result);
}
/// <summary>
@ -516,7 +495,7 @@ namespace GanjooRazor.Pages
return Redirect(pageUrlRes.Result);
}
await preparePoets(false);
await preparePoets();
if (!IsHomePage)
{

View File

@ -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
{
/// <summary>
/// IMemoryCache
/// </summary>
protected readonly IMemoryCache _memoryCache;
/// <summary>
/// ganjoor service
/// </summary>
@ -46,12 +40,10 @@ namespace GanjooRazor.Pages
/// <summary>
/// constructor
/// </summary>
/// <param name="memoryCache"></param>
/// <param name="ganjoorService"></param>
/// <param name="httpClient"></param>
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<GanjoorPoetViewModel> poets))
{
var resPoets = await _ganjoorService.GetPoets(true, false);
if (string.IsNullOrEmpty(resPoets.ExceptionString))
{
poets = new List<GanjoorPoetViewModel>(resPoets.Result);
_memoryCache.Set(cacheKey, poets);
}
}
Poets = poets;
Poets = new List<GanjoorPoetViewModel>((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<IActionResult> 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)

View File

@ -4,7 +4,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
}
}
}

View File

@ -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<GanjoorPoetViewModel[]>
(
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<GanjoorPoetCompleteViewModel>(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<GanjoorPoetCompleteViewModel>(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<GanjoorPoetCompleteViewModel>(poetCat);
}
catch (Exception exp)
{