Skip to content

Commit

Permalink
Simplify example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Layoric committed Mar 14, 2024
1 parent c779350 commit 5cadf21
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 49 deletions.
2 changes: 2 additions & 0 deletions BlazorOutputCaching.ServiceInterface/MyServices.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using ServiceStack;
using BlazorOutputCaching.ServiceModel;
using Microsoft.AspNetCore.OutputCaching;

namespace BlazorOutputCaching.ServiceInterface;

[OutputCache(Duration = 60)]
public class MyServices : Service
{
public object Any(Hello request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
<Folder Include="Types\" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.OutputCaching">
<HintPath>..\..\..\..\.dotnet\packs\Microsoft.AspNetCore.App.Ref\8.0.2\ref\net8.0\Microsoft.AspNetCore.OutputCaching.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions BlazorOutputCaching/BlazorOutputCaching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<Content Include="_videos\**" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>

<Target Name="tailwind" BeforeTargets="Publish">
<Exec Command="npm run ui:build" WorkingDirectory="./" />
</Target>
Expand Down
43 changes: 0 additions & 43 deletions BlazorOutputCaching/Configure.AppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,4 @@ public override void Configure()
}
}

public class RedisOutputCacheStore : IOutputCacheStore
{
private readonly IRedisClientsManager _redisManager;

public RedisOutputCacheStore(IRedisClientsManager redisManager)
{
_redisManager = redisManager;
}

public async ValueTask<byte[]?> GetAsync(string key, CancellationToken cancellationToken)
{
await using var redis = await _redisManager.GetClientAsync(token: cancellationToken);
var value = await redis.GetAsync<byte[]>(key, cancellationToken);
return value;
}

public async ValueTask SetAsync(string key, byte[] value, string[]? tags, TimeSpan validFor, CancellationToken cancellationToken)
{
await using var redis = await _redisManager.GetClientAsync(token: cancellationToken);

// First persist in normal cache hashset
await redis.SetAsync(key, value, validFor, cancellationToken);

if (tags == null)
return;
foreach (var tag in tags)
{
await redis.AddItemToSetAsync($"tag:{tag}", key, cancellationToken);
}
}

public async ValueTask EvictByTagAsync(string tag, CancellationToken cancellationToken)
{
await using var redis = await _redisManager.GetClientAsync(token: cancellationToken);

var keys = await redis.GetAllItemsFromListAsync($"tag:{tag}", cancellationToken);

foreach (var key in keys)
{
await redis.RemoveEntryAsync(key);
await redis.RemoveItemFromSetAsync($"tag:{tag}", key, cancellationToken);
}
}
}
57 changes: 57 additions & 0 deletions BlazorOutputCaching/Configure.OutputCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.OutputCaching;
using ServiceStack.Redis;

[assembly: HostingStartup(typeof(BlazorOutputCaching.ConfigureOutputCache))]

namespace BlazorOutputCaching;

public class ConfigureOutputCache : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
services.AddSingleton<IRedisClientsManager>(c =>
new BasicRedisClientManager("localhost:6379"));
services.AddSingleton<IOutputCacheStore, RedisOutputCacheStore>();
});
}
}

public class RedisOutputCacheStore(IRedisClientsManager redisManager) : IOutputCacheStore
{
public async ValueTask<byte[]?> GetAsync(string key, CancellationToken cancellationToken)
{
await using var redis = await redisManager.GetClientAsync(token: cancellationToken);
var value = await redis.GetAsync<byte[]>(key, cancellationToken);
return value;
}

public async ValueTask SetAsync(string key, byte[] value, string[]? tags, TimeSpan validFor, CancellationToken cancellationToken)
{
await using var redis = await redisManager.GetClientAsync(token: cancellationToken);

// First persist in normal cache hashset
await redis.SetAsync(key, value, validFor, cancellationToken);

if (tags == null)
return;
foreach (var tag in tags)
{
await redis.AddItemToSetAsync($"tag:{tag}", key, cancellationToken);
}
}

public async ValueTask EvictByTagAsync(string tag, CancellationToken cancellationToken)
{
await using var redis = await redisManager.GetClientAsync(token: cancellationToken);

var keys = await redis.GetAllItemsFromListAsync($"tag:{tag}", cancellationToken);

foreach (var key in keys)
{
await redis.RemoveEntryAsync(key);
await redis.RemoveItemFromSetAsync($"tag:{tag}", key, cancellationToken);
}
}
}
26 changes: 20 additions & 6 deletions BlazorOutputCaching/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
var services = builder.Services;
var config = builder.Configuration;

services.AddSingleton<IRedisClientsManager>(new BasicRedisClientManager());
services.AddSingleton<IOutputCacheStore, RedisOutputCacheStore>();
//services.AddSingleton<IRedisClientsManager>(new BasicRedisClientManager());
//services.AddSingleton<IOutputCacheStore, MemoryOutp>();

services.AddOutputCache();

Expand Down Expand Up @@ -94,12 +94,26 @@

app.UseServiceStack(new AppHost(), options => {
options.MapEndpoints();
options.RouteHandlerBuilders.Add((handlerBuilder, operation, verb, route) =>
options.RouteHandlerBuilders.Add((routeHandlerBuilder, operation, verb, route) =>
{
handlerBuilder.CacheOutput(policyBuilder =>
// Initialize appHost and allServiceTypes
var appHost = HostContext.AppHost;
// Find the service matching the RequestType of the operation
var operationType = operation.RequestType;
// Match with operation, verb and route
appHost.Metadata.OperationsMap.TryGetValue(operationType, out var operationMap);
var serviceType = operationMap?.ServiceType;
if (serviceType == null)
return;
if (serviceType.HasAttributeOf<OutputCacheAttribute>())
{
policyBuilder.Cache().Expire(TimeSpan.FromSeconds(15));
});
// Handle duration from OutputCacheAttribute
var outputCacheAttribute = serviceType.FirstAttribute<OutputCacheAttribute>();
routeHandlerBuilder.CacheOutput(policyBuilder =>
{
policyBuilder.Cache().Expire(TimeSpan.FromSeconds(outputCacheAttribute.Duration));
});
}
});
});

Expand Down

0 comments on commit 5cadf21

Please sign in to comment.