public virtual async Task <T> GetByIdAsync(Id id, ICommandOptions options = null)
        {
            if (String.IsNullOrEmpty(id.Value))
            {
                return(null);
            }

            options = ConfigureOptions(options.As <T>());
            if (IsCacheEnabled && options.HasCacheKey())
            {
                throw new ArgumentException("Cache key can't be set when calling GetById");
            }

            if (IsCacheEnabled && options.ShouldReadCache())
            {
                var value = await GetCachedFindHit(id).AnyContext();

                if (value?.Document != null)
                {
                    _logger.LogTrace("Cache hit: type={EntityType} key={Id}", EntityTypeName, id);

                    return(ShouldReturnDocument(value.Document, options) ? value.Document : null);
                }
            }

            FindHit <T> findHit;

            if (!HasParent || id.Routing != null)
            {
                var request = new GetRequest(ElasticIndex.GetIndex(id), id.Value);
                if (id.Routing != null)
                {
                    request.Routing = id.Routing;
                }
                var response = await _client.GetAsync <T>(request).AnyContext();

                _logger.LogRequest(response, options.GetQueryLogLevel());

                findHit = response.Found ? response.ToFindHit() : null;
            }
            else
            {
                // we don't have the parent id so we have to do a query
                // TODO: Ensure this find one query is not cached.
                findHit = await FindOneAsync(NewQuery().Id(id), options.Clone().DefaultCacheKey(id)).AnyContext();
            }

            if (IsCacheEnabled && options.ShouldUseCache())
            {
                await AddDocumentsToCacheAsync(findHit ?? new FindHit <T>(id, null, 0), options).AnyContext();
            }

            return(ShouldReturnDocument(findHit?.Document, options) ? findHit?.Document : null);
        }
Beispiel #2
0
 public static ICommandOptions Clone(this ICommandOptions source)
 {
     return(source.Clone <CommandOptions>());
 }
 public static ICommandOptions <T> Clone <T>(this ICommandOptions <T> source) where T : class
 {
     return(source.Clone <CommandOptions <T> >());
 }