Beispiel #1
0
        public override void ResetItemTimeout(HttpContext context, string id)
        {
            EnsureGoodId(id, true);
            string            CacheId = CachePrefix + id;
            Cache             cache   = HttpRuntime.InternalCache;
            InProcSessionItem item    = cache [CacheId] as InProcSessionItem;

            if (item == null)
            {
                return;
            }

            try {
                item.rwlock.AcquireWriterLock(lockAcquireTimeout);
                item.resettingTimeout = true;
                cache.Remove(CacheId);
                InsertSessionItem(item, item.timeout, CacheId);
            } catch {
                throw;
            } finally {
                if (item.rwlock.IsWriterLockHeld)
                {
                    item.rwlock.ReleaseWriterLock();
                }
            }
        }
Beispiel #2
0
        void OnSessionRemoved(string key, object value, CacheItemRemovedReason reason)
        {
            if (expireCallback != null)
            {
                if (value is SessionStateStoreData)
                {
                    expireCallback(key, (SessionStateStoreData)value);
                }
                else if (value is InProcSessionItem)
                {
                    InProcSessionItem item = (InProcSessionItem)value;
                    if (item.resettingTimeout)
                    {
                        item.resettingTimeout = false;
                        return;
                    }

                    expireCallback(key,
                                   new SessionStateStoreData(
                                       item.items,
                                       item.staticItems,
                                       item.timeout));
                }
                else
                {
                    expireCallback(key, null);
                }
            }
        }
