/// <summary> /// 构造函数:默认缓存项,不设置淘汰算法 /// </summary> internal CacheItem() { myCacheItem = new CacheItemInfoCollection <TParam, TKey, TValue> { Owner = this }; }
internal CacheItem(Func <TParam, TValue> method, Dictionary <TKey, TValue> CacheDatas) { GetCacheDataMethod = method; myCacheItem = CacheItemInfoCollection <TParam, TKey, TValue> .CreateDatas(CacheDatas); myCacheItem.Owner = this; }
/// <summary> /// 构造函数:指定同步更新间隔时间及同步时获取数据源的方法委托(单项同步) /// </summary> /// <param name="GapSpan">缓存间隔同步时间</param> /// <param name="method">同步获取数据的委托,只获取缓存中某一项</param> /// <param name="CacheDatas">初始要缓存的值</param> internal CacheItem(TimeSpan GapSpan, Func <TParam, TValue> method, Dictionary <TKey, TValue> CacheDatas) { myCacheItem = CacheItemInfoCollection <TParam, TKey, TValue> .CreateDatas(CacheDatas); myCacheItem.Owner = this; myCacheItem.GapSpanTime = GapSpan; GetCacheDataMethod = method; }
public void Reload(Dictionary <TKey, TValue> datas) { CacheItemInfoCollection <TParam, TKey, TValue> tmp = CacheItemInfoCollection <TParam, TKey, TValue> .CreateDatas(datas); tmp.Owner = this; lock (myCacheItem) { myCacheItem.Clear(); myCacheItem = tmp; } }
public void Dispose() { if (syncUtil != null) { syncUtil.Stop(); } if (myCacheItem != null) { Clear(); } myCacheItem = null; }
/// <summary> /// 根据指定初始需缓存的数据集合创建缓存值集 /// </summary> /// <param name="datas"></param> /// <returns></returns> public static CacheItemInfoCollection <TParam, TKey, TValue> CreateDatas(Dictionary <TKey, TValue> datas) { CacheItemInfoCollection <TParam, TKey, TValue> items = new CacheItemInfoCollection <TParam, TKey, TValue>(); if (datas.IsNotNull()) { foreach (TKey key in datas.Keys) { items[key] = new CacheItemInfo <TParam, TKey, TValue>(datas[key], DateTime.Now, items); } } return(items); }
/// <summary> /// 同步缓存中的数据,注意,这里并不是实际同步,而是自动标记缓存项的过期时间 /// </summary> private void SyncData() { if (GetAllMethod != null) { Dictionary <TKey, TValue> o = GetAllMethod(); if (o != null) {//未实际获取到数据 CacheItemInfoCollection <TParam, TKey, TValue> datas = CacheItemInfoCollection <TParam, TKey, TValue> .CreateDatas(o); datas.Owner = this; lock (myCacheItem) { myCacheItem = datas; } } } }
/// <summary> /// 构造函数:指定同步间隔时间及所有数据同步的委托,只适合那种更新较快的项 /// </summary> /// <param name="GapSpan"></param> /// <param name="method"></param> internal CacheItem(TimeSpan GapSpan, Func <Dictionary <TKey, TValue> > method) { GetAllMethod = method; if (method != null) { myCacheItem = CacheItemInfoCollection <TParam, TKey, TValue> .CreateDatas(method()); } else { myCacheItem = new CacheItemInfoCollection <TParam, TKey, TValue>(); } myCacheItem.Owner = this; syncUtil = ThreadSvrUtil.CreateSvrUtil(GapSpan, SyncData); //自动启动同步服务 syncUtil.Start(); }
/// <summary> /// 构造函数:指定同步间隔时间及所有数据同步的委托,只适合那种更新较快的项 /// 通过定时表达式创建定时激活器 /// 定时器设置格式如下: /// 1、定时触发:0|h|1,表示按每小时激活,0|m|1 表示按每分钟激活,0|s|1 表示按每秒钟激活,0|ms|1 表示按每毫秒激活 /// 2、时点触发:1|17:30;17:12;02:36 /// </summary> /// <param name="GapSpanExp"></param> /// <returns></returns> internal CacheItem(string GapSpanExp, Func <Dictionary <TKey, TValue> > method, Dictionary <TKey, TValue> items) { GetAllMethod = method; if (items != null) { myCacheItem = CacheItemInfoCollection <TParam, TKey, TValue> .CreateDatas(items); } else { myCacheItem = new CacheItemInfoCollection <TParam, TKey, TValue>(); } myCacheItem.Owner = this; ThreadTimer timer = ThreadTimer.CreateThreadTimerByExp(GapSpanExp); timer.Method = SyncData; syncUtil = new ThreadSvrUtil(timer); //自动启动同步服务 syncUtil.Start(); }
public CacheItemInfo(TValue value, DateTime time, CacheItemInfoCollection <TParam, TKey, TValue> _owner) { CacheItemValue = value; UpdateTime = time; Owner = _owner; }