/// <summary> /// 获取文章信息 /// 结果会按文章Id和当前登录用户缓存一定时间 /// </summary> /// <param name="articleId">文章Id</param> /// <returns></returns> public virtual object GetArticleApiInfo(Guid articleId) { return(ArticleApiInfoCache.GetOrCreate(articleId, () => { using (UnitOfWork.Scope()) { var article = Get(articleId); if (article == null) { return null; } var author = article.Author; var classes = article.Classes.Select(c => new { id = c.Id, name = c.Name }).ToList(); var tags = article.Tags.Select(t => new { id = t.Id, name = t.Name }).ToList(); var keywords = classes.Select(c => c.name).Concat(tags.Select(t => t.name)).ToList(); return new { id = article.Id, title = article.Title, summary = article.Summary, contents = article.Contents, authorId = author?.Id, authorName = author?.Username, classes, tags, keywords, createTime = article.CreateTime, updateTime = article.UpdateTime }; } }, ArticleApiInfoCacheTime)); }
/// <summary> /// 获取文章信息 /// 结果会按文章Id和当前登录用户缓存一定时间 /// </summary> /// <param name="articleId">文章Id</param> /// <returns></returns> public virtual object GetArticleApiInfo(long articleId) { // 从缓存中获取 var info = ArticleApiInfoCache.GetOrDefault(articleId); if (info != null) { return(info); } // 从数据库中获取 UnitOfWork.ReadData <Database.Article>(r => { var article = r.GetByIdWhereNotDeleted(articleId); if (article == null) { return; } var author = article.Author; var classes = article.Classes.Select(c => new { id = c.Id, name = c.Name }).ToList(); var tags = article.Tags.Select(t => new { id = t.Id, name = t.Name }).ToList(); var keywords = classes.Select(c => c.name).Concat(tags.Select(t => t.name)).ToList(); info = new { id = article.Id, title = article.Title, summary = article.Summary, contents = article.Contents, authorId = author == null ? null : (long?)author.Id, authorName = author == null ? null : author.Username, classes, tags, keywords, createTime = article.CreateTime, lastUpdated = article.LastUpdated }; // 保存到缓存中 ArticleApiInfoCache.Put(articleId, info, ArticleApiInfoCacheTime); }); return(info); }
/// <summary> /// 清理缓存 /// </summary> public void ClearCache() { ArticleApiInfoCache.Clear(); ArticleSearchResultCache.Clear(); }