示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="entity"></param>
        /// <param name="itemSet"></param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public static bool AddOrUpdateEntity(string redisKey, AbstractEntity entity, out CacheItemSet itemSet, int periodTime = 0)
        {
            itemSet = null;
            KeyValuePair <string, CacheItemSet> itemPair;

            if (TryGetCacheItem(redisKey, out itemPair, periodTime))
            {
                itemSet = itemPair.Value;
                switch (itemPair.Value.ItemType)
                {
                case CacheType.Entity:
                    itemPair.Value.SetItem(entity);
                    entity.IsInCache = true;
                    return(true);

                case CacheType.Dictionary:
                    var set = itemPair.Value.ItemData as BaseCollection;
                    if (set != null)
                    {
                        if (set.AddOrUpdate(itemPair.Key, entity, (k, t) => entity) == entity)
                        {
                            entity.IsInCache = true;
                            return(true);
                        }
                        return(false);
                    }
                    break;

                default:
                    TraceLog.WriteError("Not suported CacheType:{0} for GetPersonalEntity key:{1}", itemPair.Value.ItemType, redisKey);
                    break;
                }
            }
            return(false);
        }
示例#2
0
        /// <summary>
        /// 通过Redis键从缓存中获取实体对象
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="itemSet"></param>
        /// <returns></returns>
        public static dynamic GetPersonalEntity(string redisKey, out CacheItemSet itemSet)
        {
            itemSet = null;
            dynamic entity = null;
            KeyValuePair <string, CacheItemSet> itemPair;

            if (TryGetCacheItem(redisKey, out itemPair))
            {
                itemSet = itemPair.Value;
                switch (itemPair.Value.ItemType)
                {
                case CacheType.Entity:
                    entity = itemPair.Value.ItemData;
                    break;

                case CacheType.Dictionary:
                    var set = itemPair.Value.ItemData as BaseCollection;
                    if (set != null)
                    {
                        set.TryGetValue(itemPair.Key, out entity);
                    }
                    break;

                default:
                    TraceLog.WriteError("Not suported CacheType:{0} for GetPersonalEntity key:{1}", itemPair.Value.ItemType, redisKey);
                    break;
                }
            }
            if (entity == null)
            {
                //while is remove entity is empty.
                //TraceLog.WriteComplement("GetPersonalEntity key:{0} is empty.", redisKey);
            }
            return(entity);
        }
示例#3
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);
        }
示例#4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="key"></param>
 /// <param name="itemSet"></param>
 /// <returns></returns>
 public bool TryGetCacheItem(string key, out CacheItemSet itemSet)
 {
     if (Container.TryGetValue(key, out itemSet))
     {
         return(true);
     }
     return(false);
 }
示例#5
0
        private CacheItemSet CreateItemSet(CacheType cacheType, int periodTime)
        {
            CacheItemSet itemSet = new CacheItemSet(cacheType, periodTime, IsReadonly);

            if (!IsReadonly && _cachePool.Setting != null)
            {
                itemSet.OnChangedNotify += _cachePool.Setting.OnChangedNotify;
            }
            return(itemSet);
        }
示例#6
0
 internal bool TryGetOrAddRank(string key, out CacheItemSet itemSet, int periodTime = 0)
 {
     if (!Container.TryGetValue(key, out itemSet))
     {
         itemSet = new CacheItemSet(CacheType.Rank, periodTime, IsReadonly);
         itemSet.SetItem(new CacheList <T>());
         if (!Container.TryAdd(key, itemSet))
         {
             return(false);
         }
     }
     return(true);
 }
示例#7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="entityKey"></param>
 /// <param name="entityData"></param>
 /// <param name="periodTime"></param>
 /// <param name="itemSet"></param>
 /// <param name="isLoad"></param>
 /// <returns></returns>
 public bool TryAddEntity(string entityKey, T entityData, int periodTime, out CacheItemSet itemSet, bool isLoad = false)
 {
     itemSet = CreateItemSet(CacheType.Entity, periodTime);
     itemSet.SetItem(entityData);
     if (Container.TryAdd(entityKey, itemSet))
     {
         CheckEventBind(entityData as AbstractEntity);
         if (!isLoad)
         {
             entityData.TriggerNotify();
         }
         entityData.IsInCache = true;
         return(true);
     }
     return(false);
 }
示例#8
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();
                }
            }
        }
示例#9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="key"></param>
        /// <param name="entityData"></param>
        /// <param name="periodTime"></param>
        /// <param name="isLoad"></param>
        /// <returns></returns>
        public bool TryAddGroup(string groupKey, string key, T entityData, int periodTime, bool isLoad = false)
        {
            CacheItemSet itemSet = InitGroupContainer(groupKey, periodTime);

            if (itemSet != null)
            {
                if (!Equals(entityData, default(T)) && ((BaseCollection)itemSet.GetItem()).TryAdd(key, entityData))
                {
                    CheckEventBind(entityData as AbstractEntity);
                    if (!isLoad)
                    {
                        entityData.TriggerNotify();
                    }
                    entityData.IsInCache = true;
                    return(true);
                }
            }
            return(false);
        }
