mirror of
https://github.com/yawaflua/yaflay.ru.git
synced 2026-02-04 19:04:12 +02:00
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:
@@ -4,13 +4,23 @@ using Microsoft.AspNetCore.RateLimiting;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
using yaflay.ru.Models.Tables;
|
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.Новая_папка
|
namespace yaflay.ru.Новая_папка
|
||||||
{
|
{
|
||||||
[Route("")]
|
[Route("")]
|
||||||
public class HomeController : Controller
|
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 class commentBody
|
||||||
{
|
{
|
||||||
public string text { get; set; }
|
public string text { get; set; }
|
||||||
@@ -31,6 +41,20 @@ namespace yaflay.ru.Новая_папка
|
|||||||
public string author { get; set; }
|
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")]
|
[HttpPost("api/redirects")]
|
||||||
public async Task<IActionResult> createRedirectUri([FromBody]redirectBody body)
|
public async Task<IActionResult> createRedirectUri([FromBody]redirectBody body)
|
||||||
{
|
{
|
||||||
@@ -51,8 +75,8 @@ namespace yaflay.ru.Новая_папка
|
|||||||
redirectTo = body.url,
|
redirectTo = body.url,
|
||||||
uri = body.uri
|
uri = body.uri
|
||||||
};
|
};
|
||||||
await Startup.dbContext.Redirects.AddAsync(redirects);
|
await ctx.Redirects.AddAsync(redirects);
|
||||||
await Startup.dbContext.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -87,8 +111,8 @@ namespace yaflay.ru.Новая_папка
|
|||||||
Title = body.title,
|
Title = body.title,
|
||||||
authorNickname = response["user"]["global_name"].ToString()
|
authorNickname = response["user"]["global_name"].ToString()
|
||||||
};
|
};
|
||||||
await Startup.dbContext.Blogs.AddAsync(article);
|
await ctx.Blogs.AddAsync(article);
|
||||||
await Startup.dbContext.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
return Ok(body);
|
return Ok(body);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -117,8 +141,14 @@ namespace yaflay.ru.Новая_папка
|
|||||||
[HttpGet("api/Blog/{blogId?}/comments")]
|
[HttpGet("api/Blog/{blogId?}/comments")]
|
||||||
public async Task<IActionResult> blogComments(int? blogId)
|
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);
|
return Ok(comments);
|
||||||
}
|
}
|
||||||
[HttpPost("api/Blog/{blogId}/comments")]
|
[HttpPost("api/Blog/{blogId}/comments")]
|
||||||
@@ -133,27 +163,53 @@ namespace yaflay.ru.Новая_папка
|
|||||||
Text = body.text,
|
Text = body.text,
|
||||||
postId = blogId
|
postId = blogId
|
||||||
};
|
};
|
||||||
await Startup.dbContext.Comments.AddAsync(comment);
|
await ctx.Comments.AddAsync(comment);
|
||||||
await Startup.dbContext.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("api/Blog/{blogId}")]
|
[HttpGet("api/Blog/{blogId}")]
|
||||||
public async Task<IActionResult> blog(int blogId)
|
public async Task<IActionResult> blog(int 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));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Blogs? blog = Startup.dbContext.Blogs.FirstOrDefault(k => k.Id == blogId);
|
|
||||||
return Ok(blog);
|
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}")]
|
[HttpGet("{uri}")]
|
||||||
public async Task<IActionResult> FromGitHub(string uri)
|
public async Task<IActionResult> FromGitHub(string uri)
|
||||||
{
|
{
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
string? url = Startup.dbContext.Redirects.FirstOrDefault(k => k.uri == uri)?.redirectTo;
|
return Redirect(fromCache?.redirectTo ?? "/404");
|
||||||
return Redirect(url ?? "/404");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
@page "{id?}"
|
@page "{id?}"
|
||||||
@model BlogModel
|
@model BlogModel
|
||||||
@using yaflay.ru.Models.Tables
|
@using yaflay.ru.Models.Tables
|
||||||
|
@using Newtonsoft.Json
|
||||||
@{
|
@{
|
||||||
string path = $"{this.Request.Scheme}://{this.Request.Host}";
|
string path = $"{this.Request.Scheme}://{this.Request.Host}";
|
||||||
if (Model.Id != 0)
|
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)
|
if (Post == null)
|
||||||
{
|
{
|
||||||
ViewData["Title"] = "Blogs";
|
ViewData["Title"] = "Blogs";
|
||||||
@@ -66,7 +69,8 @@
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
ViewData["Title"] = "Blog";
|
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)
|
if (allBlogs.Length == 0)
|
||||||
{
|
{
|
||||||
<p>
|
<p>
|
||||||
|
|||||||
@@ -7,9 +7,9 @@
|
|||||||
<h3 align="left" class="readme" >README.md</h3>
|
<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>
|
<div class="text"> @Html.Raw(Github_readme) </div>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user