public static StateStub[] GetStateFromCacheOrDatabase(Guid[] roomGuids, int usrK, Guid dsiGuid, RoomPinSet roomPinSet) { try { Dictionary<Guid, RoomPin> roomPins = new Dictionary<Guid, RoomPin>(); if (roomPinSet != null) roomPinSet.ToList().ForEach((rp) => { if (!roomPins.ContainsKey(rp.RoomGuid)) roomPins.Add(rp.RoomGuid, rp); }); string[] serializedStatesFromCache = null; if (usrK > 0) serializedStatesFromCache = Caching.Instances.Main.MultiGet<string>(roomGuids.ConvertAll(g => Chat.GetRoomStateCacheHolderKey(usrK, g).ToString())); else serializedStatesFromCache = Caching.Instances.Main.MultiGet<string>(roomGuids.ConvertAll(g => Chat.GetRoomStateCacheHolderKey(dsiGuid, g).ToString())); if (serializedStatesFromCache.Length == 0) return null; Dictionary<Guid, StateStub> states = new Dictionary<Guid, StateStub>(); foreach (string serializedState in serializedStatesFromCache) { if (serializedState != null && serializedState.Length > 0) { try { StateStub s = deSerializeStateStub(serializedState); if (s != null && s.guid.Length > 0) states.Add(s.guid.UnPackGuid(), s); } catch { } } } if (roomPins.Count > 0) { foreach (Guid g in roomGuids) { if (!states.ContainsKey(g) && roomPins.ContainsKey(g) && roomPins[g].StateStub != null && roomPins[g].StateStub.Length > 0) { try { StateStub ss = Chat.deSerializeStateStub(roomPins[g].StateStub); if (ss != null && ss.guid.Length > 0) states.Add(g, ss); } catch { } } } } if (states.Count == 0) return null; else { StateStub[] ssArray = new StateStub[states.Count]; states.Values.CopyTo(ssArray, 0); return ssArray; } } catch { return null; } }
static void Main(string[] args) { Query onlineUsrsQuery = new Query(); onlineUsrsQuery.Columns = new ColumnSet(Usr.Columns.K); onlineUsrsQuery.QueryCondition = new Q(Usr.Columns.DateTimeLastPageRequest, QueryOperator.GreaterThan, DateTime.Now.AddMinutes(-5)); UsrSet us = new UsrSet(onlineUsrsQuery); foreach (Usr u in us) { try { string ticks = Caching.Instances.Main.Get(Chat.GetStateDirtyKey(u.K).ToString()) as string; if (ticks != null && ticks.Length > 1) { DateTime dtLastDirty = new DateTime(long.Parse(ticks)); if (dtLastDirty > DateTime.Now.AddDays(-1) && dtLastDirty < DateTime.Now.AddSeconds(Vars.DevEnv ? -5 : -30)) { Query q = new Query(); q.QueryCondition = new Q(RoomPin.Columns.UsrK, u.K); q.Columns = new ColumnSet(RoomPin.Columns.RoomGuid, RoomPin.Columns.StateStub, RoomPin.Columns.UsrK); RoomPinSet rps = new RoomPinSet(q); StateStub[] statesArray = Chat.GetStateFromCache(rps.ToList().ConvertAll(rp => rp.RoomGuid).ToArray(), u.K, Guid.Empty); Dictionary<Guid, StateStub> states = new Dictionary<Guid, StateStub>(); if (statesArray != null) { foreach (StateStub state in statesArray) { Guid g = state.guid.UnPackGuid(); if (!states.ContainsKey(g)) states.Add(g, state); } } foreach (RoomPin rp in rps) { if (states.ContainsKey(rp.RoomGuid)) { rp.StateStub = Chat.SerializeStateStub(states[rp.RoomGuid]); rp.Update(); } } Caching.Instances.Main.Set(Chat.GetStateDirtyKey(u.K).ToString(), "0"); Console.Write("."); } else Console.Write("-"); } else Console.Write("/"); } catch(Exception ex) { Console.Write("X"); } } }
void ensureAllRoomsAreInDatabase(Dictionary<Guid, RoomStub> rooms, List<Guid> roomsOrder, RoomPinSet roomPinSet) { if (Usr.Current != null) { Dictionary<Guid, RoomPin> roomPins = new Dictionary<Guid, RoomPin>(); if (roomPinSet != null) roomPinSet.ToList().ForEach((rp) => { if (!roomPins.ContainsKey(rp.RoomGuid)) roomPins.Add(rp.RoomGuid, rp); }); foreach (Guid g in roomsOrder) { if (!roomPins.ContainsKey(g)) { try { Chat.RoomSpec spec = Chat.RoomSpec.FromGuid(g); RoomPin rp = new RoomPin(); rp.DateTime = DateTime.Now; rp.ListOrder = rooms[g].listOrder; rp.Pinned = rooms[g].pinned; rp.RoomGuid = g; rp.Starred = rooms[g].starred; rp.UsrK = Usr.Current.K; rp.Update(); } catch { } } } } }