예제 #1
0
        /// <summary>
        /// Removes the session key from internal and DB storage
        /// </summary>
        /// <param name="sessionKey">The session key of the session to remove</param>
        private void RemoveFromStorage(string sessionKey)
        {
            SessionStoreData session = new SessionStoreData();

            session.SessionKey = sessionKey;
            this.updatesToStore.Enqueue(session);
            if (this.internalStore.ContainsKey(sessionKey))
            {
                string output = null;
                this.internalStore.TryRemove(sessionKey, out output);
            }
        }
예제 #2
0
        /// <summary>
        /// Updates the database based upon SessionStoreData in the UpdatesToStore queue.
        /// If the SessioStoreData.SessionData entry is null, the data is removed.
        /// Runs in a loop until either Token.IsCancellationRequested is true.
        /// Will finish updating the database when we are disposing and there are still updates to store
        /// </summary>
        private void UpdateDatabase()
        {
            while (!this.token.IsCancellationRequested || (this.disposing && this.updatesToStore.Count > 0))
            {
                SessionContext context = new SessionContext();
                int            count   = 0;
                if (this.updatesToStore.Count > 0)
                {
                    while (this.updatesToStore.Count > 0 && count < 100)
                    {
                        count++;
                        SessionStoreData session = this.updatesToStore.Dequeue();
                        if (string.IsNullOrWhiteSpace(session.SessionData))
                        {
                            if (context.SessionStore.Any(s => s.SessionKey == session.SessionKey))
                            {
                                context.SessionStore.Local.Where(s => s.SessionKey == session.SessionKey).ToList().ForEach(s => context.Entry(s).State = System.Data.Entity.EntityState.Deleted);
                            }
                            else
                            {
                                // Crashed on duplicate key error
                                context.Entry(session).State = System.Data.Entity.EntityState.Deleted;
                            }
                        }
                        else// if (this.internalStore.ContainsKey(session.SessionKey))
                        {
                            if (context.SessionStore.Local.Any(s => s.SessionKey == session.SessionKey) && context.Entry(context.SessionStore.Local.Where(s => s.SessionKey == session.SessionKey).Single()).State != System.Data.Entity.EntityState.Deleted)
                            {
                                context.SessionStore.Local.Where(s => s.SessionKey == session.SessionKey).Single().SessionData = session.SessionData;
                            }
                            else if (context.SessionStore.Any(s => s.SessionKey == session.SessionKey))
                            {
                                context.Entry(session).State = System.Data.Entity.EntityState.Modified;
                            }
                            else
                            {
                                context.Entry(session).State = System.Data.Entity.EntityState.Added;
                            }
                        }
                    }

                    if (count > 0)
                    {
                        context.SaveChanges();
                    }
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Updates the interal storage and the database storage and keeps them in sync
        /// </summary>
        /// <param name="sessionKey">The sessionKey to store</param>
        /// <param name="sessionData">The session Data to store</param>
        private void UpdateStorage(string sessionKey, string sessionData)
        {
            SessionStoreData session = new SessionStoreData();

            session.SessionKey  = sessionKey;
            session.SessionData = sessionData;

            if (!this.internalStore.ContainsKey(sessionKey))
            {
                this.internalStore.TryAdd(sessionKey, sessionData);
            }
            else
            {
                this.internalStore[sessionKey] = sessionData;
            }

            this.updatesToStore.Enqueue(session);
        }