Add caching all data from backend and change getting readme data from page(Index.cshtml) to api(HomeController.cs) with cache readme file

This commit is contained in:
Дмитрий Шиманский
2023-12-24 23:01:57 +03:00
parent 153979cbbd
commit 38c12ca246
4 changed files with 83 additions and 22 deletions

View File

@@ -4,13 +4,23 @@ using Microsoft.AspNetCore.RateLimiting;
using System.Text.Json;
using System.Text.Json.Nodes;
using yaflay.ru.Models.Tables;
using yaflay.ru.Pages;
using Microsoft.Extensions.Caching.Memory;
using yaflay.ru.Models;
using System;
using Microsoft.AspNetCore.Authorization;
namespace yaflay.ru.Новая_папка
{
[Route("")]
public class HomeController : Controller
{
{
private IMemoryCache cache;
private AppDbContext ctx;
public HomeController(IMemoryCache cache, AppDbContext ctx)
{
this.cache = cache;
this.ctx = ctx;
}
public class commentBody
{
public string text { get; set; }
@@ -31,6 +41,20 @@ namespace yaflay.ru.Новая_папка
public string author { get; set; }
}
[HttpGet("api/Index")]
public async Task<IActionResult> getIndexPage()
{
string? indexPage = (string)cache.Get($"indexPage");
if (indexPage == null)
{
indexPage = await Startup.client.GetStringAsync(Startup.readmeFile);
if (indexPage != null)
cache.Set($"indexPage", (object)indexPage, DateTime.Now.AddMinutes(10));
}
return Ok(indexPage);
}
[HttpPost("api/redirects")]
public async Task<IActionResult> createRedirectUri([FromBody]redirectBody body)
{
@@ -51,8 +75,8 @@ namespace yaflay.ru.Новая_папка
redirectTo = body.url,
uri = body.uri
};
await Startup.dbContext.Redirects.AddAsync(redirects);
await Startup.dbContext.SaveChangesAsync();
await ctx.Redirects.AddAsync(redirects);
await ctx.SaveChangesAsync();
return Ok();
}
else
@@ -87,8 +111,8 @@ namespace yaflay.ru.Новая_папка
Title = body.title,
authorNickname = response["user"]["global_name"].ToString()
};
await Startup.dbContext.Blogs.AddAsync(article);
await Startup.dbContext.SaveChangesAsync();
await ctx.Blogs.AddAsync(article);
await ctx.SaveChangesAsync();
return Ok(body);
}
catch (Exception ex)
@@ -117,8 +141,14 @@ namespace yaflay.ru.Новая_папка
[HttpGet("api/Blog/{blogId?}/comments")]
public async Task<IActionResult> blogComments(int? blogId)
{
Comments[]? comments = (Comments[]?)cache.Get($"commentsWithBlogId{blogId}");
if (comments == null)
{
comments = ctx.Comments.Where(k => k.postId == blogId).ToArray();
if (comments != null)
cache.Set($"commentsWithBlogId{blogId}", (object[])comments, DateTime.Now.AddMinutes(5));
}
Comments[] comments = Startup.dbContext.Comments.Where(k => k.postId == blogId).ToArray();
return Ok(comments);
}
[HttpPost("api/Blog/{blogId}/comments")]
@@ -133,29 +163,55 @@ namespace yaflay.ru.Новая_папка
Text = body.text,
postId = blogId
};
await Startup.dbContext.Comments.AddAsync(comment);
await Startup.dbContext.SaveChangesAsync();
await ctx.Comments.AddAsync(comment);
await ctx.SaveChangesAsync();
return Ok();
}
[HttpGet("api/Blog/{blogId}")]
public async Task<IActionResult> blog(int blogId)
{
Blogs? blog = Startup.dbContext.Blogs.FirstOrDefault(k => k.Id == blogId);
Blogs? blog = (Blogs)cache.Get($"blogWithId{blogId}");
if (blog == null)
{
blog = ctx.Blogs.FirstOrDefault(k => k.Id == blogId);
if (blog != null)
cache.Set($"blogWithId{blogId}", (object)blog, DateTime.Now.AddMinutes(10));
}
return Ok(blog);
}
[HttpGet("api/Blog")]
public async Task<IActionResult> allBlogs()
{
Blogs[]? blogs = (Blogs[])cache.Get($"allBlogs");
if (blogs == null)
{
blogs = ctx.Blogs.ToArray();
if (blog != null)
cache.Set($"allBlogs", (object)blogs, DateTime.Now.AddMinutes(10));
}
return Ok(blogs);
}
[HttpGet("{uri}")]
public async Task<IActionResult> FromGitHub(string uri)
{
string? url = Startup.dbContext.Redirects.FirstOrDefault(k => k.uri == uri)?.redirectTo;
return Redirect(url ?? "/404");
}
Redirects? fromCache = (Redirects)cache.Get($"redirectsWithUrl={uri}");
if (fromCache != null)
{
fromCache = ctx.Redirects.FirstOrDefault(k => k.uri == uri);
if (fromCache == null)
cache.Set($"redirectsWithUrl={uri}", (object)fromCache, DateTime.Now.AddMinutes(5));
}
return Redirect(fromCache?.redirectTo ?? "/404");
}
}
}

View File

@@ -1,12 +1,15 @@
@page "{id?}"
@model BlogModel
@using yaflay.ru.Models.Tables
@using Newtonsoft.Json
@{
string path = $"{this.Request.Scheme}://{this.Request.Host}";
if (Model.Id != 0)
{
Blogs? Post = Startup.dbContext.Blogs.FirstOrDefault(k => k.Id == Model.Id);
//Blogs? Post = Startup.dbContext.Blogs.FirstOrDefault(k => k.Id == Model.Id);
var request = await Startup.client.GetAsync(path + "/api/Blog/" + Model.Id);
Blogs? Post = JsonConvert.DeserializeObject<Blogs>(request.Content.ReadAsStringAsync().Result);
if (Post == null)
{
ViewData["Title"] = "Blogs";
@@ -66,7 +69,8 @@
else
{
ViewData["Title"] = "Blog";
var allBlogs = Startup.dbContext.Blogs.ToArray();
var request = await Startup.client.GetAsync(path + "/api/Blog/");
Blogs[]? allBlogs = JsonConvert.DeserializeObject<Blogs[]>(request.Content.ReadAsStringAsync().Result);
if (allBlogs.Length == 0)
{
<p>

View File

@@ -7,9 +7,9 @@
<h3 align="left" class="readme" >README.md</h3>
@{
string Github_readme = Startup.client.GetStringAsync(Startup.readmeFile).Result;
string path = $"{this.Request.Scheme}://{this.Request.Host}";
string Github_readme = Startup.client.GetStringAsync($"{path}/api/Index").Result;
<div class="text"> @Html.Raw(Github_readme) </div>
}

View File

@@ -20,6 +20,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
</ItemGroup>