From ee1e5c041b846e2352fb86b83a54b938247215f1 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sun, 16 May 2021 11:01:34 +0430 Subject: [PATCH] cache for recitations --- RMuseum/RMuseum.xml | 16 +++++++-- .../Implementation/RecitationService.cs | 34 +++++++++++++------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 5b4c2ea5..5659830a 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -8225,13 +8225,19 @@ Simple Image Storage (not storing multiple image sizes) for creating small cropped images - + + + memory cache + + + constructor + @@ -8495,7 +8501,12 @@ Users service - + + + memory cache + + + constructor @@ -8504,6 +8515,7 @@ + diff --git a/RMuseum/Services/Implementation/RecitationService.cs b/RMuseum/Services/Implementation/RecitationService.cs index 2ee36f1e..5e12e70f 100644 --- a/RMuseum/Services/Implementation/RecitationService.cs +++ b/RMuseum/Services/Implementation/RecitationService.cs @@ -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(narration); } catch (Exception exp) @@ -1826,6 +1833,11 @@ namespace RMuseum.Services.Implementationa /// protected readonly IAppUserService _userService; + /// + /// memory cache + /// + private readonly IMemoryCache _memoryCache; + /// /// constructor @@ -1835,13 +1847,15 @@ namespace RMuseum.Services.Implementationa /// /// /// - public RecitationService(RMuseumDbContext context, IConfiguration configuration, IBackgroundTaskQueue backgroundTaskQueue, IRNotificationService notificationService, IAppUserService userService) + /// + 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; } } }