Skip to content

Commit

Permalink
Added caching examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmwebb-lv committed Aug 9, 2024
1 parent 1344d84 commit be56169
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Src\RCommon.JsonNet\RCommon.JsonNet.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.MemoryCache\RCommon.MemoryCache.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace Examples.Caching.MemoryCaching
{
public interface ITestApplicationService
{
TestDto GetDistributedMemoryCache(string key);
TestDto GetMemoryCache(string key);
void SetDistributedMemoryCache(string key, Type type, object data);
void SetMemoryCache(string key, TestDto data);
}
}
31 changes: 23 additions & 8 deletions Examples/Caching/Examples.Caching.MemoryCaching/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,38 @@
{
// Configure RCommon
services.AddRCommon()
.WithCaching<MemoryCachingBuilder>(cache =>
.WithMemoryCaching<MemoryCachingBuilder>(cache =>
{
cache.Configure(x =>
{
x.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
});
})
.WithDistributedCaching<DistributedMemoryCacheBuilder>(cache =>
{
cache.Configure(x =>
{
x.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
});
});
services.AddTransient<ITestApplicationService, TestApplicationService>();
}).Build();

Console.WriteLine("Example Starting");
var appService = host.Services.GetRequiredService<ITestApplicationService>();
await appService.SetCache();
await appService.GetCache();

Console.WriteLine("");
Console.WriteLine("");
// In Memory Cache
appService.SetMemoryCache("test-key", new TestDto("test data 1"));
var testData1 = appService.GetMemoryCache("test-key");

// In Memory Distributed Cache
appService.SetDistributedMemoryCache("test-key", typeof(TestDto), new TestDto("test data 2"));
var testData2 = appService.GetDistributedMemoryCache("test-key");

Console.WriteLine(testData1.Message);
Console.WriteLine(testData2.Message);

Console.WriteLine("Example Complete");
Console.ReadLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using RCommon.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -8,35 +9,38 @@

namespace Examples.Caching.MemoryCaching
{
public class TestApplicationService
public class TestApplicationService : ITestApplicationService
{
private readonly IMemoryCache _memoryCache;
private readonly IDistributedCache _distributedCache;
private readonly IJsonSerializer _serializer;

public TestApplicationService(IMemoryCache memoryCache, IDistributedCache distributedCache)
public TestApplicationService(IMemoryCache memoryCache, IDistributedCache distributedCache, IJsonSerializer serializer)
{
_memoryCache = memoryCache;
_distributedCache = distributedCache;
_serializer = serializer;
}

public async Task SetMemoryCache()
public void SetMemoryCache(string key, TestDto data)
{

_memoryCache.Set<TestDto>(key, data);
}

public async Task GetMemoryCache()
public TestDto GetMemoryCache(string key)
{

return _memoryCache.Get<TestDto>(key);
}

public async Task SetDistributedMemoryCache()
public void SetDistributedMemoryCache(string key, Type type, object data)
{

_distributedCache.Set(key, Encoding.UTF8.GetBytes(_serializer.Serialize(data, type)));
}

public async Task GetDistributedMemoryCache()
public TestDto GetDistributedMemoryCache(string key)
{

var cache = _distributedCache.Get(key);
return _serializer.Deserialize<TestDto>(Encoding.UTF8.GetString(cache));
}
}
}
37 changes: 6 additions & 31 deletions Examples/Caching/Examples.Caching.PersistenceCaching/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using Examples.ApplicationServices.CQRS.Validators;

using Examples.Caching.PersistenceCaching;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RCommon;
using RCommon.ApplicationServices;
using RCommon.ApplicationServices.ExecutionResults;
using RCommon.FluentValidation;
using System.Diagnostics;
using System.Reflection;

