cache

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace WindowsFormsApplication1
{
        public class Caching
        {
            /// <summary>
            /// 緩存對象
            /// </summary>
            public static readonly Caching Cache = new Caching();
            private System.Collections.Hashtable _cache;

            private Caching()
            {
                _cache = new Hashtable();
            }
            /// <summary>
            /// 獲取/設置緩存對象
            /// </summary>
            /// <param name="key">鍵值</param>
            /// <returns></returns>
            public object this[string key]
            {
                get
                {
                    //獲取緩存對象
                    return (_cache[key]);
                }
                set
                {
                    if (_cache.Contains(key))
                    {
                        //緩存中已存在該鍵,先刪除該鍵,而後設置新的鍵及對象
                        Remove(key);
                    }
                    //添加緩存對象
                    _cache.Add(key, value);
                }
            }
            /// <summary>
            /// 清除緩存中指定鍵對象
            /// </summary>
            /// <param name="key">鍵值</param>
            public void Remove(string key)
            {
                _cache.Remove(key);
            }
            /// <summary>
            /// 清除緩存中全部對象
            /// </summary>
            public void RemoveAll()
            {
                _cache.Clear();
            }
    }
}
相關文章
相關標籤/搜索