public async Task <IEnumerable <ChannelInfo> > GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken) { ChannelCache cache = null; var key = tuner.Id; if (enableCache && !string.IsNullOrWhiteSpace(key) && _channelCache.TryGetValue(key, out cache)) { if ((DateTime.UtcNow - cache.Date) < TimeSpan.FromMinutes(60)) { return(cache.Channels.ToList()); } } var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false); var list = result.ToList(); Logger.Debug("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list)); if (!string.IsNullOrWhiteSpace(key) && list.Count > 0) { cache = cache ?? new ChannelCache(); cache.Date = DateTime.UtcNow; cache.Channels = list; _channelCache.AddOrUpdate(key, cache, (k, v) => cache); } return(list); }
public async Task <List <ChannelInfo> > GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken) { ChannelCache cache = null; var key = tuner.Id; if (enableCache && !string.IsNullOrEmpty(key) && _channelCache.TryGetValue(key, out cache)) { return(cache.Channels.ToList()); } var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false); var list = result.ToList(); //logger.LogInformation("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list)); if (!string.IsNullOrEmpty(key) && list.Count > 0) { cache = cache ?? new ChannelCache(); cache.Channels = list; _channelCache.AddOrUpdate(key, cache, (k, v) => cache); } return(list); }
private void SaveChannelCache(ulong channelId, ChannelCache newCache) { using StreamWriter file = File.CreateText(GetChannelCachePath(channelId)); // In-case there's an exception attempting to write the file out, we only want to flush (using will do this for us) when we're fully done serialising. file.AutoFlush = false; Serializer.Serialize(file, newCache); }
public RabbitBackplane( ILogger logger, IRabbitMqConnectionManager rabbitMqConnectionManager, IBackplaneMessageHandler backplaneMessageHandler, IBackplaneMetrics backplaneMetrics) { exchangeName = $"{ServiceInfo.Name}{ServiceInfo.MajorVersion}"; _backplaneMessageHandler = backplaneMessageHandler; _channelCache = new ChannelCache(rabbitMqConnectionManager, logger, exchangeName, ExchangeType.Fanout, false, backplaneMetrics.RecordNewBackplaneChannel); }
public Task UpdateContentCacheForChannel(ChannelCache cache) { // If downloading attachments has been disabled, we'll just skip all of this. if (Program.Configuration["download-attachments"] != "true") { return(Task.CompletedTask); } // We'll compare the attachments which should be cached according to the channel cache and download any which are missing. var semaphore = new SemaphoreSlim(MaxConcurrentDownloads, MaxConcurrentDownloads); var totalDownloads = 0; var downloadsCompleted = 0; foreach (var att in cache.AttachmentCache) { if (ContentCacheDir.GetFiles($"{att.Key}.*").Length > 0) { continue; } totalDownloads++; Task.Run(async() => { await semaphore.WaitAsync(); try { // Download the file to the cache directory using FileStream file = File.Create(Path.Combine(ContentCacheDir.FullName, $"{att.Key}{Path.GetExtension(att.Value)}")); Console.WriteLine($"{file.Name}\tDownloading"); using HttpResponseMessage response = await httpClient.GetAsync(att.Value); using Stream downloadStream = await response.Content.ReadAsStreamAsync(); downloadStream.Seek(0, SeekOrigin.Begin); downloadStream.CopyTo(file); Console.WriteLine($"{file.Name}\tDone"); } finally { semaphore.Release(); downloadsCompleted++; } }); } while (downloadsCompleted < totalDownloads) { Task.Delay(SemaphoreCheckTime); } semaphore.Dispose(); return(Task.CompletedTask); }
bool JoinChat(string master, string slave, bool activate) { string primaryKey = m_PrimaryKey == master ? slave : master; FriendList.FriendInfo friend = GameCloudManager.friendList.friends.Find(obj => obj.PrimaryKey == primaryKey); if (friend == null) { return(false); } int idx = m_Friends.FindIndex(obj => obj.PrimaryKey == primaryKey); if (idx < 0) { idx = m_Friends.Count; m_Friends.Add(new FriendInfo() { PrimaryKey = friend.PrimaryKey, Nickname = friend.Nickname, Rank = friend.Rank }); m_FriendList.MaxItems = m_Friends.Count; } FriendInfo info = m_Friends[idx]; bool sendJoined = false; if (info.Master != master) { Chat.Unregister(info.Channel, this); sendJoined = true; } info.Master = master; info.Slave = slave; info.Channel = ChannelCache.GetChannelFor(m_PrimaryKey + primaryKey, master, slave); Chat.Register(info.Channel, this); if (activate == true || m_ActiveFriend < 0) { ActivateChat(idx); } m_FriendList.Widget.SetModify(); if (sendJoined == true) { var result = new { master = info.Master, slave = info.Slave }; LobbyClient.SendMessageToPlayer(info.Master == m_PrimaryKey ? info.Slave : info.Master, CMD_JOINED, JsonMapper.ToJson(result)); } return(true); }
private void SaveChannelCache(ChannelCache newCache) => SaveChannelCache(newCache.Channel, newCache);