Esempio n. 1
0
        /// <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);
        }