Exemplo n.º 1
0
        /// <summary>
        /// Get a value from the cache if all constraints are fulfilled.
        /// </summary>
        protected internal async Task <PageEnvelope <TModel> > CacheGetAsync(int offset, int limit, string keyPrefix, CancellationToken cancellationToken)
        {
            if (UseCacheAtAllMethodAsync != null &&
                !await UseCacheAtAllMethodAsync(typeof(PageEnvelope <TModel>), cancellationToken))
            {
                return(null);
            }
            var key = GetCacheKeyForPage(keyPrefix, offset, limit);

            return(await GetAndMaybeReturnAsync(key, cacheEnvelope => SerializingSupport.Deserialize <PageEnvelope <TModel> >(cacheEnvelope.Data), cancellationToken : cancellationToken));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get a value from the cache if all constraints are fulfilled.
        /// </summary>
        protected internal async Task <TModel[]> CacheGetAsync(int limit, string key, CancellationToken cancellationToken)
        {
            InternalContract.RequireGreaterThan(0, limit, nameof(limit));
            if (limit > _limitOfItemsInReadAllCache)
            {
                return(null);
            }
            if (UseCacheAtAllMethodAsync != null &&
                !await UseCacheAtAllMethodAsync(typeof(TModel[]), cancellationToken))
            {
                return(null);
            }

            return(await GetAndMaybeReturnAsync(key, cacheEnvelope =>
            {
                var array = SerializingSupport.Deserialize <TModel[]>(cacheEnvelope.Data);
                if (limit > array.Length)
                {
                    return array;
                }
                var subset = array.Take(limit);
                return subset as TModel[] ?? subset.ToArray();
            }, cancellationToken : cancellationToken));
        }