网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System.Collections.Specialized;
using System.Net.Http;
using System.Net.Http.Json;
using Dpz.Core.Public.ViewModel.V4;
using Dpz.Core.Service.V4.Services;
using Dpz.Core.Web.Models;

namespace Dpz.Core.Web.Library.Api.Service;

public class HomeCacheService(
    IArticleService articleService,
    IHybridCachingProvider cachingProvider,
    IPictureRecordService pictureRecordService,
    IHttpContextAccessor httpContextAccessor,
    IHttpClientFactory httpClientFactory,
    ILogger<HomeCacheService> logger)
    : IHomeCacheService
{
    /// <summary>
    /// 过期时间
    /// </summary>
    private readonly TimeSpan _expired = TimeSpan.FromHours(3);

    /// <summary>
    /// article key
    /// </summary>
    private const string HomeKey = "home-index";

    //_blogService = blogService;

    private async Task<IList<VmPictureRecord>> GetBannersAsync()
    {
        var cacheValue = await cachingProvider.GetAsync<List<VmPictureRecord>>(CacheKey.BannerKey);
        if (!cacheValue.IsNull && cacheValue.HasValue)
            return cacheValue.Value;

        var banner = await pictureRecordService.GetBannerAsync();
        await cachingProvider.SetAsync(CacheKey.BannerKey, banner, _expired);
        return banner.ToList();
    }

    public async Task<HomeContainerModel> GetCacheDataAsync()
    {
        var cache = await cachingProvider.GetAsync<HomeContainerModel>(HomeKey);
        if (!cache.IsNull && cache.HasValue)
        {
            return cache.Value;
        }

        var model = new HomeContainerModel
        {
            Blogs = await GetTopArticlesAsync(),
            LikeArticle = await GetRandomArticlesAsync(),
            News = await GetLatestArticlesAsync(),
            Pictures = await GetBannersAsync(),
            Tags = await GetArticleTagsAsync(),
        };
        await cachingProvider.SetAsync(HomeKey, model, _expired);
        return model;
    }

    public async Task UpdateBannerAsync()
    {
        var banner = await pictureRecordService.GetBannerAsync();
        foreach (var item in banner)
        {
            item.AccessUrl += "!banner";
        }
        await cachingProvider.SetAsync(CacheKey.BannerKey, banner, _expired);
    }

    public async Task<IList<string>> GetArticleTagsAsync()
    {
        var cache = await cachingProvider.GetAsync<List<string>>(CacheKey.ArticleTags);
        if (!cache.IsNull && cache.HasValue)
        {
            return cache.Value;
        }

        var tags = await articleService.GetAllTagsAsync();
        await cachingProvider.SetAsync(CacheKey.ArticleTags, tags, _expired);
        return tags.ToList();
    }

    public async Task<IList<VmArticleMini>> GetLatestArticlesAsync()
    {
        var cacheValue = await cachingProvider.GetAsync<List<VmArticleMini>>(CacheKey.NewsArticleKey);
        if (!cacheValue.IsNull && cacheValue.HasValue)
            return cacheValue.Value;

        var list = await articleService.GetLatestAsync();
        await cachingProvider.SetAsync(CacheKey.NewsArticleKey, list, _expired);
        return list.ToList();
    }

    public async Task UpdateLatestArticlesAsync()
    {
        var list = await articleService.GetLatestAsync();
        await cachingProvider.SetAsync(CacheKey.NewsArticleKey, list, _expired);
    }

    public async Task<IList<VmArticleMini>> GetRandomArticlesAsync()
    {
        var cacheValue = await cachingProvider.GetAsync<List<VmArticleMini>>(CacheKey.RandomArticleKey);
        if (!cacheValue.IsNull && cacheValue.HasValue)
            return cacheValue.Value;

        var list = await articleService.GetRandomArticlesAsync();
        await cachingProvider.SetAsync(CacheKey.RandomArticleKey, list, _expired);
        return list.ToList();
    }

    public async Task<TodayNewsModel?> GetTodayNewsAsync()
    {
        var ipAddress = httpContextAccessor.HttpContext?.Request.GetIpAddress() ?? "";
        var todayNewsKey = $"home-news-{ipAddress}";

        var cacheValue = await cachingProvider.GetAsync<TodayNewsModel>(todayNewsKey);
        if (!cacheValue.IsNull && cacheValue.HasValue)
        {
            return cacheValue.Value;
        }


        var httpClient = httpClientFactory.CreateClient("edge");

        try
        {
            var parameters = new NameValueCollection
            {
                { "ip", ipAddress }
            };
            var request = new HttpRequestMessage(HttpMethod.Get, "https://news.topurl.cn/api?" + parameters.ToQueryString());
            var response = await httpClient.SendAsync(request);
            if (!response.IsSuccessStatusCode)
            {
                logger.LogWarning("get today news fail,status code:{Code}", response.StatusCode);
                return null;
            }
            var data = await response.Content.ReadFromJsonAsync<NewsResponse>();

            await cachingProvider.SetAsync(todayNewsKey, data?.Data, _expired);
            return data?.Data;
        }
        catch (Exception ex)
        {
            logger.LogError(ex,"get today news fail");
            return null;
        }
    }

    private async Task<List<VmArticleMini>> GetTopArticlesAsync()
    {
        var cache = await cachingProvider.GetAsync<List<VmArticleMini>>(CacheKey.TopArticleKey);
        if (!cache.IsNull && cache.HasValue)
        {
            return cache.Value;
        }

        var articleMinis = await articleService.GetTopArticlesAsync();
        await cachingProvider.SetAsync(CacheKey.TopArticleKey, articleMinis, _expired);
        return articleMinis.ToList();
    }

    public async Task UpdateViewTopAsync()
    {
        var articleMinis = await articleService.GetTopArticlesAsync();
        await cachingProvider.SetAsync(CacheKey.TopArticleKey, articleMinis, _expired);
    }
}
loading