try
{
var host = Host.CreateDefaultBuilder(args)
/*var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
Expand All @@ -22,39 +19,17 @@
.ConfigureServices(services =>
{
// Configure RCommon
services.AddRCommon()
.WithCQRS<CqrsBuilder>(cqrs =>
{
// You can do it this way which is pretty straight forward but verbose
//cqrs.AddQueryHandler<TestQueryHandler, TestQuery, TestDto>();
//cqrs.AddCommandHandler<TestCommandHandler, TestCommand, IExecutionResult>();
// Or this way which uses a little magic but is simple
cqrs.AddCommandHandlers((typeof(Program).GetTypeInfo().Assembly));
cqrs.AddQueryHandlers((typeof(Program).GetTypeInfo().Assembly));
})
.WithValidation<FluentValidationBuilder>(validation =>
{
validation.AddValidatorsFromAssemblyContaining(typeof(TestCommand));
validation.UseWithCqrs(options =>
{
options.ValidateCommands = true;
options.ValidateQueries = true;
});
});
services.AddRCommon();
services.AddTransient<ITestApplicationService, TestApplicationService>();
}).Build();
Console.WriteLine("Example Starting");
var appService = host.Services.GetRequiredService<ITestApplicationService>();
var commandResult = await appService.ExecuteTestCommand(new TestCommand("test"));
var queryResult = await appService.ExecuteTestQuery(new TestQuery());
var appService = host.Services.GetRequiredService<ITestApplicationService>();*/

Console.WriteLine(commandResult.ToString());
Console.WriteLine(queryResult.Message);
Console.WriteLine("");
Console.WriteLine("");

Console.WriteLine("Example Complete");
Console.ReadLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
using RCommon.ApplicationServices.Commands;
using RCommon.ApplicationServices.ExecutionResults;
using RCommon.ApplicationServices.Queries;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Examples.Caching.PersistenceCaching
{
public class TestApplicationService : ITestApplicationService
public class TestApplicationService
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;


public TestApplicationService(ICommandBus commandBus, IQueryBus queryBus)
public TestApplicationService()
{
_commandBus = commandBus;
_queryBus = queryBus;

}

public async Task<TestDto> ExecuteTestQuery(TestQuery query)
{
return await _queryBus.DispatchQueryAsync(query, CancellationToken.None);
}

public async Task<IExecutionResult> ExecuteTestCommand(TestCommand command)
{
return await _commandBus.DispatchCommandAsync(command, CancellationToken.None);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Src\RCommon.JsonNet\RCommon.JsonNet.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.RedisCache\RCommon.RedisCache.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace Examples.Caching.RedisCaching
{
public interface ITestApplicationService
{
TestDto GetDistributedMemoryCache(string key);
void SetDistributedMemoryCache(string key, Type type, object data);
}
}
42 changes: 15 additions & 27 deletions Examples/Caching/Examples.Caching.RedisCaching/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Examples.ApplicationServices.CQRS.Validators;
using Examples.Caching.RedisCaching;
using Examples.Caching.RedisCaching;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RCommon;
using RCommon.ApplicationServices;
using RCommon.ApplicationServices.ExecutionResults;
using RCommon.FluentValidation;
using RCommon.Caching;
using RCommon.RedisCache;
using System.Diagnostics;
using System.Reflection;

Expand All @@ -23,38 +21,27 @@
{
// Configure RCommon
services.AddRCommon()
.WithCQRS<CqrsBuilder>(cqrs =>
.WithDistributedCaching<RedisCachingBuilder>(cache =>
{
// You can do it this way which is pretty straight forward but verbose
//cqrs.AddQueryHandler<TestQueryHandler, TestQuery, TestDto>();
//cqrs.AddCommandHandler<TestCommandHandler, TestCommand, IExecutionResult>();
// Or this way which uses a little magic but is simple
cqrs.AddCommandHandlers((typeof(Program).GetTypeInfo().Assembly));
cqrs.AddQueryHandlers((typeof(Program).GetTypeInfo().Assembly));
})
.WithValidation<FluentValidationBuilder>(validation =>
{
validation.AddValidatorsFromAssemblyContaining(typeof(TestCommand));
validation.UseWithCqrs(options =>
cache.Configure(redis =>
{
options.ValidateCommands = true;
options.ValidateQueries = true;
// Redis Configuration
});
});
services.AddTransient<ITestApplicationService, TestApplicationService>();
}).Build();

Console.WriteLine("Example Starting");
var appService = host.Services.GetRequiredService<ITestApplicationService>();
var commandResult = await appService.ExecuteTestCommand(new TestCommand("test"));
var queryResult = await appService.ExecuteTestQuery(new TestQuery());

Console.WriteLine(commandResult.ToString());
Console.WriteLine(queryResult.Message);
// In Memory Distributed Cache
appService.SetDistributedMemoryCache("test-key", typeof(TestDto), new TestDto("test data 1"));
var testData1 = appService.GetDistributedMemoryCache("test-key");

Console.WriteLine(testData1.Message);

Console.WriteLine("Example Complete");
Console.ReadLine();
Expand All @@ -65,3 +52,4 @@

}


Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using RCommon.ApplicationServices.Commands;
using RCommon.ApplicationServices.ExecutionResults;
using RCommon.ApplicationServices.Queries;
using Microsoft.Extensions.Caching.Distributed;
using RCommon.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,23 +10,24 @@ namespace Examples.Caching.RedisCaching
{
public class TestApplicationService : ITestApplicationService
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;
private readonly IDistributedCache _distributedCache;
private readonly IJsonSerializer _serializer;

public TestApplicationService(ICommandBus commandBus, IQueryBus queryBus)
public TestApplicationService(IDistributedCache distributedCache, IJsonSerializer serializer)
{
_commandBus = commandBus;
_queryBus = queryBus;
_distributedCache = distributedCache;
_serializer = serializer;
}

public async Task<TestDto> ExecuteTestQuery(TestQuery query)
public void SetDistributedMemoryCache(string key, Type type, object data)
{
return await _queryBus.DispatchQueryAsync(query, CancellationToken.None);
_distributedCache.Set(key, Encoding.UTF8.GetBytes(_serializer.Serialize(data, type)));
}

public async Task<IExecutionResult> ExecuteTestCommand(TestCommand command)
public TestDto GetDistributedMemoryCache(string key)
{
return await _commandBus.DispatchCommandAsync(command, CancellationToken.None);
var cache = _distributedCache.Get(key);
return _serializer.Deserialize<TestDto>(Encoding.UTF8.GetString(cache));
}
}
}
18 changes: 18 additions & 0 deletions Examples/Caching/Examples.Caching.RedisCaching/TestDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Examples.Caching.RedisCaching
{
public record TestDto
{
public TestDto(string message)
{
Message = message;
}

public string Message { get; }
}
}
2 changes: 1 addition & 1 deletion Examples/Mediator/Examples.Mediator.MediatR/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
mediator.AddNotification<TestNotification, TestNotificationHandler>();
mediator.AddRequest<TestRequest, TestRequestHandler>();
mediator.AddRequest<TestRequestWithResponse, TestResponse, TestRequestHandlerWithResponse>();
// Additional configurations can be set like below
mediator.Configure(config =>
{
Expand Down
Loading

0 comments on commit be56169

Please sign in to comment.