Example #1
0
    async Task UpdateBlogCache(CancellationToken stoppingToken)
    {
        if (stoppingToken.IsCancellationRequested)
        {
            return;
        }

        if (await _cache.GetStatusAsync() != CacheStatus.InitializationSucceeded)
        {
            await _cache.SetStatusAsync(CacheStatus.Initializing);
        }

        var dbBlogs = await _repo.GetBlogsAsync();

        var cacheBlogs = await _cache.GetBlogsAsync();

        var updatedBlogs = dbBlogs.Except(cacheBlogs.Item ?? new List <Blog>());

        if (updatedBlogs.Count() > 0)
        {
            await _cache.AddBlogsAsync(updatedBlogs);

            _logger.LogInformation("{service} updated {count} blog(s)", nameof(BlogCacheProcessingService), updatedBlogs.Count());
        }

        await UpdatePostCache(dbBlogs, stoppingToken);

        if (stoppingToken.IsCancellationRequested)
        {
            return;
        }

        await _cache.SetStatusAsync(CacheStatus.InitializationSucceeded);
    }