public PointShowUserCollection GetTopUserShows(int userID, int count) { PointShowUserCollection users; PointShowUserCollection result = new PointShowUserCollection(); if (CacheUtil.TryGetValue <PointShowUserCollection>(showUsersKey, out users)) { int i = 0; foreach (PointShowUser user in users) { result.Add(user); i++; if (i == count) { break; } } return(users); } Dictionary <int, int> points; Dictionary <int, string> contents; UserCollection temp = GetUserShows(userID, 1, count, out points, out contents); users = new PointShowUserCollection(); int m = 0; foreach (User u in temp) { PointShowUser pointShowUser = new PointShowUser(); pointShowUser.Content = contents[u.UserID]; pointShowUser.Price = points[u.UserID]; pointShowUser.UserID = u.UserID; users.Add(pointShowUser); if (m < count) { result.Add(pointShowUser); m++; } } CacheUtil.Set <PointShowUserCollection>(showUsersKey, users); return(result); }
public void UpdateTopUserShowsWhenVisited(int userID, int price) { PointShowUserCollection users; if (CacheUtil.TryGetValue <PointShowUserCollection>(showUsersKey, out users)) { PointShowUser user = users.GetValue(userID); if (user == null) { return; } //如果比缓存里最小的单价还小 那么需要清除缓存 if (users[users.Count - 1].Price > price) { ClearTopUserShowsCache(); return; } PointShowUserCollection newUsers = new PointShowUserCollection(); foreach (PointShowUser tempUser in users) { if (tempUser.UserID == userID) { continue; } if (price > tempUser.Price) { newUsers.Add(tempUser); } newUsers.Add(tempUser); } CacheUtil.Set <PointShowUserCollection>(showUsersKey, newUsers); } }