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
+21
View File
@@ -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;
}
}
}
+2
View File
@@ -59,6 +59,8 @@ public class Startup
services.AddScoped<IOptionProvider, DbOptionProvider>();
services.AddHttpClient();
services.AddSingleton<Hash>();
// Trigger module handlers.
_core.Modules.ConfigureServices(services);
}
+8 -1
View File
@@ -68,7 +68,7 @@ public class Post : RepresentableEntity
/// The URI where this post is located in web.
/// </summary>
[Required]
[MaxLength(256)]
[MaxLength(1024)]
public string Url { get; set; }
/// <summary>
@@ -83,6 +83,13 @@ public class Post : RepresentableEntity
[Required]
public DateTime ReceivedAt { get; set; }
/// <summary>
///
/// </summary>
[Required]
[MaxLength(64)]
public string UrlHash { get; set; }
public Post()
{
PostId = new Guid();
@@ -64,8 +64,12 @@ internal static class RichSiteSummaryRepositoryExtension
#region Post
[Obsolete("Use ByFeedAndUrlHash()")]
public static async Task<Post> ByFeedAndUrl(this DbSet<Post> repository, string url, Feed feed = null) =>
await repository.FirstOrDefaultAsync(post => post.Url == url && (feed == null || post.Feed == feed));
public static async Task<Post> ByFeedAndUrlHash(this DbSet<Post> repository, string urlHash, Feed feed = null) =>
await repository.FirstOrDefaultAsync(post => post.UrlHash == urlHash && (feed == null || post.Feed == feed));
#endregion
}
+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);