コード例 #1
0
        public async Task <CoverArtModel> GetOrQueueCoverArt(Guid mbid, Guid artistMbid)
        {
            CoverArtModel coverArt;

            if (_memoryCache.TryGetValue(mbid, out coverArt))
            {
                _logger.LogInformation($"Getting cover art {coverArt.Mbid} from memory cache.");
            }
            else
            {
                _logger.LogInformation($"Cover art {mbid} is not in memory cache.");
                coverArt = await _dataService.GetCoverArt(mbid);

                // If there's no CoverArtModel, fetch it
                if (coverArt == null || coverArt.ShouldReFetch)
                {
                    // Set the background worker to retrieve the artwork
                    await _queue.EnqueueAsync(new CoverArtJob()
                    {
                        Mbid = mbid, ArtistMbid = artistMbid
                    }, CancellationToken.None);
                }
                else
                {
                    // Add the response to cache
                    _memoryCache.Set(mbid, coverArt, coverArt.Exists ? TimeSpan.FromMinutes(5) : TimeSpan.FromDays(1));
                }
            }
            return(coverArt);
        }