Profiles.Activity.Utilities.DataIO.GetFreshCache C# (CSharp) Method

GetFreshCache() private method

private GetFreshCache ( ) : Activity>.SortedList
return Activity>.SortedList
        private SortedList<Int64, Activity> GetFreshCache()
        {
            SortedList<Int64, Activity> cache = (SortedList<Int64, Activity>)Framework.Utilities.Cache.FetchObject("ActivityHistory");
            object isFresh = Framework.Utilities.Cache.FetchObject("ActivityHistoryIsFresh");
            if (cache == null)
            {
                // Grab a whole new one. This is expensive and should be unnecessary if we manage getting new ones well, so we don't do this often
                cache = GetRecentActivity(-1, activityCacheSize, true);
                Framework.Utilities.Cache.SetWithTimeout("ActivityHistory", cache, cacheExpirationSeconds);
            }
            else if (isFresh == null)
            {
                lock(syncLock)
                {
                    // get new ones from the DB

                    Int64 lastActivityLogID = cache.Count == 0 ? -1 : cache.Values[0].Id;
                    SortedList<Int64, Activity> newActivities = GetRecentActivity(lastActivityLogID, activityCacheSize, false);
                    // in with the new
                    foreach (Activity activity in newActivities.Values)
                    {
                        cache.Add(activity.Id, activity);
                    }
                    // out with the old
                    while (cache.Count > activityCacheSize)
                    {
                        cache.RemoveAt(cache.Count - 1);
                    }
                }
                // look for new activities once every minute
                Framework.Utilities.Cache.SetWithTimeout("ActivityHistoryIsFresh", new object(), chechForNewActivitiesSeconds);
            }
            return cache;
        }