public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null) { ValidatePolicy(policy); try { DateTime utcExpiry; if (policy.SlidingExpiration != NoSlidingExpiration) { utcExpiry = DateTime.UtcNow + policy.SlidingExpiration; value = new SlidingExpiryCacheItem(value, policy.SlidingExpiration); } else { utcExpiry = policy.AbsoluteExpiration.UtcDateTime; } cache.Set(key, value, utcExpiry, regionName); } catch (Exception e) { LogUtility.LogError("Error in Set: " + e.Message); if (configuration.ThrowOnError) { throw; } } }
public override void ReleaseItemExclusive(HttpContext context, string id, object lockId) { try { if (LastException == null && lockId != null) { LogUtility.LogInfo("ReleaseItemExclusive => Session Id: {0}, Session provider object: {1} => For lockId: {2}.", id, this.GetHashCode(), lockId); GetAccessToStore(id); cache.TryReleaseLockIfLockIdMatch(lockId); // Either already released lock successfully inside above if block // Or we do not hold lock so we should not release it. sessionId = null; sessionLockId = null; } } catch (Exception e) { LogUtility.LogError("ReleaseItemExclusive => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } }
public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null) { ValidatePolicy(policy); try { DateTime utcExpiry; if (policy.SlidingExpiration != NoSlidingExpiration) { utcExpiry = DateTime.UtcNow + policy.SlidingExpiration; value = new SlidingExpiryCacheItem(value, policy.SlidingExpiration); } else { utcExpiry = policy.AbsoluteExpiration.UtcDateTime; } object oldValue = cache.AddOrGetExisting(key, value, utcExpiry, regionName); oldValue = HandleSlidingExpiry(key, oldValue, regionName); return(oldValue); } catch (Exception e) { LogUtility.LogError("Error in AddOrGetExisting: " + e.Message); if (configuration.ThrowOnError) { throw; } } return(null); }
public override async Task CreateUninitializedItemAsync(HttpContextBase context, string id, int timeout, CancellationToken cancellationToken) { try { if (LastException == null) { LogUtility.LogInfo("CreateUninitializedItem => Session Id: {0}, Session provider object: {1}.", id, this.GetHashCode()); ISessionStateItemCollection sessionData = new ChangeTrackingSessionStateItemCollection(redisUtility); sessionData["SessionStateActions"] = SessionStateActions.InitializeItem; GetAccessToStore(id); // Converting timout from min to sec cache.Set(sessionData, (timeout * FROM_MIN_TO_SEC)); } } catch (Exception e) { LogUtility.LogError("CreateUninitializedItem => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } await Task.FromResult(0); }
/// <summary> /// If retry timout is provide than we will retry first time after 20 ms and after that every 1 sec till retry timout is expired or we get value. /// </summary> private T RetryLogic <T>(Func <T> redisOperation) { int timeToSleepBeforeRetryInMiliseconds = 20; DateTime startTime = DateTime.Now; while (true) { try { return(OperationExecutor(redisOperation)); } catch (Exception e) { TimeSpan passedTime = DateTime.Now - startTime; if (_configuration.RetryTimeout < passedTime) { LogUtility.LogError($"Exception: {e.Message}"); throw; } else { int remainingTimeout = (int)(_configuration.RetryTimeout.TotalMilliseconds - passedTime.TotalMilliseconds); // if remaining time is less than 1 sec than wait only for that much time and than give a last try if (remainingTimeout < timeToSleepBeforeRetryInMiliseconds) { timeToSleepBeforeRetryInMiliseconds = remainingTimeout; } } // First time try after 20 msec after that try after 1 second System.Threading.Thread.Sleep(timeToSleepBeforeRetryInMiliseconds); timeToSleepBeforeRetryInMiliseconds = 1000; } } }
public override void EndRequest(HttpContext context) { try { // This check is required for unit tests to work int sessionTimeoutInSeconds; if (context != null && context.Session != null) { sessionTimeoutInSeconds = context.Session.Timeout * FROM_MIN_TO_SEC; } else { sessionTimeoutInSeconds = (int)configuration.SessionTimeout.TotalSeconds; } if (sessionId != null && sessionLockId != null) { GetAccessToStore(sessionId); cache.TryReleaseLockIfLockIdMatch(sessionLockId, sessionTimeoutInSeconds); LogUtility.LogInfo("EndRequest => Session Id: {0}, Session provider object: {1} => Lock Released with lockId {2}.", sessionId, this.GetHashCode(), sessionLockId); sessionId = null; sessionLockId = null; } cache = null; } catch (Exception e) { LogUtility.LogError("EndRequest => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } }
public override async Task SetAndReleaseItemExclusiveAsync(HttpContextBase context, string id, SessionStateStoreData item, object lockId, bool newItem, CancellationToken cancellationToken) { try { if (LastException == null) { GetAccessToStore(id); // If it is new record if (newItem) { ISessionStateItemCollection sessionItems = null; if (item != null && item.Items != null) { sessionItems = item.Items; } else { sessionItems = new ChangeTrackingSessionStateItemCollection(redisUtility); } if (sessionItems["SessionStateActions"] != null) { sessionItems.Remove("SessionStateActions"); } // Converting timout from min to sec cache.Set(sessionItems, (item.Timeout * FROM_MIN_TO_SEC)); LogUtility.LogInfo("SetAndReleaseItemExclusive => Session Id: {0}, Session provider object: {1} => created new item in session.", id, this.GetHashCode()); } // If update if lock matches else { if (item != null && item.Items != null) { if (item.Items["SessionStateActions"] != null) { item.Items.Remove("SessionStateActions"); } // Converting timout from min to sec cache.TryUpdateAndReleaseLock(lockId, item.Items, (item.Timeout * FROM_MIN_TO_SEC)); LogUtility.LogInfo("SetAndReleaseItemExclusive => Session Id: {0}, Session provider object: {1} => updated item in session, Lock ID: {2}.", id, this.GetHashCode(), lockId); } } } } catch (Exception e) { LogUtility.LogError("SetAndReleaseItemExclusive => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } await Task.FromResult(0); }
public override void Remove(string key) { try { GetAccessToCacheStore(); cache.Remove(key); } catch (Exception e) { LogUtility.LogError("Error in Remove: " + e.Message); } }
public override void Set(string key, object entry, DateTime utcExpiry) { try { GetAccessToCacheStore(); cache.Set(key, entry, utcExpiry); } catch (Exception e) { LogUtility.LogError("Error in Set: " + e.Message); } }
public override object Add(string key, object entry, DateTime utcExpiry) { try { GetAccessToCacheStore(); return(cache.Add(key, entry, utcExpiry)); } catch (Exception e) { LogUtility.LogError("Error in Add: " + e.Message); } return(null); }
public override object Get(string key) { try { GetAccessToCacheStore(); return(cache.Get(key)); } catch (Exception e) { LogUtility.LogError("Error in Get: " + e.Message); } return(null); }
public override object Remove(string key, string regionName = null) { try { return(cache.Remove(key, regionName)); } catch (Exception e) { LogUtility.LogError("Error in Remove: " + e.Message); if (configuration.ThrowOnError) { throw; } } return(null); }
public override object Get(string key, string regionName = null) { try { object value = cache.Get(key, regionName); value = HandleSlidingExpiry(key, value, regionName); return(value); } catch (Exception e) { LogUtility.LogError("Error in Get: " + e.Message); if (configuration.ThrowOnError) { throw; } } return(null); }
public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item) { try { if (LastException == null && lockId != null) { LogUtility.LogInfo("RemoveItem => Session Id: {0}, Session provider object: {1}.", id, this.GetHashCode()); GetAccessToStore(id); cache.TryRemoveAndReleaseLockIfLockIdMatch(lockId); } } catch (Exception e) { LogUtility.LogError("RemoveItem => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } }
public override void ResetItemTimeout(HttpContext context, string id) { try { if (LastException == null) { LogUtility.LogInfo("ResetItemTimeout => Session Id: {0}, Session provider object: {1}.", id, this.GetHashCode()); GetAccessToStore(id); cache.UpdateExpiryTime((int)configuration.SessionTimeout.TotalSeconds); cache = null; } } catch (Exception e) { LogUtility.LogError("ResetItemTimeout => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } }
public override async Task RemoveItemAsync(HttpContextBase context, string id, object lockId, SessionStateStoreData item, CancellationToken cancellationToken) { try { if (LastException == null) { LogUtility.LogInfo("RemoveItem => Session Id: {0}, Session provider object: {1}, Lock ID: {2}.", id, this.GetHashCode(), lockId); GetAccessToStore(id); cache.TryRemoveAndReleaseLock(lockId); } } catch (Exception e) { LogUtility.LogError("RemoveItem => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } await Task.FromResult(0); }
public override async Task ReleaseItemExclusiveAsync(HttpContextBase context, string id, object lockId, CancellationToken cancellationToken) { try { // This check is required for unit tests to work int sessionTimeoutInSeconds; if (context != null && context.Session != null) { sessionTimeoutInSeconds = context.Session.Timeout * FROM_MIN_TO_SEC; } else { sessionTimeoutInSeconds = (int)configuration.SessionTimeout.TotalSeconds; } if (LastException == null && lockId != null) { LogUtility.LogInfo("ReleaseItemExclusive => Session Id: {0}, Session provider object: {1} => For lockId: {2}.", id, this.GetHashCode(), lockId); GetAccessToStore(id); cache.TryReleaseLockIfLockIdMatch(lockId, sessionTimeoutInSeconds); // Either already released lock successfully inside above if block // Or we do not hold lock so we should not release it. sessionId = null; sessionLockId = null; } } catch (Exception e) { LogUtility.LogError("ReleaseItemExclusive => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } await Task.FromResult(0); }
public override async Task ResetItemTimeoutAsync(HttpContextBase context, string id, CancellationToken cancellationToken) { try { if (LastException == null) { LogUtility.LogInfo("ResetItemTimeout => Session Id: {0}, Session provider object: {1}.", id, this.GetHashCode()); GetAccessToStore(id); cache.UpdateExpiryTime((int)configuration.SessionTimeout.TotalSeconds); cache = null; } } catch (Exception e) { LogUtility.LogError("ResetItemTimeout => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } await Task.FromResult(0); }
public override void EndRequest(HttpContext context) { try { if (sessionId != null && sessionLockId != null) { GetAccessToStore(sessionId); cache.TryReleaseLockIfLockIdMatch(sessionLockId); LogUtility.LogInfo("EndRequest => Session Id: {0}, Session provider object: {1} => Lock Released with lockId {2}.", sessionId, this.GetHashCode(), sessionLockId); sessionId = null; sessionLockId = null; } cache = null; } catch (Exception e) { LogUtility.LogError("EndRequest => {0}", e.ToString()); LastException = e; if (configuration.ThrowOnError) { throw; } } }
private SessionStateStoreData GetItemFromSessionStore(bool isWriteLockRequired, HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) { try { SessionStateStoreData sessionStateStoreData = null; locked = false; lockAge = TimeSpan.Zero; lockId = 0; actions = SessionStateActions.None; GetAccessToStore(id); ISessionStateItemCollection sessionData = null; int sessionTimeout; bool isLockTaken = false; //Take read or write lock and if locking successful than get data in sessionData and also update session timeout if (isWriteLockRequired) { isLockTaken = cache.TryTakeWriteLockAndGetData(DateTime.Now, (int)configuration.RequestTimeout.TotalSeconds, out lockId, out sessionData, out sessionTimeout); sessionId = id; // signal that we have to remove lock in EndRequest sessionLockId = lockId; // save lockId for EndRequest } else { isLockTaken = cache.TryCheckWriteLockAndGetData(out lockId, out sessionData, out sessionTimeout); } if (isLockTaken) { locked = false; LogUtility.LogInfo("GetItemFromSessionStore => Session Id: {0}, Session provider object: {1} => Lock taken with lockId: {2}", id, this.GetHashCode(), lockId); } else { sessionId = null; sessionLockId = null; locked = true; LogUtility.LogInfo("GetItemFromSessionStore => Session Id: {0}, Session provider object: {1} => Can not lock, Someone else has lock and lockId is {2}", id, this.GetHashCode(), lockId); } // If locking is not successful then do not return any result just return lockAge, locked=true and lockId. // ASP.NET tries to acquire lock again in 0.5 sec by calling this method again. Using lockAge it finds if // lock has been taken more than http request timeout than ASP.NET calls ReleaseItemExclusive and calls this method again to get lock. if (locked) { lockAge = cache.GetLockAge(lockId); return(null); } if (sessionData == null) { // If session data do not exists means it might be exipred and removed. So return null so that asp.net can call CreateUninitializedItem and start again. // But we just locked the record so first release it ReleaseItemExclusive(context, id, lockId); return(null); } // Restore action flag from session data if (sessionData["SessionStateActions"] != null) { actions = (SessionStateActions)sessionData["SessionStateActions"]; } //Get data related to this session from sessionDataDictionary and populate session items sessionData.Dirty = false; sessionStateStoreData = new SessionStateStoreData(sessionData, new HttpStaticObjectsCollection(), sessionTimeout); return(sessionStateStoreData); } catch (Exception e) { LogUtility.LogError("GetItemFromSessionStore => {0}", e.ToString()); locked = false; lockId = null; lockAge = TimeSpan.Zero; actions = 0; LastException = e; if (configuration.ThrowOnError) { throw; } return(null); } }