示例#1
0
        /// <summary>
        /// Gets the memory cache key to use for the cachekey specified.
        /// </summary>
        /// <param name="originalKey">The original key.</param>
        /// <param name="keepMemoryCacheKey">if set to <c>true</c> it will keep the key in the storage</param>
        /// <param name="duration">The duration, if available, the cachekey is valid. Only used if keepMemoryCacheKey is true. If TimeSpan.Zero, it's considered
        /// not available/specified</param>
        /// <param name="tag">The tag, if available. Only used if keepMemoryCacheKey is true.</param>
        /// <returns>
        /// the string equivalent of the guid associated with the cachekey specified
        /// </returns>
        private string GetMemoryCacheKey(CacheKey originalKey, bool keepMemoryCacheKey, TimeSpan duration, string tag)
        {
            Guid toReturn = Guid.Empty;

            using (TimedLock.Lock(_semaphore))
            {
                if (!_cacheKeyToMemoryCacheKey.TryGetValue(originalKey, out toReturn))
                {
                    toReturn = Guid.NewGuid();
                    if (keepMemoryCacheKey)
                    {
                        _cacheKeyToMemoryCacheKey.Add(originalKey, toReturn);
                        if (!string.IsNullOrEmpty(tag))
                        {
                            _cacheKeysPerTag.Add(tag, originalKey);
                        }

                        if (duration != TimeSpan.Zero)
                        {
                            _cacheKeysAndExpireDates.Add(new ValuePair <CacheKey, DateTime>(originalKey, DateTime.Now.ToUniversalTime().Add(duration)));
                        }
                    }
                }
            }

            return(toReturn.ToString());
        }
示例#2
0
        /// <summary>
        /// Retrieves for all available sections all forums with all relevant statistical information. This information is stored per forum in an
        /// AggregatedForumRow instance. The forum instances are indexed under their sectionid. Only forums which are vieable by the user are returned.
        /// </summary>
        /// <param name="availableSections">SectionCollection with all available sections</param>
        /// <param name="accessableForums">List of accessable forums IDs.</param>
        /// <param name="forumsWithThreadsFromOthers">The forums for which the calling user can view other users' threads. Can be null</param>
        /// <param name="userId">The userid of the calling user.</param>
        /// <returns>
        /// MultiValueHashtable with per key (sectionID) a set of AggregatedForumRow instance, one row per forum in the section. If a section contains no forums
        /// displayable to the user it's not present in the returned hashtable.
        /// </returns>
        public static async Task <MultiValueHashtable <int, AggregatedForumRow> > GetAllAvailableForumsAggregatedData(EntityCollection <SectionEntity> availableSections,
                                                                                                                      List <int> accessableForums,
                                                                                                                      List <int> forumsWithThreadsFromOthers, int userId)
        {
            var toReturn = new MultiValueHashtable <int, AggregatedForumRow>();

            // return an empty list, if the user does not have a valid list of forums to access
            if (accessableForums == null || accessableForums.Count <= 0)
            {
                return(toReturn);
            }

            // fetch all forums with statistics in a dynamic list, while filtering on the list of accessable forums for this user.
            // Create the filter separate of the query itself, as it's re-used multiple times.
            var threadFilter = ThreadGuiHelper.CreateThreadFilter(forumsWithThreadsFromOthers, userId);

            var qf = new QueryFactory();
            var q  = qf.Create()
                     .Select(() => new AggregatedForumRow()
            {
                ForumID              = ForumFields.ForumID.ToValue <int>(),
                ForumName            = ForumFields.ForumName.ToValue <string>(),
                ForumDescription     = ForumFields.ForumDescription.ToValue <string>(),
                ForumLastPostingDate = ForumFields.ForumLastPostingDate.ToValue <DateTime?>(),

                // add a scalar query which retrieves the # of threads in the specific forum.
                // this will result in the query:
                // (
                //		SELECT COUNT(ThreadID) FROM Thread
                //		WHERE ForumID = Forum.ForumID AND threadfilter.
                // ) As AmountThreads
                AmountThreads = qf.Create()
                                .Select(ThreadFields.ThreadID.Count())
                                .CorrelatedOver(ThreadFields.ForumID.Equal(ForumFields.ForumID))
                                .Where(threadFilter)
                                .ToScalar().As("AmountThreads").ToValue <int>(),

                // add a scalar query which retrieves the # of messages in the threads of this forum.
                // this will result in the query:
                // (
                //		SELECT COUNT(MessageID) FROM Message
                //		WHERE ThreadID IN
                //		(
                //			SELECT ThreadID FROM Thread WHERE ForumID = Forum.ForumID AND threadfilter
                //		)
                // ) AS AmountMessages
                AmountMessages = qf.Create()
                                 .Select(MessageFields.MessageID.Count())
                                 .Where(MessageFields.ThreadID.In(
                                            qf.Create()
                                            .Select(ThreadFields.ThreadID)
                                            .CorrelatedOver(ThreadFields.ForumID.Equal(ForumFields.ForumID))
                                            .Where(threadFilter)))
                                 .ToScalar().As("AmountMessages").ToValue <int>(),
                HasRSSFeed = ForumFields.HasRSSFeed.ToValue <bool>(),
                SectionID  = ForumFields.SectionID.ToValue <int>()
            })
                     .Where(ForumFields.ForumID.In(accessableForums))
                     .OrderBy(ForumFields.OrderNo.Ascending(), ForumFields.ForumName.Ascending())
                     .CacheResultset(Globals.DefaultCacheDurationOfResultsets);

            using (var adapter = new DataAccessAdapter())
            {
                var results = await adapter.FetchQueryAsync(q).ConfigureAwait(false);

                foreach (var forum in results)
                {
                    toReturn.Add(forum.SectionID, forum);
                }
            }

            return(toReturn);
        }