コード例 #1
0
        /// <summary>
        /// If the key has been deserialized already, return the current object value we have on hand
        ///     for it. If it has not, then deserialize it from initial Redis input and add it to the base
        ///     collection.
        /// </summary>
        /// <param name="key">The desired Session key name</param>
        /// <returns>The deserialized object at the key, or null if it does not exist.</returns>
        protected object MemoizedDeserializeGet(string key)
        {
            object storedObj = null;

            if (this.Items.TryGetValue(key, out storedObj))
            {
                if (storedObj is NotYetDeserializedPlaceholderValue)
                {
                    string serializedData;
                    if (this.SerializedRawData.TryGetValue(key, out serializedData))
                    {
                        try
                        {
                            var placeholderReference = storedObj;
                            storedObj = this.cereal.DeserializeOne(serializedData);

                            // if we can't deserialize, storedObj will still be the placeholder and in that case it's
                            //      as if the DeserializeOne method error'ed, so mark it as failed to deserialize
                            if (storedObj is NotYetDeserializedPlaceholderValue)
                            {
                                storedObj = null;
                            }

                            // Try to update the key to the deserialized object. If it fails, check to make sure that its
                            //      not because it was already deserialized in another thread
                            if (!this.Items.TryUpdate(key, storedObj, placeholderReference))
                            {
                                // the update failed, this could be because the comparison value was different for the
                                //      concurrentDictionary, so lets try fetching the value again
                                if (this.Items.TryGetValue(key, out storedObj))
                                {
                                    // if it is not a placeholder, return the re-fetched object from the Items collection
                                    if (!(storedObj is NotYetDeserializedPlaceholderValue))
                                    {
                                        return(storedObj);
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            if (RedisSerializationConfig.SerializerExceptionLoggingDel != null)
                            {
                                RedisSerializationConfig.SerializerExceptionLoggingDel(e);
                            }

                            storedObj = null;
                        }
                    }
                }
            }

            return(storedObj);
        }