Esempio n. 1
0
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="receiveParam"></param>
        /// <param name="periodTime">缓存的生命周期,单位秒</param>
        /// <param name="isReplace"></param>
        /// <returns></returns>
        protected bool TryLoadCache(string groupKey, TransReceiveParam receiveParam, int periodTime, bool isReplace)
        {
            //todo: trace TryLoadCache
            var watch = RunTimeWatch.StartNew(string.Format("Try load cache data:{0}-{1}", receiveParam.Schema.EntityType.FullName, groupKey));

            try
            {
                CacheItemSet itemSet = InitContainer(groupKey, periodTime);
                List <T>     dataList;
                if (DataContainer.TryReceiveData(receiveParam, out dataList))
                {
                    watch.Check("received count:" + dataList.Count);
                    InitCache(dataList, periodTime, isReplace);
                    itemSet.OnLoadSuccess();
                    watch.Check("Init cache:");
                    return(true);
                }
                itemSet.OnLoadError();
            }
            finally
            {
                watch.Flush(true, 200);
            }
            TraceLog.WriteError("Try load cache data:{0} error.", typeof(T).FullName);
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="receiveParam"></param>
        /// <param name="match"></param>
        /// <param name="isReplace"></param>
        protected void LoadFrom(TransReceiveParam receiveParam, Predicate <T> match, bool isReplace)
        {
            List <T> dataList = DataContainer.LoadFrom <T>(receiveParam);

            if (DataContainer.LoadStatus == LoadingStatus.Success && dataList != null)
            {
                int periodTime = receiveParam.Schema.PeriodTime;
                var tempList   = match == null ? dataList : dataList.FindAll(match);
                var pairs      = tempList.GroupBy(t => t.PersonalId).ToList();
                foreach (var pair in pairs)
                {
                    CacheItemSet itemSet = InitContainer(pair.Key, periodTime);
                    InitCache(pair.ToList(), periodTime, isReplace);
                    itemSet.OnLoadSuccess();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="entityData"></param>
        /// <param name="periodTime"></param>
        /// <param name="expiredHandle"></param>
        /// <returns></returns>
        public bool TryAddQueue(string groupKey, T entityData, int periodTime, Func <string, CacheQueue <T>, bool> expiredHandle)
        {
            var lazy = new Lazy <CacheItemSet>(() =>
            {
                var temp = new CacheItemSet(CacheType.Queue, periodTime, IsReadonly);
                temp.SetItem(new CacheQueue <T>(expiredHandle));
                temp.OnLoadSuccess();
                return(temp);
            });

            var itemSet = Container.GetOrAdd(groupKey, name => lazy.Value);

            if (itemSet != null)
            {
                //队列不存Redis,不触发事件
                ((CacheQueue <T>)itemSet.GetItem()).Enqueue(entityData);
                entityData.IsInCache = true;
                return(true);
            }
            return(false);
        }