private SessionStateStoreData GetSessionStoreItem(bool lockRecord,
HttpContext context,
string sessionId,
out bool locked,
out TimeSpan lockAge,
out object lockId,
out SessionStateActions actionFlags)
{
// Initial values for return value and out parameters.
SessionStateStoreData item = null;
lockAge = TimeSpan.Zero;
lockId = Guid.NewGuid().ToString();
locked = false;
actionFlags = SessionStateActions.None;
bool foundRecord = false;
bool deleteData = false;
DateTime newLockedDate = DateTime.Now;
Document session = null;
if (lockRecord)
{
Document lockDoc = new Document();
lockDoc[ATTRIBUTE_SESSION_ID] = GetHashKey(sessionId);
lockDoc[ATTRIBUTE_LOCK_ID] = lockId.ToString();
lockDoc[ATTRIBUTE_LOCKED] = true;
lockDoc[ATTRIBUTE_LOCK_DATE] = DateTime.Now;
try
{
session = this._table.UpdateItem(lockDoc, LOCK_UPDATE_CONFIG);
locked = false;
}
catch (ConditionalCheckFailedException)
{
// This means the record is already locked by another request.
locked = true;
}
}
if (session == null)
{
session = this._table.GetItem(GetHashKey(sessionId), CONSISTENT_READ_GET);
if (session == null && lockRecord)
{
locked = true;
}
}
string serializedItems = null;
if (session != null)
{
DateTime expire = (DateTime)session[ATTRIBUTE_EXPIRES];
if (expire < DateTime.Now)
{
deleteData = true;
locked = false;
}
else
{
foundRecord = true;
DynamoDBEntry entry;
if (session.TryGetValue(ATTRIBUTE_SESSION_ITEMS, out entry))
{
serializedItems = (string)entry;
}
if (session.Contains(ATTRIBUTE_LOCK_ID))
lockId = (string)session[ATTRIBUTE_LOCK_ID];
if (session.Contains(ATTRIBUTE_FLAGS))
actionFlags = (SessionStateActions)((int)session[ATTRIBUTE_FLAGS]);
if (session[ATTRIBUTE_LOCK_DATE] != null)
{
DateTime lockDate = (DateTime)session[ATTRIBUTE_LOCK_DATE];
lockAge = DateTime.Now.Subtract(lockDate);
}
}
}
if (deleteData)
{
this.deleteItem(sessionId);
}
// The record was not found. Ensure that locked is false.
if (!foundRecord)
{
locked = false;
lockId = null;
}
// If the record was found and you obtained a lock, then clear the actionFlags,
// and create the SessionStateStoreItem to return.
if (foundRecord && !locked)
{
if (actionFlags == SessionStateActions.InitializeItem)
{
Document updateDoc = new Document();
updateDoc[ATTRIBUTE_SESSION_ID] = GetHashKey(sessionId);
updateDoc[ATTRIBUTE_FLAGS] = 0;
this._table.UpdateItem(updateDoc);
item = CreateNewStoreData(context, (int)this._timeout.TotalMinutes);
}
else
{
item = deserialize(context, serializedItems, (int)this._timeout.TotalMinutes);
}
}
return item;
}