-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathTopicRankList.cs
More file actions
46 lines (43 loc) · 1.38 KB
/
Copy pathTopicRankList.cs
File metadata and controls
46 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using NetCoreBBS.Infrastructure;
using NetCoreBBS.Entities;
namespace NetCoreBBS.ViewComponents
{
public class TopicRankList: ViewComponent
{
private readonly DataContext db;
private IMemoryCache _memoryCache;
private string cachekey = "topicrank";
public TopicRankList(DataContext context, IMemoryCache memoryCache)
{
db = context;
_memoryCache = memoryCache;
}
public IViewComponentResult Invoke(int days)
{
var items = new List<Topic>();
if (!_memoryCache.TryGetValue(cachekey, out items))
{
items = GetRankTopics(10, days);
_memoryCache.Set(cachekey, items, TimeSpan.FromMinutes(10));
}
return View(items);
}
/// <summary>
/// 获取主题排行
/// </summary>
/// <param name="top"></param>
/// <param name="days"></param>
/// <returns></returns>
private List<Topic> GetRankTopics(int top,int days)
{
return db.Topics.Where(r=>r.CreateOn>DateTime.Now.AddDays(-days))
.OrderByDescending(r=>r.ViewCount).Take(top).ToList();
}
}
}