src/NetCoreBBS/ViewComponents/Statistics.cs

(完整代码) 1KB

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using NetCoreBBS.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NetCoreBBS.ViewComponents
{
    public class Statistics : ViewComponent
    {
        private readonly DataContext db;
        private IMemoryCache _memoryCache;
        private string cachekey = "statistics";

        public Statistics(DataContext context, IMemoryCache memoryCache)
        {
            db = context;
            _memoryCache = memoryCache;
        }

        public IViewComponentResult Invoke()
        {
            var allstatistics = new Tuple<int, int, int>(0, 0, 0);
            if (!_memoryCache.TryGetValue(cachekey, out allstatistics))
            {
                var usercount = db.Users.Count();
                var topiccount = db.Topics.Count();
                var replycount = db.TopicReplys.Count();
                allstatistics = new Tuple<int, int, int>(usercount, topiccount, replycount);
                _memoryCache.Set(cachekey, allstatistics, TimeSpan.FromMinutes(1));
            }
            return View(allstatistics);
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。