Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="key"></param>
        /// <param name="entityData"></param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public bool AddOrUpdateGroup(string groupKey, string key, T entityData, int periodTime)
        {
            bool         result  = false;
            CacheItemSet itemSet = InitGroupContainer(groupKey, periodTime);

            if (itemSet != null && !Equals(entityData, default(T)))
            {
                entityData.IsInCache = true;
                var data = (BaseCollection)itemSet.GetItem();
                T   oldValue;
                if (data.TryGetValue(key, out oldValue))
                {
                    result = data.TryUpdate(key, entityData, oldValue);
                }
                else
                {
                    result = data.TryAdd(key, entityData);
                }
                if (itemSet.LoadingStatus == LoadingStatus.None)
                {
                    itemSet.OnLoadSuccess();
                }
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="key"></param>
        /// <param name="entityData"></param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public bool AddOrUpdateGroup(string groupKey, string key, T entityData, int periodTime)
        {
            bool         result  = false;
            CacheItemSet itemSet = InitGroupContainer(groupKey, periodTime);

            if (itemSet != null && !Equals(entityData, default(T)))
            {
                var data = (BaseCollection)itemSet.GetItem();
                result = data.AddOrUpdate(key, entityData, (k, t) => entityData) == entityData;
                if (result)
                {
                    var temp = entityData as AbstractEntity;
                    CheckEventBind(temp);
                    entityData.IsInCache = true;
                    if (temp != null && (temp.IsNew || temp.HasChanged))
                    {
                        entityData.TriggerNotify();
                    }
                    if (itemSet.LoadingStatus == LoadingStatus.None)
                    {
                        itemSet.OnLoadSuccess();
                    }
                }
            }
            return(result);
        }
        /// <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);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dataList"></param>
        /// <param name="isReplace"></param>
        public void InitData(List <T> dataList, bool isReplace = true)
        {
            var st         = SchemaTable();
            int periodTime = st == null ? 0 : st.PeriodTime;

            foreach (var pair in dataList.GroupBy(t => t.PersonalId))
            {
                CacheItemSet itemSet = InitContainer(pair.Key, periodTime);
                InitCache(pair.ToList(), periodTime, isReplace);
                itemSet.OnLoadSuccess();
            }
        }
Exemple #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="key"></param>
        /// <param name="entityData"></param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public bool TryAddGroup(string groupKey, string key, T entityData, int periodTime)
        {
            CacheItemSet itemSet = InitGroupContainer(groupKey, periodTime);

            if (itemSet != null)
            {
                if (!Equals(entityData, default(T)) && ((BaseCollection)itemSet.GetItem()).TryAdd(key, entityData))
                {
                    itemSet.OnLoadSuccess();
                    return(true);
                }
            }
            return(false);
        }
Exemple #6
0
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="receiveParam"></param>
        /// <param name="periodTime">缓存的生命周期,单位秒</param>
        /// <returns></returns>
        protected bool TryLoadCache(string groupKey, TransReceiveParam receiveParam, int periodTime)
        {
            CacheItemSet itemSet = InitContainer(groupKey, periodTime);
            List <T>     dataList;

            if (DataContainer.TryReceiveData(receiveParam, out dataList))
            {
                InitCache(dataList, periodTime);
                itemSet.OnLoadSuccess();
                return(true);
            }
            itemSet.OnLoadError();
            TraceLog.WriteError("Try load cache data:{0} error.", typeof(T).FullName);
            return(false);
        }
Exemple #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="receiveParam"></param>
        /// <param name="match"></param>
        protected void LoadFrom(TransReceiveParam receiveParam, Predicate <T> match)
        {
            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);
                    itemSet.OnLoadSuccess();
                }
            }
        }
Exemple #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="key"></param>
        /// <param name="entityData"></param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public bool TryAddGroup(string groupKey, string key, T entityData, int periodTime)
        {
            CacheItemSet itemSet = InitGroupContainer(groupKey, periodTime);

            if (itemSet != null)
            {
                if (!Equals(entityData, default(T)) && ((BaseCollection)itemSet.GetItem()).TryAdd(key, entityData))
                {
                    CheckEventBind(entityData as AbstractEntity);
                    entityData.IsInCache = true;
                    entityData.TriggerNotify();
                    itemSet.OnLoadSuccess();
                    return(true);
                }
            }
            return(false);
        }
Exemple #9
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)
            {
                ((CacheQueue <T>)itemSet.GetItem()).Enqueue(entityData);
                return(true);
            }
            return(false);
        }