示例#10
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);
        }
示例#11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="key"></param>
        /// <param name="entityData"></param>
        /// <param name="periodTime"></param>
        /// <param name="isLoad"></param>
        /// <returns></returns>
        public bool AddOrUpdateGroup(string groupKey, string key, T entityData, int periodTime, bool isLoad = false)
        {
            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);
                    if (!isLoad)
                    {
                        entityData.TriggerNotify();
                    }
                    entityData.IsInCache = true;
                }
            }
            return(result);
        }
示例#12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="itemPair">key:entity's key, value:</param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public static bool TryGetCacheItem(string redisKey, out KeyValuePair <string, CacheItemSet> itemPair, int periodTime = 0)
        {
            itemPair = default(KeyValuePair <string, CacheItemSet>);
            CacheItemSet cacheItem;

            string[] keys = (redisKey ?? "").Split('_');
            if (keys.Length == 2 && !string.IsNullOrEmpty(keys[0]))
            {
                CacheContainer container = null;
                string         typeName  = RedisConnectionPool.DecodeTypeName(keys[0]);
                var            schema    = EntitySchemaSet.Get(typeName);
                periodTime = periodTime > 0 ? periodTime : schema.PeriodTime;
                if (_writePools != null && !_writePools.TryGetValue(typeName, out container))
                {
                    _writePools.InitContainer(typeName);
                    _writePools.TryGetValue(typeName, out container);
                }
                if (container == null)
                {
                    return(false);
                }

                string[] childKeys   = keys[1].Split('|');
                string   personalKey = childKeys[0];
                string   entityKey   = childKeys.Length > 1 ? childKeys[1] : "";
                if (schema.CacheType == CacheType.Dictionary)
                {
                    var lazy = new Lazy <CacheItemSet>(() =>
                    {
                        bool isReadonly = schema.AccessLevel == AccessLevel.ReadOnly;
                        BaseCollection itemCollection = isReadonly
                            ? (BaseCollection) new ReadonlyCacheCollection()
                            : new CacheCollection(schema.IsMutilKey ? 0 : 1);
                        var itemSet = new CacheItemSet(schema.CacheType, periodTime, isReadonly);
                        if (!isReadonly && _writePools.Setting != null)
                        {
                            itemSet.OnChangedNotify += _writePools.Setting.OnChangedNotify;
                        }
                        itemSet.HasCollection = true;
                        itemSet.SetItem(itemCollection);
                        return(itemSet);
                    });
                    cacheItem = container.Collection.GetOrAdd(personalKey, key => lazy.Value);
                    itemPair  = new KeyValuePair <string, CacheItemSet>(entityKey, cacheItem);
                    return(true);
                }
                if (schema.CacheType == CacheType.Entity)
                {
                    var lazy = new Lazy <CacheItemSet>(() =>
                    {
                        bool isReadonly = schema.AccessLevel == AccessLevel.ReadOnly;
                        var itemSet     = new CacheItemSet(schema.CacheType, periodTime, isReadonly);
                        if (!isReadonly && _writePools.Setting != null)
                        {
                            itemSet.OnChangedNotify += _writePools.Setting.OnChangedNotify;
                        }
                        return(itemSet);
                    });
                    cacheItem = container.Collection.GetOrAdd(entityKey, key => lazy.Value);

                    itemPair = new KeyValuePair <string, CacheItemSet>(entityKey, cacheItem);
                    return(true);
                }
                if (schema.CacheType == CacheType.Queue)
                {
                    TraceLog.WriteError("Not support CacheType.Queue get cache, key:{0}.", redisKey);
                }

                ////存在分类id与实体主键相同情况, 要优先判断实体主键
                //if (!string.IsNullOrEmpty(personalKey) && container.Collection.TryGetValue(entityKey, out cacheItem))
                //{
                //    itemPair = new KeyValuePair<string, CacheItemSet>(entityKey, cacheItem);
                //    return true;
                //}
                //if (!string.IsNullOrEmpty(personalKey) && container.Collection.TryGetValue(personalKey, out cacheItem))
                //{
                //    itemPair = new KeyValuePair<string, CacheItemSet>(entityKey, cacheItem);
                //    return true;
                //}
            }
            return(false);
        }
示例#13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="itemSet"></param>
 /// <param name="periodTime"></param>
 /// <returns></returns>
 public static bool AddOrUpdateEntity(AbstractEntity entity, out CacheItemSet itemSet, int periodTime = 0)
 {
     return(AddOrUpdateEntity(GenerateEntityKey(entity), entity, out itemSet, periodTime));
 }