/// <summary> /// Insert information in cache, along with the expirations and callback. /// </summary> /// <param name="id">Session ID</param> /// <param name="table">Value needed to be stored</param> /// private void PutInNCache(string id, Hashtable table, object lockID, bool enableRetry) { byte[] buffer = null; using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, table); buffer = stream.ToArray(); stream.Close(); } CacheItem sessionItem = new CacheItem(buffer); sessionItem.Priority = CacheItemPriority.NotRemovable; sessionItem.SlidingExpiration = new TimeSpan(0, (int)table[TIMEOUT_KEY], 0); lock (s_dataLock) { if (s_cacheNeedInit) { InitializeCache(); } } if (_lockSessions) { _cache.Insert(GetUniqueSessionId(id), sessionItem, lockID as LockHandle, true, enableRetry); } else { _cache.Insert(GetUniqueSessionId(id), sessionItem, enableRetry); } _cache.CurrentSessionCache = null; }
/// <summary> /// Fills NCache from the system ASP.NET session. /// </summary> /// <param name="cache"></param> /// <param name="session"></param> /// <param name="strict"></param> /// <param name="async"></param> void IDistributionStrategy.FillCacheFromSession(ISessionCache cache, HttpSessionState session, NSessionStateModule module, bool strict, bool isAbandoned) { string sessionId = session.SessionID; SessionKey key = new SessionKey(sessionId, module.ApplicationId); try { if (session.IsReadOnly && cache.Contains(sessionId, key.ToString()) && !isAbandoned) { return; } if (/*session.Count == 0 ||*/ isAbandoned)//[Ata]: Session is not removed from store if it is cleared { cache.Remove(sessionId, key.ToString(), false); if (module.DetailedLogsEnabled) { NSessionStateModule.NCacheLog.Debug(sessionId + " :session removed from cache"); } return; } //use-case: A session my get emptied while doing different updates... although whien added first time is is not empty //So we must update that session rather doing no operation thinking it is empty session and need not to be added. if (session.Count == 0 && _isNewSession) //We need not to keep any new empty session in the cache. [Asif Imam] April 09, 08 { return; } IDictionary ht = new Hashtable(); foreach (string skey in session.Keys) { ht[skey] = session[skey]; } byte[] _stable = CompactBinaryFormatter.ToByteBuffer(ht, module.CacheID); if (_table != null) { if (BinaryComparer(_stable, _table)) { return; } } CacheItem sessionItem = new CacheItem(_stable); sessionItem.Priority = CacheItemPriority.NotRemovable; sessionItem.Expiration = new Runtime.Caching.Expiration(Runtime.Caching.ExpirationType.Sliding, TimeSpan.FromMinutes(session.Timeout)); cache.Insert(sessionId, key.ToString(), sessionItem, false); } finally { if (session != null && strict) { session.Clear(); } } }
/// <summary> /// Fills NCache from the system ASP.NET session. /// </summary> /// <param name="cache"></param> /// <param name="session"></param> /// <param name="strict"></param> /// <param name="async"></param> void IDistributionStrategy.FillCacheFromSession(ISessionCache cache, HttpSessionState session, NSessionStateModule module, bool strict, bool isAbandoned) { string sessionId = session.SessionID; SessionKey key = new SessionKey(sessionId, module.ApplicationId); try { if (session.IsReadOnly && cache.Contains(sessionId, key.ToString()) && !isAbandoned) { return; } if (isAbandoned)// Session is not removed from store if it is cleared { cache.Remove(sessionId, key.ToString(), false); if (module.DetailedLogsEnabled) { NSessionStateModule.NCacheLog.Debug(sessionId + " :session removed from cache"); } return; } if (session.Count == 0 && _isNewSession) //We need not to keep any new empty session in the cache. { return; } IDictionary ht = new Hashtable(); foreach (string skey in session.Keys) { ht[skey] = session[skey]; } byte[] _stable = CompactBinaryFormatter.ToByteBuffer(ht, module.CacheID); if (_table != null) { if (BinaryComparer(_stable, _table)) { return; } } CacheItem sessionItem = new CacheItem(_stable); sessionItem.Priority = CacheItemPriority.NotRemovable; sessionItem.SlidingExpiration = TimeSpan.FromMinutes(session.Timeout); cache.Insert(sessionId, key.ToString(), sessionItem, false); } finally { if (session != null && strict) { session.Clear(); } } }
/// <summary> /// Fills NCache from the system ASP.NET session. /// </summary> /// <param name="cache"></param> /// <param name="session"></param> /// <param name="strict"></param> /// <param name="async"></param> void IDistributionStrategy.FillCacheFromSession(ISessionCache cache, HttpSessionState session, NSessionStateModule module, bool strict, bool isAbandoned) { if (cache == null) { return; } string sessionId = session.SessionID; SessionKey key = new SessionKey(sessionId, module.ApplicationId); try { if (session.Count == 0) //We need not to keep any empty session in the cache. [Asif Imam] April 09, 08 (This is incomplete. As it is never used. See Monolithic strategy for detail use-case) { return; } cache.Remove(sessionId, key.ToString(), false); cache.Insert(sessionId, key.ToString(), DateTime.Now, Alachisoft.NCache.Runtime.Caching.ExpirationConstants.AbsoluteNoneExpiration, TimeSpan.FromMinutes(session.Timeout), CacheItemPriority.NotRemovable); string[] tempStrArr = new string[1]; tempStrArr[0] = key.ToString(); foreach (string skey in session.Keys) { cache.Insert(sessionId, SessionKey.CompositeKey(sessionId, skey), session[skey], Alachisoft.NCache.Runtime.Caching.ExpirationConstants.AbsoluteNoneExpiration, Alachisoft.NCache.Runtime.Caching.ExpirationConstants.SlidingNoneExpiration, CacheItemPriority.NotRemovable); } if (strict) { session.Clear(); } } catch (Exception exc) { module.RaiseExceptions(exc, "SingleValueDistribution.FillCacheFromSession"); //if (exceptionsEnabled) throw; } }
/// <summary> /// Insert information in cache, along with the expirations and callback. /// </summary> /// <param name="id">Session ID</param> /// <param name="table">Value needed to be stored</param> /// private void PutInNCache(IAspEnvironmentContext context, string id, Hashtable table, object lockID, bool enableRetry) { byte[] buffer = null; using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, table); buffer = stream.ToArray(); stream.Close(); } CacheItem sessionItem = new CacheItem(buffer); sessionItem.Priority = CacheItemPriority.NotRemovable; sessionItem.Expiration = new Runtime.Caching.Expiration(Runtime.Caching.ExpirationType.Sliding, new TimeSpan(0, (int)table[TIMEOUT_KEY], 0)); lock (s_dataLock) { if (s_cacheNeedInit) { InitializeCache(); } } string locationID = GetLocationID(context, id); if (_cache == null) { LogError(new OperationFailedException("The specified cache is not running. "), id); return; } if (_lockSessions) { _cache.Insert(locationID, GetUniqueSessionId(id), sessionItem, lockID as LockHandle, true, enableRetry); } else { _cache.Insert(locationID, GetUniqueSessionId(id), sessionItem, enableRetry); } _cache.CurrentSessionCache = null; }