protected async Task <T> FindOneAsync(OneOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (EnableCache && options.UseCache) { var cacheValue = await Cache.GetAsync <T>(GetScopedCacheKey(options.CacheKey)).AnyContext(); #if DEBUG Logger.Trace().Message("Cache {0}: type={1}", cacheValue.HasValue ? "hit" : "miss", _entityType).Write(); #endif if (cacheValue.HasValue) { return(cacheValue.Value); } } var searchDescriptor = new SearchDescriptor <T>().Query(options.GetElasticSearchQuery <T>(_supportsSoftDeletes)).Size(1); if (options.Fields.Count > 0) { searchDescriptor.Source(s => s.Include(options.Fields.ToArray())); } else { searchDescriptor.Source(s => s.Exclude("idx")); } var elasticSearchOptions = options as ElasticSearchOptions <T>; searchDescriptor.Indices(elasticSearchOptions != null && elasticSearchOptions.Indices.Any() ? elasticSearchOptions.Indices.ToArray() : GetIndices()); if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0) { foreach (var sort in elasticSearchOptions.SortBy) { searchDescriptor.Sort(sort); } } #if DEBUG _elasticClient.EnableTrace(); var sw = Stopwatch.StartNew(); #endif var results = await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext(); #if DEBUG sw.Stop(); _elasticClient.DisableTrace(); Logger.Trace().Message($"FindOneAsync: {sw.ElapsedMilliseconds}ms, Elastic Took {results.ElapsedMilliseconds}ms, Serialization Took {results.ConnectionStatus.Metrics.SerializationTime}ms, Deserialization Took {results.ConnectionStatus.Metrics.DeserializationTime}ms").Write(); #endif var result = results.Documents.FirstOrDefault(); if (EnableCache && options.UseCache && result != null) { await Cache.SetAsync(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate()).AnyContext(); } return(result); }
protected bool Exists(OneOptions options) { if (options == null) { throw new ArgumentNullException("options"); } var findArgs = new FindOneArgs { Query = options.GetMongoQuery(_getIdValue), Fields = Fields.Include(CommonFieldNames.Id), ReadPreference = ReadPreference.Primary }; return(_collection.FindOneAs <T>(findArgs) != null); }
protected TModel FindOne <TModel>(OneOptions options) where TModel : class, new() { if (options == null) { throw new ArgumentNullException("options"); } TModel result = null; if (options.UseCache) { result = Cache.Get <TModel>(GetScopedCacheKey(options.CacheKey)); } if (result != null) { return(result); } var findArgs = new FindOneArgs { Query = options.GetMongoQuery(_getIdValue), Fields = Fields.Include(options.Fields.ToArray()) }; var mongoOptions = options as MongoOptions; if (mongoOptions != null && mongoOptions.SortBy != null) { findArgs.SortBy = mongoOptions.SortBy; } if (mongoOptions != null && mongoOptions.ReadPreference != null) { findArgs.ReadPreference = mongoOptions.ReadPreference; } result = _collection.FindOneAs <TModel>(findArgs); if (result != null && options.UseCache) { Cache.Set(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate()); } return(result); }
protected bool Exists(OneOptions options) { if (options == null) { throw new ArgumentNullException("options"); } options.Fields.Add("id"); var searchDescriptor = new SearchDescriptor <T>().Filter(options.GetElasticSearchFilter <T>()).Size(1); var elasticSearchOptions = options as ElasticSearchOptions <T>; if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0) { searchDescriptor.Indices(elasticSearchOptions.Indices); foreach (var sort in elasticSearchOptions.SortBy) { searchDescriptor.Sort(sort); } } return(_elasticClient.Search <T>(searchDescriptor).HitsMetaData.Total > 0); }
protected async Task <bool> ExistsAsync(OneOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.Fields.Add("id"); var searchDescriptor = new SearchDescriptor <T>().Query(options.GetElasticSearchQuery <T>(_supportsSoftDeletes)).Size(1); var elasticSearchOptions = options as ElasticSearchOptions <T>; searchDescriptor.Indices(elasticSearchOptions != null && elasticSearchOptions.Indices.Any() ? elasticSearchOptions.Indices.ToArray() : GetIndices()); if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0) { foreach (var sort in elasticSearchOptions.SortBy) { searchDescriptor.Sort(sort); } } #if DEBUG _elasticClient.EnableTrace(); var sw = Stopwatch.StartNew(); #endif var results = await _elasticClient.SearchAsync <T>(searchDescriptor).AnyContext(); #if DEBUG sw.Stop(); _elasticClient.DisableTrace(); Logger.Trace().Message($"ExistsAsync: {sw.ElapsedMilliseconds}ms, Elastic Took {results.ElapsedMilliseconds}ms, Serialization Took {results.ConnectionStatus.Metrics.SerializationTime}ms, Deserialization Took {results.ConnectionStatus.Metrics.DeserializationTime}ms").Write(); #endif return(results.HitsMetaData.Total > 0); }
protected TModel FindOneAs <TModel>(OneOptions options) where TModel : class, new() { if (options == null) { throw new ArgumentNullException("options"); } TModel result = null; if (EnableCache) { if (options.UseCache) { result = Cache.Get <TModel>(GetScopedCacheKey(options.CacheKey)); } if (options.UseCache && result != null) { Log.Trace().Message("Cache hit: type={0}", _entityType).Write(); } else if (options.UseCache) { Log.Trace().Message("Cache miss: type={0}", _entityType).Write(); } if (result != null) { return(result); } } var searchDescriptor = new SearchDescriptor <T>().Filter(options.GetElasticSearchFilter <T>()).Size(1); if (options.Fields.Count > 0) { searchDescriptor.Source(s => s.Include(options.Fields.ToArray())); } else { searchDescriptor.Source(s => s.Exclude("idx")); } var elasticSearchOptions = options as ElasticSearchOptions <T>; if (elasticSearchOptions != null && elasticSearchOptions.SortBy.Count > 0) { searchDescriptor.Indices(elasticSearchOptions.Indices); foreach (var sort in elasticSearchOptions.SortBy) { searchDescriptor.Sort(sort); } } _elasticClient.EnableTrace(); var item = _elasticClient.Search <T>(searchDescriptor).Documents.FirstOrDefault(); _elasticClient.DisableTrace(); if (typeof(T) != typeof(TModel)) { if (Mapper.FindTypeMapFor <T, TModel>() == null) { Mapper.CreateMap <T, TModel>(); } result = Mapper.Map <T, TModel>(item); } else { result = item as TModel; } if (EnableCache && result != null && options.UseCache) { Cache.Set(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate()); } return(result); }
protected T FindOne(OneOptions options) { return(FindOneAs <T>(options)); }