gj.autofac.caching.redis
1.2.2
dotnet add package gj.autofac.caching.redis --version 1.2.2
NuGet\Install-Package gj.autofac.caching.redis -Version 1.2.2
<PackageReference Include="gj.autofac.caching.redis" Version="1.2.2" />
paket add gj.autofac.caching.redis --version 1.2.2
#r "nuget: gj.autofac.caching.redis, 1.2.2"
// Install gj.autofac.caching.redis as a Cake Addin #addin nuget:?package=gj.autofac.caching.redis&version=1.2.2 // Install gj.autofac.caching.redis as a Cake Tool #tool nuget:?package=gj.autofac.caching.redis&version=1.2.2
autofac.caching.redis
Automatic function level caching using redis in C#.
Usage
A full example can be found in the gj.autofac.caching.redis.tester directory.
using System.Diagnostics;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
namespace gj.autofac.caching.redis.tester;
static class Program
{
static void TimeAndRun(Action action, Microsoft.Extensions.Logging.ILogger logger)
{
var start = Stopwatch.StartNew();
action.Invoke();
start.Stop();
logger.LogInformation("Time taken: {0}ms", start.Elapsed.TotalMilliseconds);
}
static Task Main()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory) // Base directory for appsettings.json
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Verbose)
.CreateLogger();
var services = new ServiceCollection();
services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog(dispose: false);
});
var builder = new ContainerBuilder();
//Either make sure IConfiguration or RedisConfig is registered.
services.AddSingleton<IConfiguration>(configuration);
builder.Populate(services);
//Register an object Serializer to use
builder.RegisterType<MessagePackObjectSerializer>().As<IObjectSerializer>();
//Register the Redis Connection as single instance
builder.RegisterType<RedisConnectionManager>().SingleInstance();
builder.RegisterType<RedisCacheInterceptor>();
builder.RegisterType<ExampleService>()
.As<IExampleService>()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof(RedisCacheInterceptor));
var container = builder.Build();
var service = container.Resolve<IExampleService>();
var logger = container.Resolve<ILogger<ExampleService>>();
var id = 3;
for (var i = 0; i < 5; i++)
{
TimeAndRun(async () => await service.AsyncFunctionTest(id), logger);
}
return Task.CompletedTask;
}
}
License
MIT License
Copyright (c) 2024 Greg James
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Autofac.Extras.DynamicProxy (>= 7.1.0)
- MessagePack (>= 3.0.300)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Newtonsoft.Json (>= 13.0.3)
- StackExchange.Redis (>= 2.8.22)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.