private bool Remove(string tag) { Timer.Start(); if (OnceDictTag != null && Cache.ContainsKey(OnceDictTag)) { OnceDictTag = null; } var result = Cache.Remove(tag); DetailListener?.Invoke($"Remove [{tag}] {Cache.Count}{Environment.NewLine}"); return(result); }
private static async Task <bool> CollectGoogleAnalyticsAsync(Dictionary <string, string> dict) { try { DetailListener?.Invoke($"CollectGoogleAnalyticsAsync{Environment.NewLine}{dict.ToFormatTableString()}"); string postStr = NetHelper.UrlEncode(dict); var result = await NetLegacy.PostAsync(GAURL, postStr); return(result != null); } catch (Exception e) { Log.Error(e, true); return(false); } }
public async void TryBadCache() { if (BadCache.Count > 0) { bool flag = true; for (int i = BadCache.Count - 1; flag && i >= 0; i--) { var x = BadCache[i]; flag = await _Send(x); if (flag) { BadCache.RemoveAt(i); } } DetailListener?.Invoke($"{'='.Dup(60)}{Environment.NewLine}TryBadCache remain {BadCacheCountFormat()}"); Log.DEBUG($"TryBadCache remain {BadCacheCountFormat()}"); } }
public async Task SendAsync() { Timer.Stop(); var list = Cache.Values.ToList(); Cache.Clear(); foreach (var x in list.Split(MaxCacheCount)) { var result = await _Send(x); if (result) { DetailListener?.Invoke($"{'='.Dup(60)}{Environment.NewLine}Send {x.Count}, remain bad cache {BadCacheCountFormat()}"); } else { BadCache.Add(x); DetailListener?.Invoke($"{'='.Dup(60)}{Environment.NewLine}Not send, Add {x.Count}, bad cache {BadCacheCountFormat()}"); Log.DEBUG($"Analytics Send {x.Count}, remain {BadCacheCountFormat()}"); } } }
public string Add(Dictionary <string, string> data, string tag = "", bool CountDown = true) { if (!CanAddItem) { return(tag); } if (CountDown) { Timer.Start(); } if (tag.IsNullOrEmpty()) { tag = RandomGenerator.RandomString(8); } else { Remove(tag); } data["v"] = "1"; data["tid"] = TrackId; data["cid"] = ClientId; foreach (var item in EachHitDict) { data[item.Key] = item.Value; } if (OnceDictTag == null) { foreach (var item in OnceDict) { data[item.Key] = item.Value; } OnceDictTag = tag; } Cache.Add(tag, data); DetailListener?.Invoke($"Add [{tag}] {Cache.Count}{data.ToFormatTableString()}{Environment.NewLine}"); //if (Cache.Count >= MaxCacheCount) Send(); return(tag); }
private static async Task <bool> BatchCollectGoogleAnalyticsAsync(IEnumerable <Dictionary <string, string> > batchData) { try { DetailListener?.Invoke($"BatchCollectGoogleAnalyticsAsync{Environment.NewLine}{batchData.JoinToString(o => o.ToFormatTableString(), Environment.NewLine)}"); string batchStr = ""; foreach (var dict in batchData) { batchStr += "\n" + NetHelper.UrlEncode(dict); } if (batchStr.Length > 1) { batchStr = batchStr.Substring(1); } var result = await NetLegacy.PostAsync(GABATCHURL, batchStr); return(result != null); } catch (Exception e) { Log.Error(e, true); return(false); } }