From 2ae2d92e261fb4c500d5b8b46766b0972b9badf9 Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Sun, 18 Feb 2024 18:50:01 +0300 Subject: [PATCH] increase speed of mysql when searching posts --- Core/Service/Hash.cs | 21 +++++++++++++++++++ Core/Startup.cs | 2 ++ modules/RichSiteSummary/Data/Post.cs | 9 +++++++- .../RichSiteSummaryRepositoryExtension.cs | 6 +++++- modules/RichSiteSummary/Service/RssFetch.cs | 8 +++++-- 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 Core/Service/Hash.cs diff --git a/Core/Service/Hash.cs b/Core/Service/Hash.cs new file mode 100644 index 0000000..2e55136 --- /dev/null +++ b/Core/Service/Hash.cs @@ -0,0 +1,21 @@ +using System.Security.Cryptography; +using System.Text; + +namespace Kruzya.TelegramBot.Core.Service +{ + public class Hash + { + public string GenerateHash(string input) + { + var output = string.Empty; + var hashedBytes = SHA256.HashData(Encoding.UTF8.GetBytes(input)); + + foreach (var b in hashedBytes) + { + output += string.Format("{0,2:x2}", b); + } + + return output; + } + } +} diff --git a/Core/Startup.cs b/Core/Startup.cs index 98414cc..b81117e 100644 --- a/Core/Startup.cs +++ b/Core/Startup.cs @@ -59,6 +59,8 @@ public class Startup services.AddScoped(); services.AddHttpClient(); + services.AddSingleton(); + // Trigger module handlers. _core.Modules.ConfigureServices(services); } diff --git a/modules/RichSiteSummary/Data/Post.cs b/modules/RichSiteSummary/Data/Post.cs index 8657e96..245a5a2 100644 --- a/modules/RichSiteSummary/Data/Post.cs +++ b/modules/RichSiteSummary/Data/Post.cs @@ -68,7 +68,7 @@ public class Post : RepresentableEntity /// The URI where this post is located in web. /// [Required] - [MaxLength(256)] + [MaxLength(1024)] public string Url { get; set; } /// @@ -83,6 +83,13 @@ public class Post : RepresentableEntity [Required] public DateTime ReceivedAt { get; set; } + /// + /// + /// + [Required] + [MaxLength(64)] + public string UrlHash { get; set; } + public Post() { PostId = new Guid(); diff --git a/modules/RichSiteSummary/RichSiteSummaryRepositoryExtension.cs b/modules/RichSiteSummary/RichSiteSummaryRepositoryExtension.cs index 95cf763..d4c44b4 100644 --- a/modules/RichSiteSummary/RichSiteSummaryRepositoryExtension.cs +++ b/modules/RichSiteSummary/RichSiteSummaryRepositoryExtension.cs @@ -61,11 +61,15 @@ internal static class RichSiteSummaryRepositoryExtension await repository.ForFetching(period, 25); #endregion - + #region Post + [Obsolete("Use ByFeedAndUrlHash()")] public static async Task ByFeedAndUrl(this DbSet repository, string url, Feed feed = null) => await repository.FirstOrDefaultAsync(post => post.Url == url && (feed == null || post.Feed == feed)); + public static async Task ByFeedAndUrlHash(this DbSet repository, string urlHash, Feed feed = null) => + await repository.FirstOrDefaultAsync(post => post.UrlHash == urlHash && (feed == null || post.Feed == feed)); + #endregion } \ No newline at end of file diff --git a/modules/RichSiteSummary/Service/RssFetch.cs b/modules/RichSiteSummary/Service/RssFetch.cs index 7e3118a..d424462 100644 --- a/modules/RichSiteSummary/Service/RssFetch.cs +++ b/modules/RichSiteSummary/Service/RssFetch.cs @@ -19,6 +19,7 @@ using Telegram.Bot.Types.Enums; using Feed = Kruzya.TelegramBot.RichSiteSummary.Data.Feed; using CodeHollow.FeedReader.Feeds; +using Kruzya.TelegramBot.Core.Service; namespace Kruzya.TelegramBot.RichSiteSummary.Service; @@ -35,14 +36,16 @@ public class RssFetch : IHostedService, IDisposable private readonly ILogger _logger; private readonly IServiceScopeFactory _scopeFactory; private readonly ConcurrentQueue _queue; + private readonly Hash _hash; private static TimeSpan TimerPeriod => TimeSpan.FromSeconds(45); - public RssFetch(ILogger logger, ConcurrentQueue queue, IServiceScopeFactory scopeFactory) + public RssFetch(ILogger logger, ConcurrentQueue queue, IServiceScopeFactory scopeFactory, Hash hash) { _scopeFactory = scopeFactory; _logger = logger; _queue = queue; + _hash = hash; } #region IHostedService @@ -132,7 +135,7 @@ public class RssFetch : IHostedService, IDisposable var postsImages = new Dictionary(); foreach (var post in parsedFeed.Items) { - feedPost = await dbContext.Posts.ByFeedAndUrl(post.Link, feed); + feedPost = await dbContext.Posts.ByFeedAndUrlHash(_hash.GenerateHash(post.Link), feed); if (feedPost != null) { // skip. This post already exists. @@ -144,6 +147,7 @@ public class RssFetch : IHostedService, IDisposable feedPost.Title = post.Title; feedPost.Url = post.Link; feedPost.PostedAt = post.PublishingDate.GetValueOrDefault(DateTime.Now); + feedPost.UrlHash = _hash.GenerateHash(feedPost.Url); postsImages.Add(feedPost, await FetchImageUrl(post)); newPosts.Add(feedPost);