mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2025-01-18 12:54:20 +01:00
Added feedcache for fetching feed items to give every item a parent.
This commit is contained in:
parent
9e6d2183fb
commit
722f6f132a
26
SharpRss/Core/FeedCache.cs
Normal file
26
SharpRss/Core/FeedCache.cs
Normal 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;
|
||||
|
||||
}
|
||||
}
|
|
@ -7,7 +7,9 @@ using System.Threading.Tasks;
|
|||
using Dapper;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Serilog;
|
||||
using SharpRss.Core;
|
||||
using SharpRss.Models;
|
||||
using ToolQit.Containers;
|
||||
|
||||
namespace SharpRss
|
||||
{
|
||||
|
@ -187,11 +189,13 @@ namespace SharpRss
|
|||
}
|
||||
dbTransaction.Commit();
|
||||
}
|
||||
|
||||
public static async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string[]? encodedFeedUrls)
|
||||
{
|
||||
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
|
||||
dbc.Open();
|
||||
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)})");
|
||||
//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())
|
||||
|
@ -210,6 +214,8 @@ namespace SharpRss
|
|||
Categories = reader["categories"].ToString().Split(','),
|
||||
Content = reader["content"].ToString()
|
||||
};
|
||||
feedItemModel.ParentFeed = fCache.CacheFeed(feedItemModel.EncodedFeedUrl);
|
||||
// Get feed
|
||||
items.Add(feedItemModel);
|
||||
}
|
||||
Log.Debug("Fetching feed items resulted: {ItemCount} item(s)", items.Count);
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace SharpRss.Models
|
|||
{
|
||||
public class FeedItemModel
|
||||
{
|
||||
public FeedModel? ParentFeed { get; set; }
|
||||
public string? Id { get; set; } = string.Empty;
|
||||
public string EncodedFeedUrl { get; set; } = string.Empty;
|
||||
public bool Read { get; set; }
|
||||
|
|
|
@ -33,5 +33,12 @@ namespace SharpRss.Models
|
|||
_imageUrl = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is FeedModel objModel)
|
||||
return EncodedUrl.Equals(objModel.EncodedUrl);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>> 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>> GetFeedItemsFromFeedsAsync(string[] feedIds, string? groupId = null)
|
||||
public async Task<HashSet<FeedItemModel>> GetFeedItemsFromFeedsAsync(string[] feedIds, string? categoryId = null)
|
||||
{
|
||||
var items = await DbAccess.GetFeedItemsAsync(feedIds);
|
||||
return items;
|
||||
|
|
|
@ -22,8 +22,4 @@
|
|||
<ProjectReference Include="..\ToolQit\ToolQit\ToolQit.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Core" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Reference in New Issue
Block a user