increase speed of mysql when searching posts

This commit is contained in:
2024-02-18 18:50:01 +03:00
parent 24977c90bd
commit 2ae2d92e26
5 changed files with 42 additions and 4 deletions
+6 -2
View File
@@ -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<UserMessage> _queue;
private readonly Hash _hash;
private static TimeSpan TimerPeriod => TimeSpan.FromSeconds(45);
public RssFetch(ILogger<RssFetch> logger, ConcurrentQueue<UserMessage> queue, IServiceScopeFactory scopeFactory)
public RssFetch(ILogger<RssFetch> logger, ConcurrentQueue<UserMessage> 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<Post, string>();
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);