/// <summary> /// /// </summary> /// <param name="isLoad"></param> public MemoryCacheStruct(bool isLoad = true) { containerKey = typeof(T).FullName; _container = CacheFactory.MemoryCache.InitContainer(containerKey); if (isLoad) { Initialize(false); } }
/// <summary> /// 获取Container中存在的Cache,不重新创建 /// </summary> /// <param name="redisKey"></param> /// <param name="itemPair"></param> /// <param name="periodTime"></param> /// <returns></returns> public static bool TryGetContainerCacheItem(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)//|| schema.CacheType == CacheType.Entity) { var result = container.Collection.TryGetValue(personalKey, out cacheItem); itemPair = new KeyValuePair <string, CacheItemSet>(entityKey, cacheItem); return(result); } if (schema.CacheType == CacheType.Entity) { var result = container.Collection.TryGetValue(entityKey, out cacheItem); itemPair = new KeyValuePair <string, CacheItemSet>(entityKey, cacheItem); return(result); } 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); }
/// <summary> /// /// </summary> /// <param name="disposing"></param> protected override void Dispose(bool disposing) { if (disposing) { _containerKey = null; _cachePool = null; _container = null; } base.Dispose(disposing); }
/// <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); }
/// <summary> /// /// </summary> public MemoryCacheStruct() { containerKey = typeof(T).FullName; _container = _momoryPools.InitContainer(containerKey); Initialize(); }
/// <summary> /// /// </summary> public MemoryCacheStruct() { containerKey = typeof(T).FullName; _container = CacheFactory.MemoryCache.InitContainer(containerKey); Initialize(); }