mirror of
https://github.com/BlossomiShymae/Needlework.Net.git
synced 2025-12-06 02:00:47 +01:00
feat: use file kv-store for posts
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -34,6 +34,7 @@ bld/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
[Ll]ogs/
|
||||
[Dd]ata/
|
||||
|
||||
# Visual Studio 2015/2017 cache/options directory
|
||||
.vs/
|
||||
@@ -482,3 +483,7 @@ $RECYCLE.BIN/
|
||||
|
||||
# Vim temporary swap files
|
||||
*.swp
|
||||
|
||||
*.sqlite
|
||||
*.sqlite-shm
|
||||
*.sqlite-wal
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Akavache;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
@@ -9,6 +10,7 @@ using Needlework.Net.ViewModels.MainWindow;
|
||||
using Needlework.Net.ViewModels.Pages;
|
||||
using Needlework.Net.Views.MainWindow;
|
||||
using System;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
@@ -59,6 +61,13 @@ public partial class App : Application, IEnableLogger
|
||||
DataContext = _serviceProvider.GetRequiredService<MainWindowViewModel>()
|
||||
};
|
||||
MainWindow = desktop.MainWindow;
|
||||
|
||||
desktop.ShutdownRequested += (_, _) =>
|
||||
{
|
||||
var blobCache = _serviceProvider.GetRequiredService<IBlobCache>();
|
||||
blobCache.Flush().Wait();
|
||||
blobCache.Dispose();
|
||||
};
|
||||
}
|
||||
|
||||
base.OnFrameworkInitializationCompleted();
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="akavache" Version="10.2.41" />
|
||||
<PackageReference Include="AngleSharp" Version="1.3.0" />
|
||||
<PackageReference Include="Avalonia" Version="11.2.8" />
|
||||
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.3.0" />
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Avalonia;
|
||||
using Akavache;
|
||||
using Akavache.Sqlite3;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Flurl.Http.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -105,6 +107,11 @@ class Program
|
||||
builder.AddSingleton<NotificationService>();
|
||||
builder.AddSingleton<SchemaPaneService>();
|
||||
builder.AddSingleton<HextechDocsPostService>();
|
||||
builder.AddSingleton<IBlobCache>((_) =>
|
||||
{
|
||||
Directory.CreateDirectory("Data");
|
||||
return new SqlRawPersistentBlobCache("Data/data.sqlite");
|
||||
});
|
||||
builder.AddSingleton<IFlurlClientCache>(new FlurlClientCache()
|
||||
.Add("GithubClient", "https://api.github.com")
|
||||
.Add("GithubUserContentClient", "https://raw.githubusercontent.com")
|
||||
|
||||
@@ -1,23 +1,31 @@
|
||||
using AngleSharp;
|
||||
using FastCache;
|
||||
using Akavache;
|
||||
using AngleSharp;
|
||||
using Needlework.Net.Extensions;
|
||||
using Needlework.Net.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Needlework.Net.Services
|
||||
{
|
||||
public class HextechDocsPostService
|
||||
public class HextechDocsPostService : IEnableLogger
|
||||
{
|
||||
private readonly IBrowsingContext _context = BrowsingContext.New(Configuration.Default.WithDefaultLoader());
|
||||
|
||||
public async Task<List<HextechDocsPost>> GetPostsAsync()
|
||||
private readonly IBlobCache _blobCache;
|
||||
|
||||
public HextechDocsPostService(IBlobCache blobCache)
|
||||
{
|
||||
if (Cached<List<HextechDocsPost>>.TryGet(nameof(GetPostsAsync), out var cached))
|
||||
{
|
||||
return cached;
|
||||
_blobCache = blobCache;
|
||||
}
|
||||
|
||||
public async Task<List<HextechDocsPost>> GetPostsAsync()
|
||||
{
|
||||
return await _blobCache.GetOrFetchObject("HextechDocsPosts", async () =>
|
||||
{
|
||||
this.Log()
|
||||
.Debug("Downloading HextechDocs posts...");
|
||||
var document = await _context.OpenAsync("https://hextechdocs.dev/tag/lcu/");
|
||||
var elements = document.QuerySelectorAll("article.post-card");
|
||||
var posts = new List<HextechDocsPost>();
|
||||
@@ -34,8 +42,8 @@ namespace Needlework.Net.Services
|
||||
};
|
||||
posts.Add(post);
|
||||
}
|
||||
|
||||
return cached.Save(posts, TimeSpan.FromMinutes(60));
|
||||
return posts;
|
||||
}, DateTimeOffset.Now + TimeSpan.FromHours(12));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user