Added feedcache for fetching feed items to give every item a parent.

This commit is contained in:
Max 2023-06-15 20:04:15 +02:00
parent 9e6d2183fb
commit 722f6f132a
6 changed files with 41 additions and 5 deletions

View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using SharpRss.Models;
using SharpRss.Services;
namespace SharpRss.Core
{
public class FeedCache
{
public FeedCache()
{
FetchFeeds();
}
private readonly SyndicationService _syndicationService = new SyndicationService();
private Dictionary<string, FeedModel> _cachedFeeds = new Dictionary<string, FeedModel>();
private async void FetchFeeds()
{
HashSet<FeedModel> fetchedFeeds = await _syndicationService.GetFeedsAsync();
_cachedFeeds = fetchedFeeds.ToDictionary(x => x.EncodedUrl);
}
public FeedModel? CacheFeed(string encodedUrl) => _cachedFeeds.TryGetValue(encodedUrl, out FeedModel model) ? model : null;
}
}

View File

@ -7,7 +7,9 @@ using System.Threading.Tasks;
using Dapper; using Dapper;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Serilog; using Serilog;
using SharpRss.Core;
using SharpRss.Models; using SharpRss.Models;
using ToolQit.Containers;
namespace SharpRss namespace SharpRss
{ {
@ -187,11 +189,13 @@ namespace SharpRss
} }
dbTransaction.Commit(); dbTransaction.Commit();
} }
public static async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string[]? encodedFeedUrls) public static async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string[]? encodedFeedUrls)
{ {
await using SqliteConnection dbc = new SqliteConnection(ConnectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
HashSet<FeedItemModel> items = new HashSet<FeedItemModel>(); HashSet<FeedItemModel> items = new HashSet<FeedItemModel>();
FeedCache fCache = new FeedCache();
await using DbDataReader reader = await dbc.ExecuteReaderAsync(encodedFeedUrls == null ? "SELECT * FROM feed_item" : $"SELECT * FROM feed_item WHERE encoded_feed_url IN({FormatParametersFromArray(encodedFeedUrls)})"); await using DbDataReader reader = await dbc.ExecuteReaderAsync(encodedFeedUrls == null ? "SELECT * FROM feed_item" : $"SELECT * FROM feed_item WHERE encoded_feed_url IN({FormatParametersFromArray(encodedFeedUrls)})");
//NOTE(dbg): While debugging this part of the function the code below (Reader.ReadAsync) will return 0, because the debugger already reads the items from the reader. //NOTE(dbg): While debugging this part of the function the code below (Reader.ReadAsync) will return 0, because the debugger already reads the items from the reader.
while (await reader.ReadAsync()) while (await reader.ReadAsync())
@ -210,6 +214,8 @@ namespace SharpRss
Categories = reader["categories"].ToString().Split(','), Categories = reader["categories"].ToString().Split(','),
Content = reader["content"].ToString() Content = reader["content"].ToString()
}; };
feedItemModel.ParentFeed = fCache.CacheFeed(feedItemModel.EncodedFeedUrl);
// Get feed
items.Add(feedItemModel); items.Add(feedItemModel);
} }
Log.Debug("Fetching feed items resulted: {ItemCount} item(s)", items.Count); Log.Debug("Fetching feed items resulted: {ItemCount} item(s)", items.Count);

View File

@ -4,6 +4,7 @@ namespace SharpRss.Models
{ {
public class FeedItemModel public class FeedItemModel
{ {
public FeedModel? ParentFeed { get; set; }
public string? Id { get; set; } = string.Empty; public string? Id { get; set; } = string.Empty;
public string EncodedFeedUrl { get; set; } = string.Empty; public string EncodedFeedUrl { get; set; } = string.Empty;
public bool Read { get; set; } public bool Read { get; set; }

View File

@ -33,5 +33,12 @@ namespace SharpRss.Models
_imageUrl = value; _imageUrl = value;
} }
} }
public override bool Equals(object obj)
{
if (obj is FeedModel objModel)
return EncodedUrl.Equals(objModel.EncodedUrl);
return false;
}
} }
} }

View File

@ -57,7 +57,7 @@ namespace SharpRss.Services
public async Task<HashSet<FeedModel>> GetFeedsAsync(string? categoryId = null) => await DbAccess.GetFeedsAsync(categoryId == null ? null : new[]{ categoryId }); public async Task<HashSet<FeedModel>> GetFeedsAsync(string? categoryId = null) => await DbAccess.GetFeedsAsync(categoryId == null ? null : new[]{ categoryId });
public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync(new []{""}); public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync(new []{""});
public async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string feedId, string? groupId = null) => await GetFeedItemsFromFeedsAsync(new[] { feedId }, groupId); public async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string feedId, string? groupId = null) => await GetFeedItemsFromFeedsAsync(new[] { feedId }, groupId);
public async Task<HashSet<FeedItemModel>> GetFeedItemsFromFeedsAsync(string[] feedIds, string? groupId = null) public async Task<HashSet<FeedItemModel>> GetFeedItemsFromFeedsAsync(string[] feedIds, string? categoryId = null)
{ {
var items = await DbAccess.GetFeedItemsAsync(feedIds); var items = await DbAccess.GetFeedItemsAsync(feedIds);
return items; return items;

View File

@ -22,8 +22,4 @@
<ProjectReference Include="..\ToolQit\ToolQit\ToolQit.csproj" /> <ProjectReference Include="..\ToolQit\ToolQit\ToolQit.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Core" />
</ItemGroup>
</Project> </Project>