コード例 #1
0
ファイル: CacheContainer.cs プロジェクト: zhukaixy/Scut
 /// <summary>
 /// 释放
 /// </summary>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         //释放 托管资源
         if (_collection != null)
         {
             _collection.Dispose();
         }
         _collection = null;
     }
     base.Dispose(disposing);
 }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool TryTakeGroup(string key, out T[] list)
        {
            list = default(T[]);
            CacheItemSet itemSet;

            if (Container.TryGetValue(key, out itemSet) && itemSet.HasLoadSuccess)
            {
                BaseCollection enityGroup = (BaseCollection)itemSet.GetItem();
                list = enityGroup.Values.Cast <T>().ToArray();
                return(true);
            }
            return(false);
        }
コード例 #3
0
ファイル: EntityContainer.cs プロジェクト: JoeChen999/scut
 /// <summary>
 /// 遍历
 /// </summary>
 /// <param name="func">第一个参数为分组Key,第二个为实体Key</param>
 public void ForeachGroup(Func <string, string, T, bool> func)
 {
     if (func == null)
     {
         throw new ArgumentNullException("func");
     }
     CheckLoad();
     Container.Foreach <CacheItemSet>((key, itemSet) =>
     {
         BaseCollection enityGroup = (BaseCollection)itemSet.GetItem();
         enityGroup.Foreach(func, key);
         return(true);
     });
 }
コード例 #4
0
ファイル: EntityContainer.cs プロジェクト: zhukaixy/Scut
        /// <summary>
        /// 初始化容器
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public CacheItemSet InitGroupContainer(string groupKey, int periodTime)
        {
            var lazy = new Lazy <CacheItemSet>(() =>
            {
                BaseCollection itemData = IsReadonly
                    ? (BaseCollection) new ReadonlyCacheCollection()
                    : new CacheCollection();
                var itemSet = CreateItemSet(CacheType.Dictionary, periodTime);
                itemSet.SetItem(itemData);
                return(itemSet);
            });

            return(Container.GetOrAdd(groupKey, name => lazy.Value));
        }
コード例 #5
0
        /// <summary>
        /// 初始化容器
        /// </summary>
        /// <param name="groupKey"></param>
        /// <param name="periodTime"></param>
        /// <returns></returns>
        public CacheItemSet InitGroupContainer(string groupKey, int periodTime)
        {
            bool isMutilKey = EntitySchemaSet.Get <T>().IsMutilKey;
            var  lazy       = new Lazy <CacheItemSet>(() =>
            {
                BaseCollection itemCollection = IsReadonly
                    ? (BaseCollection) new ReadonlyCacheCollection()
                    : new CacheCollection(isMutilKey ? 0 : 1);
                var itemSet           = CreateItemSet(CacheType.Dictionary, periodTime);
                itemSet.HasCollection = true;
                itemSet.SetItem(itemCollection);
                return(itemSet);
            });

            return(Container.GetOrAdd(groupKey, name => lazy.Value));
        }
コード例 #6
0
        public T TakeEntityFromKey(string key)
        {
            T      value     = default(T);
            string entityKey = key;

            Container.Foreach <CacheItemSet>((k, itemSet) =>
            {
                BaseCollection enityGroup = (BaseCollection)itemSet.GetItem();
                if (enityGroup.ContainsKey(entityKey))
                {
                    value = enityGroup[entityKey] as T;
                    return(false);
                }
                return(true);
            });
            return(value);
        }
コード例 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="match"></param>
        /// <returns></returns>
        public bool IsExistGroup(Predicate <T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException("match");
            }
            CheckLoad();
            bool result = false;

            Container.Foreach <CacheItemSet>((key, itemSet) =>
            {
                BaseCollection enityGroup = (BaseCollection)itemSet.GetItem();
                if (enityGroup.IsExist(match))
                {
                    result = true;
                    return(false);
                }
                return(true);
            });
            return(result);
        }
コード例 #8
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);
        }
コード例 #9
0
ファイル: BaseCacheStruct.cs プロジェクト: illden/Scut
 /// <summary>
 /// 尝试获取分组
 /// </summary>
 /// <param name="groupKey"></param>
 /// <param name="collection"></param>
 /// <param name="loadStatus"></param>
 /// <returns></returns>
 protected bool TryGetGroup(string groupKey, out BaseCollection collection, out LoadingStatus loadStatus)
 {
     return(DataContainer.TryGetGroup(groupKey, out collection, out loadStatus));
 }
コード例 #10
0
 /// <summary>
 /// 分组集合模型
 /// </summary>
 /// <param name="groupKey"></param>
 /// <param name="enityGroup"></param>
 /// <param name="loadStatus"></param>
 /// <returns></returns>
 public bool TryGetGroup(string groupKey, out BaseCollection enityGroup, out LoadingStatus loadStatus)
 {
     return(TryGetCacheItem(groupKey, true, out enityGroup, out loadStatus));
 }