using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Blog.Core.Common; using Blog.Core.IServices; using Blog.Core.Model.Models; using Blog.Core.Model.VeiwModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace Blog.Core.Controllers { /// /// Blog控制器所有接口 /// [Produces("application/json")] [Route("api/Blog")] //[Authorize(Roles = "Admin")] //[Authorize(Policy = "Admin")] public class BlogController : Controller { IAdvertisementServices advertisementServices; IBlogArticleServices blogArticleServices; IRedisCacheManager redisCacheManager; /// /// 构造函数 /// /// /// /// public BlogController(IAdvertisementServices advertisementServices, IBlogArticleServices blogArticleServices, IRedisCacheManager redisCacheManager) { this.advertisementServices = advertisementServices; this.blogArticleServices = blogArticleServices; this.redisCacheManager = redisCacheManager; } /// /// 获取博客列表 /// /// [HttpGet] [Route("GetBlogs")] public async Task> GetBlogs() { //var connect=Appsettings.app(new string[] { "AppSettings", "RedisCaching" , "ConnectionString" });//按照层级的顺序,依次写出来 List blogArticleList = new List(); if (redisCacheManager.Get("Redis.Blog") != null) { blogArticleList = redisCacheManager.Get>("Redis.Blog"); } else { blogArticleList = await blogArticleServices.Query(d => d.bID > 5); redisCacheManager.Set("Redis.Blog", blogArticleList, TimeSpan.FromHours(2)); } return blogArticleList; } // GET: api/Blog/5 /// /// 获取详情 /// /// /// [HttpGet("{id}", Name = "Get")] public async Task Get(int id) { var model = await blogArticleServices.getBlogDetails(id); var data = new { success = true, data = model }; return data; } /// /// 获取详情 /// /// /// [HttpGet] [Route("GetBlogsCache")] public async Task GetBlogsCache() { var model = await blogArticleServices.getBlogs(); var data = new { success = true, data = model }; return data; } } }