Beispiel #3
0
        public override void RemoveItem(HttpContext context,
                                        string id,
                                        object lockId,
                                        SessionStateStoreData item)
        {
            EnsureGoodId(id, true);
            string            CacheId    = CachePrefix + id;
            Cache             cache      = HttpRuntime.InternalCache;
            InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;

            if (inProcItem == null || lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
            {
                return;
            }

            try {
                inProcItem.rwlock.AcquireWriterLock(lockAcquireTimeout);
                cache.Remove(CacheId);
            } catch {
                throw;
            } finally {
                if (inProcItem.rwlock.IsWriterLockHeld)
                {
                    inProcItem.rwlock.ReleaseWriterLock();
                }
            }
        }
Beispiel #4
0
        public override void ReleaseItemExclusive(HttpContext context,
                                                  string id,
                                                  object lockId)
        {
            EnsureGoodId(id, true);
            string            CacheId = CachePrefix + id;
            InProcSessionItem item    = HttpRuntime.InternalCache [CacheId] as InProcSessionItem;

            if (item == null || lockId == null || lockId.GetType() != typeof(Int32) || item.lockId != (Int32)lockId)
            {
                return;
            }

            try {
                item.rwlock.AcquireWriterLock(lockAcquireTimeout);
                item.locked = false;
            } catch {
                throw;
            } finally {
                if (item.rwlock.IsWriterLockHeld)
                {
                    item.rwlock.ReleaseWriterLock();
                }
            }
        }
Beispiel #5
0
        void OnSessionRemoved(string key, object value, CacheItemRemovedReason reason)
        {
            if (expireCallback != null)
            {
                if (key.StartsWith(CachePrefix, StringComparison.OrdinalIgnoreCase))
                {
                    key = key.Substring(CachePrefixLength);
                }

                if (value is SessionStateStoreData)
                {
                    expireCallback(key, (SessionStateStoreData)value);
                }
                else if (value is InProcSessionItem)
                {
                    InProcSessionItem item = (InProcSessionItem)value;
                    if (item.resettingTimeout)
                    {
                        item.resettingTimeout = false;
                        return;
                    }

                    expireCallback(key,
                                   new SessionStateStoreData(
                                       item.items,
                                       item.staticItems,
                                       item.timeout));
                }
                else
                {
                    expireCallback(key, null);
                }
            }
        }
Beispiel #6
0
        public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
        {
            EnsureGoodId(id, true);
            InProcSessionItem item = new InProcSessionItem();

            item.expiresAt = DateTime.UtcNow.AddMinutes(timeout);
            item.timeout   = timeout;
            InsertSessionItem(item, timeout, CachePrefix + id);
        }
Beispiel #7
0
        /* In certain situations the 'item' parameter passed to SetAndReleaseItemExclusive
         * may be null. The issue was reported in bug #333898, but the reporter cannot
         * provide a test case that triggers the issue. Added work around the problem
         * in the way that should have the least impact on the rest of the code. If 'item'
         * is null, then the new session item is created without the items and staticItems
         * collections - they will be initialized to defaults when retrieving the session
         * item. This is not a correct fix, but since there is no test case this is the best
         * what can be done right now.
         */
        public override void SetAndReleaseItemExclusive(HttpContext context,
                                                        string id,
                                                        SessionStateStoreData item,
                                                        object lockId,
                                                        bool newItem)
        {
            EnsureGoodId(id, true);
            string                      CacheId    = CachePrefix + id;
            Cache                       cache      = HttpRuntime.InternalCache;
            InProcSessionItem           inProcItem = cache [CacheId] as InProcSessionItem;
            ISessionStateItemCollection itemItems  = null;
            int itemTimeout = 20;
            HttpStaticObjectsCollection itemStaticItems = null;

            if (item != null)
            {
                itemItems       = item.Items;
                itemTimeout     = item.Timeout;
                itemStaticItems = item.StaticObjects;
            }

            if (newItem || inProcItem == null)
            {
                inProcItem           = new InProcSessionItem();
                inProcItem.timeout   = itemTimeout;
                inProcItem.expiresAt = DateTime.UtcNow.AddMinutes(itemTimeout);
                if (lockId.GetType() == typeof(Int32))
                {
                    inProcItem.lockId = (Int32)lockId;
                }
            }
            else
            {
                if (lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
                {
                    return;
                }
                inProcItem.resettingTimeout = true;
                cache.Remove(CacheId);
            }

            try {
                inProcItem.rwlock.AcquireWriterLock(lockAcquireTimeout);
                inProcItem.locked      = false;
                inProcItem.items       = itemItems;
                inProcItem.staticItems = itemStaticItems;
                InsertSessionItem(inProcItem, itemTimeout, CacheId);
            } catch {
                throw;
            } finally {
                if (inProcItem.rwlock.IsWriterLockHeld)
                {
                    inProcItem.rwlock.ReleaseWriterLock();
                }
            }
        }
		void InsertSessionItem (InProcSessionItem item, int timeout, string id)
		{
			if (item == null || String.IsNullOrEmpty (id))
				return;

			HttpRuntime.InternalCache.Insert (id,
							  item,
							  null,
							  Cache.NoAbsoluteExpiration,
							  TimeSpan.FromMinutes (timeout),
							  CacheItemPriority.AboveNormal,
							  removedCB);
		}
Beispiel #9
0
        void InsertSessionItem(InProcSessionItem item, int timeout, string id)
        {
            if (item == null || String.IsNullOrEmpty(id))
            {
                return;
            }

            HttpRuntime.InternalCache.Insert(id,
                                             item,
                                             null,
                                             Cache.NoAbsoluteExpiration,
                                             TimeSpan.FromMinutes(timeout),
                                             CacheItemPriority.AboveNormal,
                                             removedCB);
        }
        public override void RemoveItem(HttpContext context,
                                        string id,
                                        object lockId,
                                        SessionStateStoreData item)
        {
            EnsureGoodId(id, true);
            string            CacheId    = CachePrefix + id;
            Cache             cache      = HttpRuntime.InternalCache;
            InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;

            if (inProcItem == null || lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
            {
                return;
            }

            bool locked = false;
            ReaderWriterLockSlim itemLock = null;

            try
            {
                itemLock = inProcItem.rwlock;
                if (itemLock != null && itemLock.TryEnterWriteLock(lockAcquireTimeout))
                {
                    locked = true;
                }
                else
                {
                    throw new ApplicationException("Failed to acquire lock after");
                }
                cache.Remove(CacheId);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (locked)
                {
                    itemLock.ExitWriteLock();
                }
            }
        }
        public override void ResetItemTimeout(HttpContext context, string id)
        {
            EnsureGoodId(id, true);
            string            CacheId = CachePrefix + id;
            Cache             cache   = HttpRuntime.InternalCache;
            InProcSessionItem item    = cache [CacheId] as InProcSessionItem;

            if (item == null)
            {
                return;
            }

            bool locked = false;
            ReaderWriterLockSlim itemLock = null;

            try
            {
                itemLock = item.rwlock;
                if (itemLock != null && itemLock.TryEnterWriteLock(lockAcquireTimeout))
                {
                    locked = true;
                }
                else
                {
                    throw new ApplicationException("Failed to acquire lock after");
                }
                item.resettingTimeout = true;
                cache.Remove(CacheId);
                InsertSessionItem(item, item.timeout, CacheId);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (locked && itemLock != null)
                {
                    itemLock.ExitWriteLock();
                }
            }
        }
Beispiel #12
0
        SessionStateStoreData GetItemInternal(HttpContext context,
                                              string id,
                                              out bool locked,
                                              out TimeSpan lockAge,
                                              out object lockId,
                                              out SessionStateActions actions,
                                              bool exclusive)
        {
            locked  = false;
            lockAge = TimeSpan.MinValue;
            lockId  = Int32.MinValue;
            actions = SessionStateActions.None;

            if (id == null)
            {
                return(null);
            }

            Cache             cache   = HttpRuntime.InternalCache;
            string            CacheId = CachePrefix + id;
            InProcSessionItem item    = cache [CacheId] as InProcSessionItem;

            if (item == null)
            {
                return(null);
            }

            bool readLocked = false, writeLocked = false;

            try {
                if (item.rwlock.TryEnterUpgradeableReadLock(lockAcquireTimeout))
                {
                    readLocked = true;
                }
                else
                {
                    throw new ApplicationException("Failed to acquire lock");
                }

                if (item.locked)
                {
                    locked  = true;
                    lockAge = DateTime.UtcNow.Subtract(item.lockedTime);
                    lockId  = item.lockId;
                    return(null);
                }

                if (exclusive)
                {
                    if (item.rwlock.TryEnterWriteLock(lockAcquireTimeout))
                    {
                        writeLocked = true;
                    }
                    else
                    {
                        throw new ApplicationException("Failed to acquire lock");
                    }
                    item.locked     = true;
                    item.lockedTime = DateTime.UtcNow;
                    item.lockId++;
                    lockId = item.lockId;
                }
                if (item.items == null)
                {
                    actions    = SessionStateActions.InitializeItem;
                    item.items = new SessionStateItemCollection();
                }
                if (item.staticItems == null)
                {
                    item.staticItems = staticObjects;
                }

                return(new SessionStateStoreData(item.items,
                                                 item.staticItems,
                                                 item.timeout));
            } catch {
                // we want such errors to be passed to the application.
                throw;
            } finally {
                if (writeLocked)
                {
                    item.rwlock.ExitWriteLock();
                }
                if (readLocked)
                {
                    item.rwlock.ExitUpgradeableReadLock();
                }
            }
        }
Beispiel #13
0
		/* In certain situations the 'item' parameter passed to SetAndReleaseItemExclusive
		   may be null. The issue was reported in bug #333898, but the reporter cannot
		   provide a test case that triggers the issue. Added work around the problem
		   in the way that should have the least impact on the rest of the code. If 'item'
		   is null, then the new session item is created without the items and staticItems
		   collections - they will be initialized to defaults when retrieving the session
		   item. This is not a correct fix, but since there is no test case this is the best
		   what can be done right now.
		*/
		public override void SetAndReleaseItemExclusive (HttpContext context,
								 string id,
								 SessionStateStoreData item,
								 object lockId,
								 bool newItem)
		{
			EnsureGoodId (id, true);
			string CacheId = CachePrefix + id;
			Cache cache = HttpRuntime.InternalCache;
			InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
			ISessionStateItemCollection itemItems = null;
			int itemTimeout = 20;
			HttpStaticObjectsCollection itemStaticItems = null;

			if (item != null) {
				itemItems = item.Items;
				itemTimeout = item.Timeout;
				itemStaticItems = item.StaticObjects;
			}
			
			if (newItem || inProcItem == null) {
				inProcItem = new InProcSessionItem ();
				inProcItem.timeout = itemTimeout;
				inProcItem.expiresAt = DateTime.UtcNow.AddMinutes (itemTimeout);
				if (lockId.GetType() == typeof(Int32))
					inProcItem.lockId = (Int32)lockId;
			} else {
				if (lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
					return;
				inProcItem.resettingTimeout = true;
				cache.Remove (CacheId);
			}

			bool locked = false;
			ReaderWriterLockSlim itemLock = null;
			try {
				itemLock = inProcItem.rwlock;
				if (itemLock != null && itemLock.TryEnterWriteLock (lockAcquireTimeout))
					locked = true;
				else
					throw new ApplicationException ("Failed to acquire lock");
				inProcItem.locked = false;
				inProcItem.items = itemItems;
				inProcItem.staticItems = itemStaticItems;
				InsertSessionItem (inProcItem, itemTimeout, CacheId);
			} catch {
				throw;
			} finally {
				if (locked && itemLock != null)
					itemLock.ExitWriteLock ();
			}
		}
Beispiel #14
0
		public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
		{
			EnsureGoodId (id, true);
			InProcSessionItem item = new InProcSessionItem ();
			item.expiresAt = DateTime.UtcNow.AddMinutes (timeout);
			item.timeout = timeout;
			InsertSessionItem (item, timeout, CachePrefix + id);
		}