示例#1
0
        /// <summary>
        /// Insert value into the cache using
        /// appropriate name/value pairs
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="o">Item to be cached</param>
        /// <param name="key">Name of item</param>
        public static void Add <T>(string key, T o)
        {
            var ss = new SerializableSession
            {
                SessionObject = o
            };

            HttpContext.Current.Session.Add(key, ss);
        }
示例#2
0
        public static T SetOrGet <T>(string key, T value)
        {
            if (!Exists(key))
            {
                Add(key, value);
            }
            else
            {
                var ss = new SerializableSession();
                ss = (SerializableSession)HttpContext.Current.Session[key];

                value = (T)ss.SessionObject;
            }

            return(value);
        }
示例#3
0
        /// <summary>
        /// Retrieve cached item
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="key">Name of cached item</param>
        /// <param name="value">Cached value. Default(T) if item doesn't exist.</param>
        /// <returns>Cached item as type</returns>
        public static bool Get <T>(string key, out T value)
        {
            try
            {
                if (!Exists(key))
                {
                    value = default(T);
                    return(false);
                }

                var ss = new SerializableSession();
                ss = (SerializableSession)HttpContext.Current.Session[key];

                value = (T)ss.SessionObject;
            }
            catch (Exception)
            {
                value = default(T);
                return(false);
            }

            return(true);
        }