Ejemplo n.º 1
0
        /// <summary>
        /// Extract session data from hastable fetched from cache
        /// </summary>
        /// <param name="context"></param>
        /// <param name="items"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        private object GetContents(IAspEnvironmentContext context, Hashtable items, ref SessionInitializationActions flag)
        {
            //int timeout = (int)items[TIMEOUT_KEY];
            flag = (SessionInitializationActions)items[ACTIONS_KEY];
            byte[] buffer  = items[SESSION_DATA] as byte[];
            int    timeout = (int)items[TIMEOUT_KEY];

            return(DeserializeSession(buffer, timeout));

            //SessionStateItemCollection itemsCollection = null;
            //HttpStaticObjectsCollection staticCollection = null;
            //if (items.Contains(ITEMS_KEY))
            //{
            //    itemsCollection = (SessionStateItemCollection)items[ITEMS_KEY];
            //}
            //else
            //{
            //    itemsCollection = new SessionStateItemCollection();
            //}
            //if (items.Contains(STATIC_ITEMS_KEY))
            //{
            //    staticCollection = (HttpStaticObjectsCollection)items[STATIC_ITEMS_KEY];
            //}
            //else
            //{
            //    staticCollection = SessionStateUtility.GetSessionStaticObjects(context);
            //}
            //return new SessionStateStoreData(itemsCollection, staticCollection, timeout);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Extract session data from hastable fetched from cache
        /// </summary>
        /// <param name="context"></param>
        /// <param name="items"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        private object GetContents(IAspEnvironmentContext context, Hashtable items, ref SessionInitializationActions flag)
        {
            flag = (SessionInitializationActions)items[ACTIONS_KEY];
            byte[] buffer  = items[SESSION_DATA] as byte[];
            int    timeout = (int)items[TIMEOUT_KEY];

            return(DeserializeSession(buffer, timeout));
        }
Ejemplo n.º 3
0
        public object GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge,
                                       out object lockId, out SessionInitializationActions action)
        {
            var newContext = WrapContext(context);

            try
            {
                return(_store.GetItemExclusive(newContext, id, out locked, out lockAge, out lockId, out action));
            }
            finally
            {
                newContext.FinalizeContext();
            }
        }
