예제 #1
0
    public async Task <IEnumerable <Category> > GetAllCategoriesAsync(string[] roles)
    {
        var categories = await GetCachedValueAsync(
            () => _cache.GetCategoriesAsync(roles),
            () => _repo.GetAllCategoriesAsync(roles)
            );

        return(categories ?? new List <Category>());
    }
    // TODO: support removing categories and permissions from cache
    async Task UpdateCategoryCache(CancellationToken stoppingToken)
    {
        if (stoppingToken.IsCancellationRequested)
        {
            return;
        }

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

        var dbCategoriesAndRoles = await _repo.GetCategoriesAndRolesAsync();

        var allRoles     = dbCategoriesAndRoles.SelectMany(x => x.Roles).Distinct().ToArray();
        var dbCategories = await _repo.GetAllCategoriesAsync(allRoles);

        var cacheCategories = await _cache.GetCategoriesAsync(allRoles);

        var updatedCategories = dbCategories.Except(cacheCategories.Item ?? new List <Category>());

        if (updatedCategories.Count() > 0)
        {
            var securedCategories = updatedCategories
                                    .Select(category => new SecuredResource <Category>(
                                                category,
                                                dbCategoriesAndRoles
                                                .First(x => x.Id == category.Id)
                                                .Roles
                                                ));

            await _cache.AddCategoriesAsync(securedCategories);

            _logger.LogInformation("{service} updated {count} video categories", nameof(VideoCacheProcessingService), updatedCategories.Count());
        }

        await UpdateVideoCache(dbCategories, dbCategoriesAndRoles, stoppingToken);

        if (stoppingToken.IsCancellationRequested)
        {
            return;
        }

        await _cache.SetStatusAsync(CacheStatus.InitializationSucceeded);
    }