| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1228 人关注过本帖
标题:本人开发的缓存类
只看楼主 加入收藏
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
结帖率:0
收藏
 问题点数:0 回复次数:20 
本人开发的缓存类
  /// <summary>
    /// 获取数据的委托
    /// </summary>
    /// <param name="forbase"></param>
    /// <returns></returns>
    public delegate object DataDelegate(object forbase);
    /// <summary>
    /// 把数据写入xml的委托
    /// </summary>
    /// <param name="forbase">ID或者MD5码</param>
    /// <param name="ClassName">类,new()或者this</param>
    /// <param name="path">连接(例如 :c:\\4.xml)</param>
    /// <returns></returns>
    public delegate void WriteToXML(string forbase,string path,object ClassName);
搜索更多相关主题的帖子: 开发 public returns void 
2012-10-17 12:25
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.Caching
{


    /// <summary>
    /// 企业库缓存
    /// </summary>
    public class Cache : IDisposable, ICache
    {
        private Hashtable inMemoryCache;
        DataDelegate _Motheds;
        /// <summary>
        /// 必填项目 方法名字,表示如果数据不存在,使用该方法填充数据(Motheds(DataForBase))
        /// </summary>
        public DataDelegate Motheds
        {
            get { return _Motheds; }
            set { _Motheds = value; }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="initialItems"></param>
        public Cache(Hashtable initialItems )
        {
            inMemoryCache = Hashtable.Synchronized(initialItems);
            //IManager = Manager;
        }
        /// <summary>
        /// 获取有多少个count
        /// </summary>
        public int Count
        {
            get { return inMemoryCache.Count; }
        }

        public Hashtable CurrentCacheState
        {
            get { return (Hashtable)inMemoryCache.Clone(); }
        }

        public void Remove(object key)
        {
            bool lockWasSuccessful;
            CacheItem cacheItemBeforeLock = null;
            do{
                lock (inMemoryCache.SyncRoot)
                {
                    cacheItemBeforeLock = (CacheItem)inMemoryCache[key];
                    lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock);
                }
            } while (lockWasSuccessful == false);

            //IManager.CacheRemove(key);
            inMemoryCache.Remove(key);
           
            Monitor.Exit(cacheItemBeforeLock);

        }

        public void Add(object key, object value, CacheItemPriority scavengingPriority, DateTime dt)
        {
            bool lockWasSuccessful;
            CacheItem cacheItemBeforeLock = null;
            do
            {
                lock (inMemoryCache.SyncRoot)
                {
                    cacheItemBeforeLock = new CacheItem( false, null, CacheItemPriority.Normal, dt);
                    inMemoryCache.Add(key, cacheItemBeforeLock);
                    lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock);
                }
            } while (lockWasSuccessful == false);
            // IManager.CacheAdd(key, value, dt, CacheItemPriority.Normal);
            cacheItemBeforeLock.IsCacheOk = true;
            cacheItemBeforeLock.Data = value;
            inMemoryCache[key] = cacheItemBeforeLock;

            Monitor.Exit(cacheItemBeforeLock);

        }
        /// <summary>
        /// 根据Key获取相关的数据,如果没有数据,则通过values去获取数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public CacheItem GetItem(object key, object values)
        {
            CacheItem _cacheItem = null;

            if (inMemoryCache.ContainsKey(key))
            {
                _cacheItem = (CacheItem)inMemoryCache[key];
                if (_cacheItem.IsCacheOk)
                {
                    //从缓存中获取数据
                    //_cacheItem.Data = IManager.CacheItemDate(key);
                    if (_cacheItem.Data == null)
                    {
                        Remove(key);
                        _cacheItem = null;

                    }
                }
                else
                {
                    //三秒钟内获取数据
                    for (int i = 0; i < 150; i++)
                    {
                        Thread.Sleep(20);
                        _cacheItem = (CacheItem)inMemoryCache[key];
                        if (_cacheItem.Data != null)
                        {
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
            else
            {
                //如果没有数据
                //object values = methodes(forbase);
                ClearExdateCache();
                Add(key, values, CacheItemPriority.Low, DateTime.Now.AddSeconds(30));
                _cacheItem = (CacheItem)inMemoryCache[key];
            }
            return _cacheItem;
        }
        /// <summary>
        /// 清除已经过期的缓存
        /// </summary>
        public void ClearExdateCache()
        {
            lock (inMemoryCache.SyncRoot)
            {
                //Hashtable ht = inMemoryCache.Clone()
                foreach (object key in CurrentCacheState.Keys)
                {
                    CacheItem item = (CacheItem)inMemoryCache[key];
                    if (item.LastAccessedTime <= DateTime.Now)
                    {
                        inMemoryCache.Remove(key);
                        //IManager.CacheRemove(key.ToString());
                    }
                }
            }
        }
        /// <summary>
        /// 删除最快过期的一个缓存
        /// </summary>
        public void RemoveCacheLastDateOut()
        {
            lock (inMemoryCache.SyncRoot)
            {
                CacheItem ditem=null;
                object key_ = null;
                foreach (object key in CurrentCacheState.Keys)
                {
                    CacheItem item = (CacheItem)inMemoryCache[key];
                    if (ditem == null)
                    {
                        ditem = item;
                        key_ = key;
                        continue;
                    }
                    else
                    {
                        if (ditem.LastAccessedTime > item.LastAccessedTime)
                        {
                            key_ = key;
                            ditem = item;
                        }
                    }
                }
                inMemoryCache.Remove(key_);
                //IManager.CacheRemove(ditem.Key);
            }
        }

        /// <summary>
        /// 删除缓存等级最低,最快过期的一个缓存
        /// </summary>

        public void RemoveCacheByCacheItemPriority()
        {
            lock (inMemoryCache.SyncRoot)
            {
                CacheItem ditem = null;
                object key_ = null;
                foreach (object key in CurrentCacheState.Keys)
                {
                    CacheItem item = (CacheItem)inMemoryCache[key];
                    if (ditem == null)
                    {
                        ditem = item;
                        key_ = key;
                        continue;
                    }
                    else
                    {
                        if (ditem.ScavengingPriority > item.ScavengingPriority)
                        {
                            ditem = item;
                            key_ = key;
                        }
                        else
                        {
                            if (ditem.ScavengingPriority == item.ScavengingPriority && ditem.LastAccessedTime > item.LastAccessedTime)
                            {
                                ditem = item;
                                key_ = key;
                            }
                        }
                    }
                }
                inMemoryCache.Remove(key_);
            }
        }


        ~Cache()
        {
            Dispose(false);
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                inMemoryCache = null;
                //backingStore.Dispose();
                //backingStore = null;
            }
        }


        public object DataDoing( DataDelegate Mothed,object forbase)
        {
             return Mothed(forbase);
        }



    }
}

59ita点com(我就爱TA)
2012-10-17 12:26
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.Caching
{
    public class CacheItem
    {
        private object _Data;
        /// <summary>
        /// 用于保存的数据
        /// </summary>
        public object Data
        {
            get { return _Data; }
            set { _Data = value; }
        }
        private CacheItemPriority _scavengingPriority;
        /// <summary>
        /// 缓存的权限(高,低等等)
        /// </summary>
        public CacheItemPriority ScavengingPriority
        {
            get { return _scavengingPriority; }
            set { _scavengingPriority = value; }
        }
        private DateTime _lastAccessedTime;
        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime LastAccessedTime
        {
            get { return _lastAccessedTime; }
            set { _lastAccessedTime = value; }
        }
        bool _isCacheOk;
        /// <summary>
        /// 缓存是准备好
        /// </summary>
        public bool IsCacheOk
        {
            get { return _isCacheOk; }
            set { _isCacheOk = value; }
        }

        public CacheItem(bool _IsCacheOk, object _Data, CacheItemPriority _ScavengingPriority, DateTime _LastAccessedTime)
        {
            this._isCacheOk = _IsCacheOk;
            this._Data = _Data;
            this._lastAccessedTime = _LastAccessedTime;
            this._scavengingPriority = _ScavengingPriority;
        }

    }
}

59ita点com(我就爱TA)
2012-10-17 12:27
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.Caching
{
    /// <summary>
    /// 缓存的类型
    /// </summary>
    public enum CacheItemPriority
    {
        /// <summary>
        /// Should never be seen in nature.
        /// </summary>
        None = 0,
        /// <summary>
        /// Low priority for scavenging.
        /// </summary>
        Low = 1,
        /// <summary>
        /// Normal priority for scavenging.
        /// </summary>
        Normal,
        /// <summary>
        /// High priority for scavenging.
        /// </summary>
        High,
        /// <summary>
        /// Non-removable priority for scavenging.
        /// </summary>
        NotRemovable
    }
}

59ita点com(我就爱TA)
2012-10-17 12:27
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.Caching
{
    /// <summary>
    /// 缓存移除的方式
    /// </summary>
    public enum CacheItemRemovedReason
    {
        /// <summary>
        /// 已经过期
        /// </summary>
        Expired,

        /// <summary>
        /// 移除
        /// </summary>
        Removed,

        /// <summary>
        /// 因为缓存数量已满,则根据缓存等级移除较低级的缓存
        /// </summary>
        Scavenged,

        /// <summary>
        /// 未知的
        /// </summary>
        Unknown = 9999
    }
}

59ita点com(我就爱TA)
2012-10-17 12:27
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.Caching
{
    /// <summary>
    /// 数据缓存的接口
    /// </summary>
    public interface ICache
    {
        Hashtable CurrentCacheState { get; }
        /// <summary>
        /// 获取有多少条数据
        /// </summary>
        int Count { get; }
        /// <summary>
        /// 删除指定值的缓存
        /// </summary>
        /// <param name="key"></param>
        void Remove(object key);
        /// <summary>
        /// 添加一条缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="scavengingPriority">缓存等级</param>
        /// <param name="dt">过期时间</param>
        void Add(object key, object value, CacheItemPriority scavengingPriority, DateTime dt);
        /// <summary>
        /// 获取指定键的缓存,如果值不存在,则用values填充(默认缓存30秒)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        CacheItem GetItem(object key, object values);
        /// <summary>
        /// 删除已经过期的缓存
        /// </summary>
        void ClearExdateCache();
        /// <summary>
        /// 删除等级最低,最快过期的一条缓存
        /// </summary>
        void RemoveCacheByCacheItemPriority();
        object DataDoing(DataDelegate Mothed, object forbase);
    }
}

59ita点com(我就爱TA)
2012-10-17 12:28
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.Caching
{
    /// <summary>
    /// 缓存管理
    /// </summary>
    public interface ICacheManager
    {
        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="data"></param>
        /// <param name="_datetime">过期时间</param>
        /// <param name="scavengingPriority"></param>
        void CacheAdd(string key, object data, DateTime _datetime, CacheItemPriority scavengingPriority);
        void CacheRemove(string key);
        object CacheItemDate(string key);
    }
}

59ita点com(我就爱TA)
2012-10-17 12:28
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.DataHelper
{
    public class DataForBase
    {
       // IList<LinkDataType> _Para;
        CommandType _CommandType;
        /// <summary>
        /// 指定如何解析字符串
        /// </summary>
        public CommandType CommandType
        {
            get { return _CommandType; }
            set { _CommandType = value; }
        }
        string _SQLStoredName;
        /// <summary>
        /// 存储过程、表或者语句的名称
        /// </summary>
        public string SQLStoredName
        {
            get { return _SQLStoredName; }
            set { _SQLStoredName = value; }
        }

        private IList<LinkDataType> _LinkDataTypeList;

        /// <summary>
        /// 对应存贮过程的数据类型集合
        /// </summary>
        public IList<LinkDataType> LinkDataTypeList
        {
            get { return _LinkDataTypeList; }
            set { _LinkDataTypeList = value; }
        }
    }
}

59ita点com(我就爱TA)
2012-10-17 12:29
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.DataHelper
{
    /// <summary>
    /// 数据库处理方式
    /// </summary>
    public class DataHelper<T> : CacheDataHelper, IDataHelper, ITInterface<T> where T:new()
    {
        static int _count = string.IsNullOrEmpty(ConfigurationManager.AppSettings["CacheCount"]) ? 50 : int.Parse(ConfigurationManager.AppSettings["CacheCount"]);
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="NamespaceClassnameAndPara"></param>
        /// <param name="ht"></param>
        /// <param name="forbase"></param>
        /// <returns>IList类型</returns>
        public object GetItem(IHelperPara NamespaceClassnameAndPara, Hashtable ht, object forbase)
        {
            ICache cache = new ITA.Base.EntLib.Caching.Cache(ht);
            cache.ClearExdateCache();
            CacheItem item = cache.GetItem(NamespaceClassnameAndPara, cache.DataDoing(new DataDelegate(GetDataByDataBase<T>), forbase));
            return item.Data;
        }

        IList<TClass> GetDataByDataBase<TClass>(object forbase) where TClass : new()
        {

            DataForBase dfb = (DataForBase)forbase;
            List<TClass> li = new List<TClass>();
            using (IData data = DataCom.GetdateType())
            {
                IDataReader dr = data.ExecuteIReader(, dfb.SQLStoredName, data.SetDbParameter(dfb.LinkDataTypeList));
                while (dr.Read())
                {
                    var ClassName = new TClass();
                    DataHelp.ReaderToObject(dr, ClassName);
                    li.Add(ClassName);
                }

                dr.Close();
                dr.Dispose();
            }
            return li;
        }

        #region IDataHelper 成员


        public object GetItem(IHelperPara NamespaceClassnameAndPara, Hashtable ht)
        {
            ICache cache = new ITA.Base.EntLib.Caching.Cache(ht);
            cache.ClearExdateCache();
            CacheItem item = cache.GetItem(NamespaceClassnameAndPara, cache.DataDoing(new DataDelegate(GetDataByDataBase<T>), NamespaceClassnameAndPara.Para));
            return item.Data;

        }

        #endregion
    }
}

59ita点com(我就爱TA)
2012-10-17 12:29
belin2000
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:975
专家分:194
注 册:2004-6-2
收藏
得分:0 
程序代码:
namespace ITA.Base.EntLib.DataHelper
{
    /// <summary>
    /// 用于数据缓存的类,代表key
    /// </summary>
    public class DataHelperPara : HelperPara,IHelperPara
    {
        DataForBase _Para;
        /// <summary>
        /// 数据对象
        /// </summary>
        public object Para
        {
            get { return _Para; }
            set { _Para = (DataForBase)value; }
        }
    }
}

59ita点com(我就爱TA)
2012-10-17 12:30
快速回复:本人开发的缓存类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.080842 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved