Esempio n. 1
0
 internal EntityContainer(BaseCachePool cachePool, Func <bool, bool> loadFactory, Func <string, bool, bool> loadItemFactory)
 {
     _cachePool       = cachePool;
     _loadFactory     = loadFactory;
     _loadItemFactory = loadItemFactory;
     _containerKey    = typeof(T).FullName;
     if (_cachePool != null && !_cachePool.TryGetValue(_containerKey, out _container))
     {
         Initialize();
         _cachePool.TryGetValue(_containerKey, out _container);
     }
 }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool ContainEntityKey(Type type, string key)
        {
            CacheContainer container;

            key = AbstractEntity.EncodeKeyCode(key);
            if (_writePools != null && _writePools.TryGetValue(type.FullName, out container))
            {
                CacheItemSet itemSet;
                return(container.Collection.TryGetValue(key, out itemSet) &&
                       itemSet.LoadingStatus == LoadingStatus.Success &&
                       !itemSet.IsEmpty);
            }
            return(false);
        }
Esempio n. 3
0
        internal EntityContainer(BaseCachePool cachePool, Func <bool, bool> loadFactory, Func <string, bool, bool> loadItemFactory)
        {
            _cachePool       = cachePool;
            _loadFactory     = loadFactory;
            _loadItemFactory = loadItemFactory;
            _containerKey    = typeof(T).FullName;
            if (_cachePool != null && !_cachePool.TryGetValue(_containerKey, out _container))
            {
                Initialize();
                _cachePool.TryGetValue(_containerKey, out _container);
            }

            if (_container == null)
            {
                TraceLog.WriteError("加载{0}-EntityContainer失败,未能获取_container", _containerKey);
            }
            else if (_container.Collection == null)
            {
                TraceLog.WriteError("加载{0}-EntityContainer失败,未能获取Collection", _containerKey);
            }
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="itemPair">key:entity's key, value:</param>
        /// <returns></returns>
        public static bool TryGetCacheItem(string redisKey, out KeyValuePair <string, CacheItemSet> itemPair)
        {
            itemPair = default(KeyValuePair <string, CacheItemSet>);
            CacheItemSet cacheItem;

            string[] keys = (redisKey ?? "").Split('_');
            if (keys.Length == 2 && !string.IsNullOrEmpty(keys[0]))
            {
                CacheContainer container;
                string         typeName = RedisConnectionPool.DecodeTypeName(keys[0]);
                var            schema   = EntitySchemaSet.Get(typeName);
                if (_writePools != null && _writePools.TryGetValue(typeName, out container))
                {
                    string[] childKeys   = keys[1].Split('|');
                    string   personalKey = childKeys[0];
                    string   entityKey   = childKeys.Length > 1 ? childKeys[1] : "";
                    if (schema.CacheType == CacheType.Dictionary &&
                        container.Collection.TryGetValue(personalKey, out cacheItem))
                    {
                        itemPair = new KeyValuePair <string, CacheItemSet>(entityKey, cacheItem);
                        return(true);
                    }
                    if (schema.CacheType == CacheType.Entity &&
                        container.Collection.TryGetValue(entityKey, out cacheItem))
                    {
                        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);
        }
Esempio n. 5
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;

            string[] keys = (redisKey ?? "").Split('_');
            if (keys.Length == 2 && !string.IsNullOrEmpty(keys[0]))
            {
                CacheContainer container;
                string         typeName = RedisConnectionPool.DecodeTypeName(keys[0]);
                if (_writePools != null && _writePools.TryGetValue(typeName, out container))
                {
                    string[] childKeys   = keys[1].Split('|');
                    string   personalKey = childKeys[0];
                    string   entityKey   = childKeys.Length > 1 ? childKeys[1] : "";
                    if (!string.IsNullOrEmpty(personalKey) &&
                        (container.Collection.TryGetValue(entityKey, out itemSet) ||
                         container.Collection.TryGetValue(personalKey, out itemSet)))
                    {
                        switch (itemSet.ItemType)
                        {
                        case CacheType.Entity:
                            entity = itemSet.ItemData;
                            break;

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

                        default:
                            TraceLog.WriteError("Not suported CacheType:{0} for GetPersonalEntity key:{1}", itemSet.ItemType, redisKey);
                            break;
                        }
                    }
                }
            }
            if (entity == null)
            {
                //while is remove entity is empty.
                //TraceLog.WriteComplement("GetPersonalEntity key:{0} is empty.", redisKey);
            }
            return(entity);
        }