/// <summary> /// Initializes a new instance of the <see cref="IronClient" /> class. /// </summary> /// <param name="name">Client name</param> /// <param name="version">Version of the client</param> /// <param name="product">Iron.io product</param> /// <param name="host">hostname to use</param> /// <param name="port">port number</param> /// <param name="projectId">Project identifier available from the HUD</param> /// <param name="token">Token available from the HUD</param> /// <param name="protocol">Protocol e.g. http https</param> /// <param name="apiVersion">Version of the API to use (1,2)</param> /// <param name="configFile">Path to a specific JSON configuration file to load</param> public IronClient(string name, string version, string product, string host = null, int port = 0, string projectId = null, string token = null, string protocol = null, int apiVersion = 0, string configFile = null) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Name cannot be null or empty"); } AutoMapper.Mapper.CreateMap <Configuration, Configuration>().ForMember(c => c.Port, opt => opt.Condition(s => s.Port > 0)) .ForMember(c => c.ApiVersion, opt => opt.Condition(s => s.ApiVersion > 0)) .ForMember(c => c.Host, opt => opt.Condition(s => !string.IsNullOrEmpty(s.Host))) .ForMember(c => c.ProjectId, opt => opt.Condition(s => !string.IsNullOrEmpty(s.ProjectId))) .ForMember(c => c.Token, opt => opt.Condition(s => !string.IsNullOrEmpty(s.Token))) .ForMember(c => c.Protocol, opt => opt.Condition(s => !string.IsNullOrEmpty(s.Protocol))); ConfigurationFactory configFactory = new DefaultConfigurationFactory(); if (product == "iron_mq") { configFactory = new MQConfigFactory(configFactory); } if (product == "iron_worker") { configFactory = new WorkerConfigFactory(configFactory); } if (product == "iron_cache") { configFactory = new CacheConfigFactory(configFactory); } string homePath = (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) ? Environment.GetEnvironmentVariable("HOME") : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); configFactory = new JsonConfigFactory(configFactory, System.IO.Path.Combine(homePath, ".iron.json")); configFactory = new EnvConfigFactory(configFactory); configFactory = new JsonConfigFactory(configFactory, "iron.json"); configFactory = new JsonConfigFactory(configFactory, configFile); configFactory = new ArgsConfigFactory(configFactory, name, version, product, host, port, projectId, token, protocol, apiVersion); this.Config = configFactory.GetConfiguartion(); if (string.IsNullOrEmpty(this.Config.ProjectId)) { throw new Exception("No projectId set. projectId is a required field."); } if (string.IsNullOrEmpty(this.Config.Token)) { throw new Exception("No token set. token is a required field."); } this.BuildHeaders(); }
/// <summary> /// 异步清理业务缓存 /// </summary> /// <param name="cacheKeys"></param> /// <param name="callBackHandler"></param> /// <returns></returns> public Task ClearCacheAsync(IEnumerable <string> cacheKeys, Action callBackHandler = null) { if (cacheKeys.IsEmpty()) { return(Task.FromResult <object>(null)); } Task clearTask = null; string[] orgCacheKeys = cacheKeys.ToArray(); int totalKeys = orgCacheKeys.Length; if (totalKeys <= 1) { //-------单个key模式---- clearTask = Task.Factory.StartNew(() => { CacheConfigFactory.GetCacheManager().Remove(orgCacheKeys[0]); }); } else { //-------批量模式------- string[] inner_cacheKeysCopy = new string[orgCacheKeys.Length]; orgCacheKeys.CopyTo(inner_cacheKeysCopy, 0); clearTask = Task.Factory.StartNew((state) => { string[] keys = (string[])state; keys.AsParallel().ForAll(key => { CacheConfigFactory.GetCacheManager().Remove(key); }); if (null != callBackHandler) { callBackHandler(); } }, inner_cacheKeysCopy); } return(clearTask); }
/// <summary> /// 查询这个关键词在3大平台上的关键词集合, /// 统计 按照统计进行排序并输出 /// </summary> /// <param name="keyWord"></param> /// <returns></returns> public Task <IEnumerable <string> > QueryThisKeywordMappingsAsync(string keyWord) { return(Task.Factory.StartNew(() => { //创建一个 基于阻塞集合 用以保证线程安全 //var allWords = new BlockingCollection<string>(); var lstWords_Tmall = new List <string>(); var lstWords_Taobao = new List <string>(); //------------cache begin----------------------- string cacheKey = string.Concat("AutoMapping:", keyWord); var cacheManager = CacheConfigFactory.GetCacheManager(); if (cacheManager.IsHasSet(cacheKey)) { var cacheWords = cacheManager.Get <IEnumerable <string> >(cacheKey); if (cacheWords != null) { return cacheWords; //var cursor= cacheWords.GetEnumerator();//直接返回缓存对象数组 //while (cursor.MoveNext()) //{ // yield return cursor.Current; //} } } //----------如果未能从缓存获取,那么从网络流中检索------------------- string userAgent = WorkContext.HttpContext.Request.Headers[HttpServerProxy.RequestHeaderKeyUserAgent]; //开辟3个异步任务 并行获取关键词 注意:即使某个平台挂了 不能查询关键词 那么不妨碍其他平台 var tsk_tmall = Task.Factory.StartNew(() => { try { var result_words = new TmallAutoMapping(userAgent).QueryWords(keyWord); lstWords_Tmall.AddRange(result_words); } catch (Exception ex) { Logger.Error(new Exception(string.Concat("tmall 获取关键词列表失败:失败原因:", ex.ToString()))); } }); var tsk_taobao = Task.Factory.StartNew(() => { try { var result_words = new TaobaoAutoMapping(userAgent).QueryWords(keyWord); lstWords_Taobao.AddRange(result_words); } catch (Exception ex) { Logger.Error(new Exception(string.Concat("taobao 获取关键词列表失败:失败原因:", ex.ToString()))); } }); //放弃京东的建议词 //var tsk_jingdong = Task.Factory.StartNew(() => //{ // try // { // var result_words = new JingdongAutoMaping(userAgent).QueryWords(keyWord); // allWords.AddRange(result_words); // } // catch (Exception ex) // { // Logger.Error(new Exception(string.Concat("jingdong 获取关键词列表失败:失败原因:", ex.ToString()))); // } //}); //等待并行任务完毕 Task.WaitAll(tsk_tmall, tsk_taobao);//, tsk_jingdong); //分组 排序 并取前十 //var gps = allWords.GroupBy(x => x).OrderByDescending(x => x.Count()).Take(10); // var resultList = gps.Select(x => x.Key); //-------------------注册到缓存中-------------------- //天猫 淘宝 64开 var resultList = lstWords_Tmall.Take(6).ToList(); for (int i = 0; i < lstWords_Taobao.Count; i++) { var word = lstWords_Taobao[i]; if (resultList.Contains(word)) { continue; } resultList.Add(word); if (resultList.Count >= 10) { break; } } cacheManager.Set(cacheKey, resultList, 60 * 5); return resultList; })); }