public PostV5 GetPostFromCache(int postID) { PostV5 threadContent = ThreadContent; if (threadContent != null && threadContent.PostID == postID) { return(threadContent); } PostCollectionV5[] cachedPosts = cachedPagePosts; if (cachedPosts == null) { return(null); } PostV5 post; for (int i = 0; i < CacheFirstPageCount + CacheLastPageCount; i++) { PostCollectionV5 tempPosts = cachedPosts[i]; if (tempPosts != null && tempPosts.TryGetValue(postID, out post)) { return(post); } } return(null); }
public bool ContainPostCache(int postID) { PostV5 threadContent = ThreadContent; if (threadContent != null && threadContent.PostID == postID) { return(true); } PostCollectionV5[] cachedPosts = cachedPagePosts; if (cachedPosts == null) { return(false); } for (int i = 0; i < CacheFirstPageCount + CacheLastPageCount; i++) { PostCollectionV5 tempPosts = cachedPosts[i]; if (tempPosts != null && tempPosts.ContainsKey(postID)) { return(true); } } return(false); }
/// <summary> /// 更新缓存 如果缓存里不存在该帖子 则不进行任何操作 /// </summary> /// <param name="post"></param> public virtual void UpdatePostCache(PostV5 post) { lock (m_CachedPagePostsLocker) { PostV5 threadContent = ThreadContent; if (threadContent != null && post.PostID == threadContent.PostID) { ThreadContent = post; } else { if (cachedPagePosts != null) { for (int i = 0; i < CacheFirstPageCount + CacheLastPageCount; i++) { PostCollectionV5 posts = cachedPagePosts[i]; if (posts != null && posts.ContainsKey(post.PostID)) { posts.Set(post); break; } } } } } }
/// <summary> /// 临时 使用的 /// </summary> /// <param name="pageNumber"></param> /// <returns></returns> public bool ContainPageCache(int pageNumber) { if (cachedPagePosts == null) { return(false); } if (pageNumber <= CacheFirstPageCount || pageNumber > TotalPages - CacheLastPageCount) { PostCollectionV5 posts = cachedPagePosts[GetCachedPageIndex(pageNumber)]; if (posts == null || posts.Count == 0) { return(false); } else { return(true); } } return(false); }
public PostCollectionV5(PostCollectionV5 posts) : base(posts) { }
public void AddPostToCache(PostV5 post) { if (cachedPagePosts == null) { return; } int pageSize = AllSettings.Current.BbsSettings.PostsPageSize; lock (m_CachedPagePostsLocker) { int index = GetCachedPageIndex(TotalPages); //int total = TotalRepliesForPage + 1; //if (total % AllSettings.Current.BbsSettings.PostsPageSize > 0) //{ // //如果最后页 没缓存 那就不加入 // if (cachedPagePosts[index] == null) // { // return; // } //} PostCollectionV5 posts = cachedPagePosts[index]; if (posts == null) { posts = new PostCollectionV5(); } if (posts.Count > 0) { int tempi = -1; for (int i = posts.Count - 1; i > -1; i--) { if (post.PostID < posts[i].PostID) { tempi = i; } else { break; } } if (tempi > -1) { posts.Insert(tempi, post); post = posts[posts.Count - 1]; } } if (posts.Count >= pageSize) //需要新的页面 缓存 { PostCollectionV5 tempPosts = new PostCollectionV5(); tempPosts.Add(post); if (index == CacheFirstPageCount + CacheLastPageCount - 1)//已经是最后一页 { //需要新的页面 则把LastCachePage的最前面一页 删除 后面的页面往前挪一个位置 新页加到后面 PostCollectionV5[] tempPages = new PostCollectionV5[CacheFirstPageCount + CacheLastPageCount]; for (int i = 0; i < CacheFirstPageCount + CacheLastPageCount; i++) { if (i < CacheFirstPageCount) { tempPages[i] = cachedPagePosts[i]; } else { if (i + 1 < CacheFirstPageCount + CacheLastPageCount) { tempPages[i] = cachedPagePosts[i + 1]; } } } tempPages[CacheFirstPageCount + CacheLastPageCount - 1] = tempPosts; cachedPagePosts = tempPages; } else { cachedPagePosts[index + 1] = tempPosts; } } else { posts.Add(post); cachedPagePosts[index] = posts; } } }