cache for recitations

This commit is contained in:
Hamid Reza Mohammadi 2021-05-16 11:01:34 +04:30
parent ae33799da0
commit ee1e5c041b
2 changed files with 38 additions and 12 deletions

View File

@ -8225,13 +8225,19 @@
Simple Image Storage (not storing multiple image sizes) for creating small cropped images
</summary>
</member>
<member name="M:RMuseum.Services.Implementation.PictureFileService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IImageFileService)">
<member name="F:RMuseum.Services.Implementation.PictureFileService._memoryCache">
<summary>
memory cache
</summary>
</member>
<member name="M:RMuseum.Services.Implementation.PictureFileService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IImageFileService,Microsoft.Extensions.Caching.Memory.IMemoryCache)">
<summary>
constructor
</summary>
<param name="context"></param>
<param name="configuration"></param>
<param name="simpleImageStorage"></param>
<param name="memoryCache"></param>
</member>
<member name="T:RMuseum.Services.Implementation.RecitationsRssBuilder">
<summary>
@ -8495,7 +8501,12 @@
Users service
</summary>
</member>
<member name="M:RMuseum.Services.Implementationa.RecitationService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IBackgroundTaskQueue,RSecurityBackend.Services.IRNotificationService,RSecurityBackend.Services.IAppUserService)">
<member name="F:RMuseum.Services.Implementationa.RecitationService._memoryCache">
<summary>
memory cache
</summary>
</member>
<member name="M:RMuseum.Services.Implementationa.RecitationService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IBackgroundTaskQueue,RSecurityBackend.Services.IRNotificationService,RSecurityBackend.Services.IAppUserService,Microsoft.Extensions.Caching.Memory.IMemoryCache)">
<summary>
constructor
</summary>
@ -8504,6 +8515,7 @@
<param name="backgroundTaskQueue"></param>
<param name="notificationService"></param>
<param name="userService"></param>
<param name="memoryCache"></param>
</member>
<member name="T:RMuseum.Services.IMusicCatalogueService">
<summary>

View File

@ -2,6 +2,7 @@
using ganjoor;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using RMuseum.DbContext;
@ -192,16 +193,22 @@ namespace RMuseum.Services.Implementationa
{
try
{
//whenever I had not a reference to audio.Owner in the final selection it became null, so this strange arrangement is not all because of my stupidity!
var source =
from audio in _context.Recitations.AsNoTracking()
.Include(a => a.Owner)
.Where(a => a.Id == id)
join poem in _context.GanjoorPoems
on audio.GanjoorPostId equals poem.Id
select new RecitationViewModel(audio, audio.Owner, poem);
var cachKey = $"RecitationService::Get::{id}";
if(!_memoryCache.TryGetValue(cachKey, out RecitationViewModel narration))
{
//whenever I had not a reference to audio.Owner in the final selection it became null, so this strange arrangement is not all because of my stupidity!
var source =
from audio in _context.Recitations.AsNoTracking()
.Include(a => a.Owner)
.Where(a => a.Id == id)
join poem in _context.GanjoorPoems
on audio.GanjoorPostId equals poem.Id
select new RecitationViewModel(audio, audio.Owner, poem);
var narration = await source.SingleOrDefaultAsync();
narration = await source.SingleOrDefaultAsync();
_memoryCache.Set(cachKey, narration);
}
return new RServiceResult<RecitationViewModel>(narration);
}
catch (Exception exp)
@ -1826,6 +1833,11 @@ namespace RMuseum.Services.Implementationa
/// </summary>
protected readonly IAppUserService _userService;
/// <summary>
/// memory cache
/// </summary>
private readonly IMemoryCache _memoryCache;
/// <summary>
/// constructor
@ -1835,13 +1847,15 @@ namespace RMuseum.Services.Implementationa
/// <param name="backgroundTaskQueue"></param>
/// <param name="notificationService"></param>
/// <param name="userService"></param>
public RecitationService(RMuseumDbContext context, IConfiguration configuration, IBackgroundTaskQueue backgroundTaskQueue, IRNotificationService notificationService, IAppUserService userService)
/// <param name="memoryCache"></param>
public RecitationService(RMuseumDbContext context, IConfiguration configuration, IBackgroundTaskQueue backgroundTaskQueue, IRNotificationService notificationService, IAppUserService userService, IMemoryCache memoryCache)
{
_context = context;
Configuration = configuration;
_backgroundTaskQueue = backgroundTaskQueue;
_notificationService = notificationService;
_userService = userService;
_memoryCache = memoryCache;
}
}
}