Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
dotfelixb committed Mar 12, 2024
1 parent 5a8b375 commit f8c997d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/NQuery.Core/Interfaces/INQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ namespace NQuery.Core.Interfaces;

public interface INQuery
{
Task<TOutput?> QueryAsync<TOutput>(
Task<T?> QueryAsync<T>(
string key,
Func<Task<TOutput?>> query);
Func<Task<T?>> query);

Task<IEnumerable<TOutput>> QueryAsync<TOutput>(
Task<IEnumerable<T>> QueryAsync<T>(
string key,
Func<Task<IEnumerable<TOutput>>> query);
Func<Task<IEnumerable<T>>> query);

Task<TOutput?> MutationAsync<TOutput>(
Task<T?> MutationAsync<T>(
string key,
Func<Task<TOutput?>> mutate);
Func<Task<T?>> mutate);
}
26 changes: 13 additions & 13 deletions src/NQuery.Core/NQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,33 @@ public static NQuery Create(NQueryConfiguration configuration)
return new NQuery(configuration, ConnectionMultiplexer.Connect(stackExchangeRedisConfig));
}

public async Task<TOutput?> QueryAsync<TOutput>(string key, Func<Task<TOutput?>> query)
public async Task<T?> QueryAsync<T>(string key, Func<Task<T?>> query)
{
return _configuration.UseInMemory
? await InMemoryQuery(key, query)
: await ExternalQuery(key, query);
}

public async Task<IEnumerable<TOutput>> QueryAsync<TOutput>(string key, Func<Task<IEnumerable<TOutput>>> query)
public async Task<IEnumerable<T>> QueryAsync<T>(string key, Func<Task<IEnumerable<T>>> query)
{
return _configuration.UseInMemory
? await InMemoryQuery(key, query)
: await ExternalQuery(key, query);
}

public async Task<TOutput?> MutationAsync<TOutput>(string key, Func<Task<TOutput?>> mutate)
public async Task<T?> MutationAsync<T>(string key, Func<Task<T?>> mutate)
{
return _configuration.UseInMemory
? await InMemoryMutate(key, mutate)
: await ExternalMutate(key, mutate);
}

private async Task<TOutput> InMemoryQuery<TOutput>(string key, Func<Task<TOutput>> query)
private async Task<T> InMemoryQuery<T>(string key, Func<Task<T>> query)
{
// check if key is already set
if (_memoryDb.TryGetValue(key, out var value))
{
return (TOutput)value;
return (T)value;
}

// key not set
Expand All @@ -99,12 +99,12 @@ private async Task<TOutput> InMemoryQuery<TOutput>(string key, Func<Task<TOutput
return rst;
}

private async Task<IEnumerable<TOutput>> InMemoryQuery<TOutput>(string key, Func<Task<IEnumerable<TOutput>>> query)
private async Task<IEnumerable<T>> InMemoryQuery<T>(string key, Func<Task<IEnumerable<T>>> query)
{
// check if key is already set
if (_memoryDb.TryGetValue(key, out var value))
{
return (IEnumerable<TOutput>)value;
return (IEnumerable<T>)value;
}

// key not set
Expand All @@ -118,7 +118,7 @@ private async Task<IEnumerable<TOutput>> InMemoryQuery<TOutput>(string key, Func
return rstLst;
}

private async Task<TOutput?> InMemoryMutate<TOutput>(string key, Func<Task<TOutput?>> mutate)
private async Task<T?> InMemoryMutate<T>(string key, Func<Task<T?>> mutate)
{
// check if key is already set
if (_memoryDb.ContainsKey(key))
Expand All @@ -129,13 +129,13 @@ private async Task<IEnumerable<TOutput>> InMemoryQuery<TOutput>(string key, Func
return await mutate();
}

private async Task<TOutput?> ExternalQuery<TOutput>(string key, Func<Task<TOutput>> query)
private async Task<T?> ExternalQuery<T>(string key, Func<Task<T>> query)
{
// check if key is already set
var exist = await _redisDb.StringGetAsync(new RedisKey(key));
if (!exist.IsNull)
{
return JsonSerializer.Deserialize<TOutput>(exist!);
return JsonSerializer.Deserialize<T>(exist!);
}

// key not set
Expand All @@ -149,13 +149,13 @@ await _redisDb.StringSetAsync(
return rst;
}

private async Task<IEnumerable<TOutput>> ExternalQuery<TOutput>(string key, Func<Task<IEnumerable<TOutput>>> query)
private async Task<IEnumerable<T>> ExternalQuery<T>(string key, Func<Task<IEnumerable<T>>> query)
{
// check if key is already set
var exist = await _redisDb.StringGetAsync(new RedisKey(key));
if (!exist.IsNull)
{
return JsonSerializer.Deserialize<IEnumerable<TOutput>>(exist);
return JsonSerializer.Deserialize<IEnumerable<T>>(exist);

Check warning on line 158 in src/NQuery.Core/NQuery.cs

View workflow job for this annotation

GitHub Actions / Build

Possible null reference argument for parameter 'json' in 'IEnumerable<T>? JsonSerializer.Deserialize<IEnumerable<T>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 158 in src/NQuery.Core/NQuery.cs

View workflow job for this annotation

GitHub Actions / Build

Possible null reference return.
}

// key not set
Expand All @@ -170,7 +170,7 @@ await _redisDb.StringSetAsync(
return rstLst;
}

private async Task<TOutput?> ExternalMutate<TOutput>(string key, Func<Task<TOutput?>> mutate)
private async Task<T?> ExternalMutate<T>(string key, Func<Task<T?>> mutate)
{
// check if key is already set
var exist = await _redisDb.StringGetAsync(new RedisKey(key));
Expand Down
6 changes: 2 additions & 4 deletions src/NQuery.Test/AspNetQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NQuery.Test;

public class AspNetQueryTest
{
readonly ServiceCollection _services = new ServiceCollection();
private readonly ServiceCollection _services = new ServiceCollection();

[SetUp]
public void Setup()
Expand All @@ -17,9 +17,7 @@ public void Setup()
cfg.UseRedis(opts =>
{
opts.Endpoints.Add(new RedisEndpoint() { Host = "127.0.0.1", Port = 6379 });
opts.AbortOnConnectFail = false;
opts.ResolveDns = true;
opts.UseSsl = false;
opts.AbortOnConnectFail = false;
});
});
}
Expand Down

0 comments on commit f8c997d

Please sign in to comment.