/// <summary> /// 加载缓存 /// </summary> /// <param name="entityCfg"></param> public void LoadCache(EntityCfg entityCfg) { string key = string.Empty; try { //从继承的CustomerLoader中获取数据 IList data; ICacheLoader cacheLoader; if (!string.IsNullOrEmpty(entityCfg.CustomLoaderType)) { string[] cfgContent = entityCfg.CustomLoaderType.Split(','); cacheLoader = MB.Util.DllFactory.Instance.LoadObject(cfgContent[0], cfgContent[1]) as ICacheLoader; data = cacheLoader.LoadCache(); } else { throw new MB.Util.APPException("Entity节点中CustomLoaderType不存在"); } if (!data.GetType().IsGenericType) { throw new MB.Util.APPException("加载器返回的IList不是泛型列表");; } key = data.GetType().GetGenericArguments()[0].FullName; if (data != null) { CacheData cacheData = fillCacheData(data, entityCfg); //将数据填入CacheData addCacheDataToContainer(key, cacheData, entityCfg); //将CacheData存入EnterpriseLib的CacheManager } } catch (Exception ex) { MB.Util.TraceEx.Write("缓存加载器{0}出错,错误信息:{1}", key, ex.ToString()); } }
/// <summary> /// 加载CacheData中的项目 /// </summary> private CacheData fillCacheData(IList data, EntityCfg entityCfg) { Type genericCacheDataType = typeof(CacheData <>).MakeGenericType(data.GetType().GetGenericArguments()[0]); CacheData cacheData = DllFactory.Instance.CreateInstance(genericCacheDataType) as CacheData; MB.Util.MyReflection.Instance.InvokeMethodByName(cacheData, "InitCacheData", data, entityCfg); return(cacheData); }
/// <summary> /// 将Cache的数据加到缓存manager中 /// </summary> /// <param name="cacheData"></param> /// <param name="entityCfg"></param> private void addCacheDataToContainer(string key, CacheData cacheData, EntityCfg entityCfg) { if (entityCfg != null) { List <ICacheItemExpiration> cacheItemExpiration = new List <ICacheItemExpiration>(); foreach (var cacheExpireCfg in entityCfg.Expirations) { if (cacheExpireCfg.ExpirationType == CacheExpirationType.AbsoluteTime) { TimeSpan ts = TimeSpan.FromMilliseconds(cacheExpireCfg.ExpireTime); if (ts.TotalMilliseconds > 0) { AbsoluteTime absoluteTime = new AbsoluteTime(ts); cacheItemExpiration.Add(absoluteTime); } } else if (cacheExpireCfg.ExpirationType == CacheExpirationType.SlidingTime) { TimeSpan ts = TimeSpan.FromMilliseconds(cacheExpireCfg.ExpireTime); if (ts.TotalMilliseconds > 0) { SlidingTime slidingTime = new SlidingTime(ts); cacheItemExpiration.Add(slidingTime); } } else if (cacheExpireCfg.ExpirationType == CacheExpirationType.FileDependency) { if (System.IO.File.Exists(cacheExpireCfg.FilePath)) { FileDependency fileDependency = new FileDependency(cacheExpireCfg.FilePath); cacheItemExpiration.Add(fileDependency); } } } if (entityCfg.IsCacheItemRefreshed) { _EntitySetCache.Add(key, cacheData, entityCfg.CacheItemPriority, new CacheItemRefreshAction(entityCfg, key), cacheItemExpiration.ToArray()); } else { _EntitySetCache.Add(key, cacheData, entityCfg.CacheItemPriority, null, cacheItemExpiration.ToArray()); } } else { _EntitySetCache.Add(key, cacheData); } }
/// <summary> /// 从继承的CustomerLoader中获取数据 /// </summary> /// <param name="entityCfg"></param> /// <returns></returns> private IList loadCacheData(EntityCfg entityCfg) { IList data; if (!string.IsNullOrEmpty(entityCfg.CustomLoaderType)) { string[] cfgContent = entityCfg.CustomLoaderType.Split(','); ICacheLoader cacheLoader = MB.Util.DllFactory.Instance.LoadObject(cfgContent[0].Trim(), cfgContent[1].Trim()) as ICacheLoader; data = cacheLoader.LoadCache(); } else { throw new MB.Util.APPException("Entity节点中CustomLoaderType不存在"); } return(data); }
/// <summary> /// 获取缓存加载器 /// </summary> /// <returns></returns> public List <EntityCfg> GetEntitySetCfg() { string cacheConfig = CachePath + CacheFileInfoName; if (!File.Exists(cacheConfig)) { throw new MB.Util.APPException(string.Format("缓存需要的配置文件 {0} 不存在", cacheConfig), Util.APPMessageType.SysErrInfo); } //获取需要载入的文件列表 XElement root = XElement.Load(cacheConfig); var entitySet = from c in root.Elements("EntitySet").Elements() select c; List <EntityCfg> cfgs = new List <EntityCfg>(); foreach (var entity in entitySet) { EntityCfg cfg = new EntityCfg(); string name = entity.Attribute("Name") == null ? string.Empty : entity.Attribute("Name").Value; string ruleType = entity.Attribute("RuleType") == null ? string.Empty : entity.Attribute("RuleType").Value; string customLoaderType = entity.Attribute("CustomLoaderType") == null ? string.Empty : entity.Attribute("CustomLoaderType").Value; string cacheItempriority = entity.Attribute("CacheItemPriority") == null ? "NotRemovable" : entity.Attribute("CacheItemPriority").Value; string isRefreshed = entity.Attribute("IsCacheItemRefreshed") == null ? "false" : entity.Attribute("IsCacheItemRefreshed").Value; cfg.Name = name; cfg.RuleType = ruleType; cfg.CustomLoaderType = customLoaderType; Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority priority; if (Enum.TryParse <Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority>(cacheItempriority, out priority)) { cfg.CacheItemPriority = priority; } else { cfg.CacheItemPriority = Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority.NotRemovable; MB.Util.TraceEx.Write(string.Format("Entity中的名称为({0})的CacheItemPriority的值{1}配置的不正确", name, cacheItempriority)); } bool tempIsRefreshed; if (bool.TryParse(isRefreshed, out tempIsRefreshed)) { cfg.IsCacheItemRefreshed = tempIsRefreshed; } else { cfg.IsCacheItemRefreshed = false; } List <CacheExpirationCfg> expireCfgs = new List <CacheExpirationCfg>(); var expirations = from e in entity.Descendants("Expiration") select e; foreach (var expiration in expirations) { CacheExpirationCfg expCfg = new CacheExpirationCfg(); string expirationType = expiration.Attribute("ExpirationType") == null ? string.Empty : expiration.Attribute("ExpirationType").Value; string filePath = expiration.Attribute("FilePath") == null ? string.Empty : expiration.Attribute("FilePath").Value; string expireTime = expiration.Attribute("ExpireTime") == null ? "0" : expiration.Attribute("ExpireTime").Value; CacheExpirationType expireType; if (Enum.TryParse <CacheExpirationType>(expirationType, out expireType)) { expCfg.ExpirationType = expireType; } else { expCfg.ExpirationType = CacheExpirationType.Never; MB.Util.TraceEx.Write(string.Format("Expiration中的名称为({0})的ExpirationType的值{1}配置的不正确", name, expirationType)); } expCfg.FilePath = filePath; int tempExpireTime; if (int.TryParse(expireTime, out tempExpireTime)) { expCfg.ExpireTime = tempExpireTime; } else { expCfg.ExpireTime = 0; } expireCfgs.Add(expCfg); } cfg.Expirations = expireCfgs; cfgs.Add(cfg); } return(cfgs); }
public CacheItemRefreshAction(EntityCfg entityCfg, string key) { _EntityCfg = entityCfg; _Key = key; }