Example #1
0
        /// <summary>
        ///  Get the Forum Info - using examine, because that can be faster when
        ///  there are lots and lots of posts - although i've yet to see it
        ///  really get faster than the traverse method (yet)
        /// </summary>
        /// <param name="item"></param>
        /// <param name="useExamine">true to use examine</param>
        public static ForumCacheItem GetForumInfo(this IPublishedContent item, bool useExamine)
        {
            if (!useExamine)
            {
                return(GetForumInfo(item));
            }

            ExamineManager.Instance.TryGetSearcher("InternalSearcher", out _);

            var cacheName = $"forum_{item.Id}";
            var cache     = Current.AppCaches.RuntimeCache;
            var forumInfo = cache.GetCacheItem <ForumCacheItem>(cacheName);

            if (forumInfo != null)
            {
                return(forumInfo);
            }

            //Stopwatch sw = new Stopwatch();
            //sw.Start();

            forumInfo = new ForumCacheItem();

            ISearchResults searchResults = queryLastPostIndex(item, item.Name);
            ISearchResults updateResults = queryLastEditIndex(item, item.Name);

            forumInfo.Count      = searchResults.ToList().Count - 1;
            forumInfo.latestPost = DateTime.MinValue;

            if (searchResults.Any())
            {
                var lastpostdate = new DateTime(Convert.ToInt64(searchResults.First().Values["createDate"]));
                if (lastpostdate > DateTime.MinValue)
                {
                    forumInfo.latestPost = lastpostdate;
                }
                forumInfo.lastpostAuthor = searchResults.First().Values["postCreator"];
            }
            if (updateResults.Any())
            {
                var latestedit = new DateTime(Convert.ToInt64(searchResults.First().Values["editDate"]));
                if (latestedit > DateTime.MinValue)
                {
                    forumInfo.latestEdit = latestedit;
                }
            }

            cache.InsertCacheItem <ForumCacheItem>(cacheName, () => forumInfo);

            return(forumInfo);
        }
Example #2
0
        public static ForumCacheItem GetForumInfo(this IPublishedContent item)
        {
            var cacheName = $"forum_{item.Id}";
            var cache     = Current.AppCaches.RuntimeCache;
            var forumInfo = cache.GetCacheItem <ForumCacheItem>(cacheName);

            if (forumInfo != null)
            {
                return(forumInfo);
            }

            // not in the cache, we have to make it.
            forumInfo = new ForumCacheItem();

            var posts = item.Descendants().Where(x => x.IsVisible() && x.IsDocumentType("forumPost")).ToList();

            forumInfo.Count = posts.Count();
            if (posts.Any())
            {
                var lastPost = posts.OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (lastPost != null)
                {
                    forumInfo.latestPost = lastPost.CreateDate;
                }
                forumInfo.lastpostAuthor = lastPost.Value <string>(("postCreator"));

                var lastedit = posts.OrderByDescending(x => x.Value <DateTime>("editDate")).FirstOrDefault();
                if (lastedit != null)
                {
                    forumInfo.latestEdit = lastedit.Value <DateTime>("editDate");
                }
            }

            cache.GetCacheItem <ForumCacheItem>(cacheName, () => forumInfo);

            return(forumInfo);
        }