Exemplo n.º 1
0
    public async Task <SearchResponse <Content> > SearchContentAsync(
        ContentSearchRequest contentSearchRequest,
        int skip = 0,
        int take = 5,
        CancellationToken cancellationToken = default)
    {
        var searchResponse = await ContentRepository.SearchContentAsync(
            contentSearchRequest,
            skip,
            take,
            cancellationToken);

        foreach (var content in searchResponse.Items)
        {
            var cacheKey = $"{CacheKeyPrefix}{content.Path}";

            if (await DistributedCache.GetAsync <Content>(
                    cacheKey, JsonSerializerOptions, cancellationToken) == null)
            {
                await DistributedCache.SetAsync(
                    cacheKey,
                    content,
                    DistributedCacheEntryOptions,
                    JsonSerializerOptions,
                    cancellationToken);
            }
        }

        return(searchResponse);
    }
Exemplo n.º 2
0
    public async Task <Content?> GetContentAsync(
        string path,
        CancellationToken cancellationToken = default)
    {
        var cacheKey = $"{CacheKeyPrefix}{path}";

        var content = await DistributedCache.GetAsync <Content>(
            cacheKey, JsonSerializerOptions, cancellationToken);

        if (content == null)
        {
            content = await ContentRepository.GetContentAsync(
                path, cancellationToken);

            if (content != null)
            {
                await DistributedCache.SetAsync(
                    cacheKey,
                    content,
                    DistributedCacheEntryOptions,
                    JsonSerializerOptions,
                    cancellationToken);
            }
        }

        return(content);
    }
Exemplo n.º 3
0
        public async Task GetAsync_ReturnsBoolCachedValue()
        {
            // Arrange

            var        key         = Guid.NewGuid().ToString();
            const bool cachedValue = true;

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = await sut.GetAsync <bool>(key);

            // Assert
            Assert.AreEqual(cachedValue, result);
        }