Ejemplo n.º 4
0
 private SessionStateActions ToSessionStateActions(SessionInitializationActions actions)
 {
     return((SessionStateActions)((int)actions));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Form a hashtable to be added to cache
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        /// <param name="flag"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        private Hashtable InsertContents(IAspEnvironmentContext context, object data, SessionInitializationActions flag, int timeout)
        {
            Hashtable items = new Hashtable(4);

            if (data != null)
            {
                byte[] buffer = SerializeSession(data);
                items.Add(SESSION_DATA, buffer);
                items.Add(TIMEOUT_KEY, timeout);
            }
            else
            {
                items.Add(TIMEOUT_KEY, (int)timeout);
            }
            //if (data == null)
            //{
            //    items.Add(ITEMS_KEY, new SessionStateItemCollection());
            //    if (context != null)
            //    {
            //        items.Add(STATIC_ITEMS_KEY, SessionStateUtility.GetSessionStaticObjects(context));
            //    }
            //    items.Add(TIMEOUT_KEY, timeout);
            //}
            //else
            //{
            //    if (data.Items != null && data.Items.Count != 0)
            //    {
            //        items.Add(ITEMS_KEY, data.Items);
            //    }
            //    if (data.StaticObjects != null && !data.StaticObjects.NeverAccessed)
            //    {
            //        items.Add(STATIC_ITEMS_KEY, data.StaticObjects);
            //    }
            //    items.Add(TIMEOUT_KEY, data.Timeout);
            //}


            items.Add(ACTIONS_KEY, flag);

            return(items);
        }
Ejemplo n.º 6
0
        private object getSessionStoreItem(bool acquireLock, IAspEnvironmentContext context, string id, out bool locked, out TimeSpan lockAge, out object lockID, out SessionInitializationActions action)
        {
            //lock age is set to zero, so SessionStateModule will,
            //after 0.5 sec, calls this GetItemExclusive function until a value or
            //null reference is returned. If no data was found, 'locked' is set to false and
            //null reference is returned which tells SessionStateModule to call CreateDataStore
            //function to create new data store

            locked  = false;
            lockAge = TimeSpan.Zero;
            lockID  = null;
            action  = SessionInitializationActions.InitializeItem;
            DateTime lockDate = DateTime.UtcNow;

            LockHandle lockHandle   = new LockHandle(null, lockDate);
            object     items        = null;
            Hashtable  table        = null;
            bool       lockTimedOut = false;
            string     locationID   = GetLocationID(context, id);


            if (_cache == null)
            {
                LogError(new OperationFailedException("The specified cache is not running. "), id);
                return(null);
            }

            try
            {
                _cache.IsSessionCookieless = IsSessionCookieless(context);
                byte[] buffer = null;
                lock (s_dataLock)
                {
                    if (s_cacheNeedInit)
                    {
                        InitializeCache();
                    }
                }
                if (_lockSessions)
                {
                    try
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), ref lockHandle, acquireLock, true);
                    }
                    catch (Exception)
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), ref lockHandle, acquireLock, false);
                    }


                    lockID   = lockHandle.LockId == null ? null : lockHandle;
                    lockDate = lockHandle == null ? DateTime.UtcNow : lockHandle.LockDate;
                }
                else
                {
                    try
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), true);
                    }
                    catch (Exception)
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), false);
                    }
                }

                if (buffer != null)
                {
                    using (MemoryStream stream = new MemoryStream(buffer))
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        table = formatter.Deserialize(stream) as Hashtable;
                        stream.Close();
                    }
                }

                if (_lockSessions && !String.IsNullOrEmpty(lockHandle.LockId))
                {
                    DateTime now = DateTime.UtcNow;
                    if ((0L < lockDate.Ticks) && (lockDate.Ticks < now.Ticks))
                    {
                        lockAge = (TimeSpan)(now - lockDate);
                    }
                    else
                    {
                        lockAge = TimeSpan.Zero;
                    }

                    /// Following 'if' block is executed if item is locked.
                    /// i.e NCache API returns null and out variables are populated with lockId and lockDate
                    /// Note: Item exists in cache but is locked.
                    if (table == null)
                    {
                        if (this._sessionLockingRetries >= 0)
                        {
                            if (!context.ContainsItem(SESSION_LOCK_COUNT))
                            {
                                context[SESSION_LOCK_COUNT] = 0;
                            }

                            int retriesCompleted = (int)context[SESSION_LOCK_COUNT];

                            if (retriesCompleted < this._sessionLockingRetries)
                            {
                                context[SESSION_LOCK_COUNT] = retriesCompleted + 1;
                            }
                            else ///this will construct and send a dummy session to application
                            {
                                if (this._emptySessionWhenLocked)
                                {
                                    locked       = false;///a dummy session is going to be returned
                                    lockID       = null;
                                    lockTimedOut = true;

                                    return(CreateEmptySession(context, _defaultTimeout));
                                }
                                else
                                {
                                    throw new ProviderException("Cannot acquire session lock. Session is already locked.");
                                }
                            }
                        }

                        locked = true;
                        return(null);
                    }
                }

                /// If item exists in cahce and lock acqusition was successfull or locking was disabled.
                if (table != null)
                {
                    items = GetContents(context, table, ref action);
                    if (action == SessionInitializationActions.InitializeItem)
                    {
                        int timeout = (int)table[TIMEOUT_KEY];
                        items = CreateNewStoreData(context, timeout);
                    }
                }
                else
                {
                    if (NCacheLog != null && NCacheLog.IsInfoEnabled)
                    {
                        NCacheLog.Warn("SessionStoreBase.GetSessionItem", "Session not found in cache. " + id);
                    }
                }
            }
            catch (Exception exc)
            {
                /// If item is not found in Remote cache.
                /// Update cookies to do any further operations for this session request on current primary cache;
                /// and do not raise exception.

                LogError(exc, id);
            }
            finally
            {
                if ((lockTimedOut || table != null) && context.ContainsItem(SESSION_LOCK_COUNT))
                {
                    context.RemoveItem(SESSION_LOCK_COUNT);
                }
                //    throw new ProviderException("Cannot acquire session lock. Session is already locked.");
            }
            return(items);
        }
Ejemplo n.º 7
0
 public virtual object GetItemExclusive(IAspEnvironmentContext context, string id, out bool locked, out TimeSpan lockAge, out object lockID, out SessionInitializationActions action)
 {
     return(getSessionStoreItem(_lockSessions, context, id, out locked, out lockAge, out lockID, out action));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Form a hashtable to be added to cache
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        /// <param name="flag"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        private Hashtable InsertContents(IAspEnvironmentContext context, object data, SessionInitializationActions flag, int timeout)
        {
            Hashtable items = new Hashtable(4);

            if (data != null)
            {
                byte[] buffer = SerializeSession(data);
                items.Add(SESSION_DATA, buffer);
                items.Add(TIMEOUT_KEY, timeout);
            }
            else
            {
                items.Add(TIMEOUT_KEY, (int)timeout);
            }

            items.Add(ACTIONS_KEY, flag);

            return(items);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Form a hashtable to be added to cache
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        /// <param name="flag"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        private Hashtable InsertContents(HttpContext context, SessionStateStoreData data, SessionInitializationActions flag, int timeout)
        {
            Hashtable items = new Hashtable(4);

            if (data != null)
            {
                byte[] buffer = SessionSerializationUtil.Serialize(data);
                items.Add(SESSION_DATA, buffer);
                items.Add(TIMEOUT_KEY, (int)data.Timeout);
            }
            else
            {
                items.Add(TIMEOUT_KEY, (int)timeout);
            }

            items.Add(ACTIONS_KEY, flag);

            return(items);
        }