add: 文章标签

This commit is contained in:
jackfiled 2024-01-26 17:29:37 +08:00
parent 4a97483b09
commit 28537f0e7a
2 changed files with 34 additions and 0 deletions

View File

@ -7,6 +7,8 @@ public class EssayContentService
{ {
private readonly ConcurrentDictionary<string, BlogEssay> _essays = new(); private readonly ConcurrentDictionary<string, BlogEssay> _essays = new();
private readonly Dictionary<string, List<BlogEssay>> _tags = [];
public bool TryGet(string key, out BlogEssay? essay) public bool TryGet(string key, out BlogEssay? essay)
=> _essays.TryGetValue(key, out essay); => _essays.TryGetValue(key, out essay);
@ -15,4 +17,35 @@ public class EssayContentService
public IEnumerable<KeyValuePair<string, BlogEssay>> Essays => _essays; public IEnumerable<KeyValuePair<string, BlogEssay>> Essays => _essays;
public int Count => _essays.Count; public int Count => _essays.Count;
public void RefreshTags()
{
foreach (BlogEssay essay in _essays.Values)
{
foreach (string tag in essay.Tags)
{
if (_tags.TryGetValue(tag, out var list))
{
list.Add(essay);
}
else
{
_tags[tag] = [essay];
}
}
}
}
public IEnumerable<KeyValuePair<string, int>> Tags => from item in _tags
select KeyValuePair.Create(item.Key, item.Value.Count);
public IEnumerable<BlogEssay> GetTag(string tag)
{
if (_tags.TryGetValue(tag, out var list))
{
return list;
}
throw new KeyNotFoundException("Selected tag not found.");
}
} }

View File

@ -66,6 +66,7 @@ public class RendererService(ILogger<RendererService> logger,
}); });
await PostProcess(postProcessEssays); await PostProcess(postProcessEssays);
essayContentService.RefreshTags();
_stopwatch.Stop(); _stopwatch.Stop();
logger.LogInformation("Render finished, consuming {} s.", logger.LogInformation("Render finished, consuming {} s.",