Skip to content

Commit

Permalink
Added two ASP.NET Core usage examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLamansky committed Jun 24, 2024
1 parent 334bde4 commit 8a97d1f
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,48 @@ The more concurrent requests, the greater the benefit: this solution thrives und

This _is not_ a cache: once an action is completed, the results are shared with everyone supplying the same input and discarded.

To use, copy SharedAction/SharedAction.cs directly into your project.
To get started, copy [SharedAction/SharedAction.cs](SharedAction/SharedAction.cs) directly into your project.

## How to Use

Note: if you load test these using a browser, you may encounter stalls due to queuing.
A purpose-built load generator tool is recommended.

### Web API controller

```C#
public record Inventory(decimal Price, int InventoryCount); // Example data object.
// The key must include every input variable.
private static readonly SharedAction<(string Sku, int WarehouseId), Inventory?> inventoryActions = new();

[HttpGet]
public async Task<Inventory?> GetAsync(string sku, int warehouseId, CancellationToken cancellationToken)
{
return await inventoryActions.RunAsync((sku, warehouseId), async (input, cancellationToken) =>
{
// The "input" parameter avoids the overhead of capturing variables.
// "GetInventoryAsync" is a placeholder for the real call to the underlying system.
return await GetInventoryAsync(input.Sku, input.WarehouseId, cancellationToken);
}, cancellationToken);
}
```

### ASP.NET Core Minimal API

```C#
// The key must include every input variable.
var inventoryActions = new SharedAction<(string Sku, int WarehouseId), Inventory?>();

app.MapGet("/api/realtimeinventory", async (string sku, int warehouseId, CancellationToken cancellationToken) =>
{
var result = await inventoryActions.RunAsync((sku, warehouseId), async (input, cancellationToken) =>
{
// The "input" parameter avoids the overhead of capturing variables.
// "GetInventoryAsync" is a placeholder for the real call to the underlying system.
return await GetInventoryAsync(input.Sku, input.WarehouseId, cancellationToken);
}, cancellationToken);

return result;
});
```

0 comments on commit 8a97d1f

Please sign in to comment.