From 0dcc3062c90864d2e8c1fb7e212ab963c72f43bb Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 28 May 2021 09:43:16 +0430 Subject: [PATCH] updating pages cache when a user changes his/her NickName --- RMuseum/RMuseum.xml | 13 ++++ .../Implementation/GanjoorAppUserService.cs | 75 ++++++++++++++++++- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 0026b417..a26d882c 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -7560,6 +7560,11 @@ Ganjoor User Service + + + IMemoryCache + + Sign Up Email Subject @@ -7605,6 +7610,14 @@ + + + modify existing user /*update related entities cache*/ + + + + + IGanjoorService implementation diff --git a/RMuseum/Services/Implementation/GanjoorAppUserService.cs b/RMuseum/Services/Implementation/GanjoorAppUserService.cs index 149dae54..a6802f6b 100644 --- a/RMuseum/Services/Implementation/GanjoorAppUserService.cs +++ b/RMuseum/Services/Implementation/GanjoorAppUserService.cs @@ -1,8 +1,11 @@ using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using RMuseum.DbContext; +using RMuseum.Models.Ganjoor.ViewModels; using RSecurityBackend.Models.Auth.Db; +using RSecurityBackend.Models.Auth.ViewModels; using RSecurityBackend.Models.Generic; using RSecurityBackend.Services; using RSecurityBackend.Services.Implementation; @@ -25,12 +28,20 @@ namespace RMuseum.Services.Implementation ISecretGenerator secretGenerator, IImageFileService imageFileService, IUserRoleService userRoleService, - IConfiguration configuration) + IConfiguration configuration, + IMemoryCache memoryCache + ) : base(context, userManager, signInManager, roleManager, secretGenerator, imageFileService, userRoleService, configuration) { - + _memoryCache = memoryCache; } + /// + /// IMemoryCache + /// + protected readonly IMemoryCache _memoryCache; + + /// /// Sign Up Email Subject /// @@ -218,10 +229,68 @@ namespace RMuseum.Services.Implementation } catch { - return new RServiceResult(true); //ignore this error! because signup was succesful + return new RServiceResult(true); //ignore this error! because signup was succesfull } } return res; } + + /// + /// modify existing user /*update related entities cache*/ + /// + /// + /// + /// + public override async Task> ModifyUser(Guid userId, RegisterRAppUser updateUserInfo) + { + RAppUser unmodifiedUserInfo = await _userManager.FindByIdAsync(userId.ToString()); + if (unmodifiedUserInfo == null) + { + return new RServiceResult(false, "کاربر مورد نظر یافت نشد"); + } + + string nickName = unmodifiedUserInfo.NickName; + + RServiceResult res = await base.ModifyUser(userId, updateUserInfo); + if(res.Result) + { + try + { + if (nickName != updateUserInfo.NickName) + { + RMuseumDbContext context = _context as RMuseumDbContext; + var poemIdSet = await context.GanjoorComments.AsNoTracking().Where(c => c.UserId == userId).Select(c => c.PoemId).ToListAsync(); + foreach (var poemId in poemIdSet) + { + //await _ganjoorService.CacheCleanForPageById(poemId); /*had error in service initializtion, so done it in the dirty way*/ + + var dbPage = await context.GanjoorPages.Where(p => p.Id == poemId).AsNoTracking().SingleOrDefaultAsync(); + if (dbPage != null) + { + //CacheCleanForPageByUrl(dbPage.FullUrl); + var url = dbPage.FullUrl; + var cachKey = $"GanjoorService::GetPageByUrl::{url}"; + if (_memoryCache.TryGetValue(cachKey, out GanjoorPageCompleteViewModel page)) + { + _memoryCache.Remove(cachKey); + + var poemCachKey = $"GetPoemById({page.Id}, {true}, {false}, {true}, {true}, {true}, {true}, {true}, {true}, {true})"; + if (_memoryCache.TryGetValue(poemCachKey, out GanjoorPoemCompleteViewModel p)) + { + _memoryCache.Remove(poemCachKey); + } + } + } + } + } + } + catch + { + return new RServiceResult(true); //ignore this error! because main operation was successfull! + } + + } + return res; + } } }