/// <summary>获取 或 增加 配置项</summary> /// <param name="name"></param> /// <returns></returns> public CacheSetting GetOrAdd(String name) { var ms = Items ?? new CacheSetting[0]; var item = ms.FirstOrDefault(e => e?.Name == name); if (item != null) { return(item); } // 如果找不到,则增加 item = new CacheSetting { Name = name }; //Items.Add(item); lock (ms) { var list = new List <CacheSetting>(ms) { item }; Items = list.ToArray(); } return(item); }
/// <summary>初始化配置</summary> /// <param name="set"></param> protected override void Init(CacheSetting set) { if (clearTimer == null) { var period = 60; clearTimer = new TimerX(RemoveNotAlive, null, period * 1000, period * 1000); } }
/// <summary>获取 或 增加 配置项</summary> /// <param name="name"></param> /// <returns></returns> public CacheSetting GetOrAdd(String name) { var item = Items.FirstOrDefault(e => e.Name == name); if (item != null) { return(item); } // 如果找不到,则增加 item = new CacheSetting { Name = name }; Items.Add(item); return(item); }
/// <summary>初始化</summary> /// <param name="set"></param> protected override void Init(CacheSetting set) { var config = set?.Value; if (config.IsNullOrEmpty()) { return; } var dic = config.SplitAsDictionary("=", ";"); if (dic.Count > 0) { Server = dic["Server"]; Password = dic["Password"]; Db = dic["Db"].ToInt(); } }
/// <summary>创建缓存实例</summary> /// <param name="set">配置项</param> /// <returns></returns> public static ICache Create(CacheSetting set) { return(_cache.GetOrAdd(set.Name, k => { var id = set.Provider; var type = ObjectContainer.Current.ResolveType <ICache>(id); if (type == null) { throw new ArgumentNullException(nameof(type), "找不到名为[{0}]的缓存实现[{1}]".F(set.Name, id)); } var ic = type.CreateInstance() as ICache; if (ic is Cache ic2) { ic2.Init(set); } return ic; })); }
/// <summary>获取 或 增加 配置项</summary> /// <param name="name">名称</param> /// <param name="provider">提供者</param> /// <param name="value">配置</param> /// <returns></returns> public CacheSetting GetOrAdd(String name, String provider = null, String value = null) { var ms = Items ?? new CacheSetting[0]; var item = ms.FirstOrDefault(e => e?.Name == name); if (item != null) { return(item); } // 如果找不到,则增加 item = new CacheSetting { Name = name }; //Items.Add(item); lock (ms) { var list = new List <CacheSetting>(ms) { item }; Items = list.ToArray(); } // 添加提供者并保存 if (!provider.IsNullOrEmpty()) { item.Provider = provider; item.Value = value; Save(); } return(item); }
/// <summary>初始化配置</summary> /// <param name="set"></param> protected virtual void Init(CacheSetting set) { }