VOOZH about

URL: https://www.nuget.org/packages/Soenneker.SemanticKernel.Cache/

⇱ NuGet Gallery | Soenneker.SemanticKernel.Cache 4.0.771




👁 Image
Soenneker.SemanticKernel.Cache 4.0.771

Prefix Reserved
dotnet add package Soenneker.SemanticKernel.Cache --version 4.0.771
 
 
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 4.0.771
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.SemanticKernel.Cache" Version="4.0.771" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.SemanticKernel.Cache" Version="4.0.771" />
 
Directory.Packages.props
<PackageReference Include="Soenneker.SemanticKernel.Cache" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.SemanticKernel.Cache --version 4.0.771
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Soenneker.SemanticKernel.Cache, 4.0.771"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.SemanticKernel.Cache@4.0.771
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.SemanticKernel.Cache&version=4.0.771
 
Install as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=4.0.771
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

👁 alternate text is missing from this package README image
👁 alternate text is missing from this package README image
👁 alternate text is missing from this package README image
👁 alternate text is missing from this package README image

👁 alternate text is missing from this package README image
Soenneker.SemanticKernel.Cache

Providing async thread-safe singleton Semantic Kernel instances

Why?

When using Microsoft.SemanticKernel, it's important to centralize and reuse kernel setup logic rather than repeating configuration for each consumer or request. This avoids the overhead of reinitializing connectors and plugins. SemanticKernelCache supports this by providing a thread-safe, per-key singleton cache that lazily creates Kernel instances using customizable options. Kernels are disposed at application shutdown or manually if needed.

Installation

Install the package via the .NET CLI:

dotnet add package Soenneker.SemanticKernel.Cache

Usage

1. Register the Cache in Dependency Injection

In your Program.cs (or equivalent startup file), register the cache with the DI container:

using Soenneker.SemanticKernel.Cache;

public static async Task Main(string[] args)
{
 var builder = WebApplication.CreateBuilder(args);

 // Register SemanticKernelCache as a singleton service.
 builder.Services.AddSemanticKernelCacheAsSingleton();

 // Other configuration...
}

2. Inject and Retrieve a Kernel Instance

Inject ISemanticKernelCache into your classes and retrieve a Microsoft.SemanticKernel.Kernel instance by providing the required options.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Chat;
using Soenneker.SemanticKernel.Cache;

public class TestClass
{
 private readonly ISemanticKernelCache _semanticKernelCache;
 private readonly SemanticKernelOptions _options;

 public TestClass(ISemanticKernelCache semanticKernelCache)
 {
 _semanticKernelCache = semanticKernelCache;
 
 // Create the options object once. Replace these with your actual values.
 var options = new SemanticKernelOptions
 {
 ModelId = "deepseek-r1:32b",
 Endpoint = "http://localhost:11434",
 KernelFactory = (opts, ct) =>
 {
 IKernelBuilder builder = Kernel.CreateBuilder().AddOllamaChatCompletion(opts.ModelId, new Uri(opts.Endpoint));

 return ValueTask.FromResult(builder);
 }
 };
 }

 public async async ValueTask<string> GetKernelResponse(string input, CancellationToken cancellationToken = default)
 {
 // Retrieve (or create) the kernel instance using a key (here, nameof(TestClass)).
 Kernel kernel = await _semanticKernelCache.Get(nameof(TestClass), _options, cancellationToken);

 // Retrieve the chat completion service from the kernel.
 var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

 // Create a chat history and add the user's message.
 var history = new ChatHistory();
 history.AddUserMessage(input);

 // Request a chat completion using the chat service.
 var chatResult = await chatCompletionService.GetChatMessageContentAsync(history, kernel: kernel);

 // Return the chat result (or process it further as needed).
 return chatResult.ToString();
 }
}

Extending for Different Connectors/Plugins

The SemanticKernelOptions class includes an optional KernelFactory delegate. This allows you to override the default behavior (which uses the Azure Text Completion service) and create the kernel using a different connector or plugin. For example:

var openAiOptions = new SemanticKernelOptions
{
 ModelId = "openai-model-id",
 Endpoint = "https://api.openai.com/v1/",
 ApiKey = "your-openai-api-key",
 KernelFactory = (opts, ct) =>
 {
 Kernel kernel = new KernelBuilder().AddOpenAITextCompletionService(opts.ModelId, opts.Endpoint, opts.ApiKey);

 return ValueTask.FromResult(kernel);
 },
 ConfigureKernelAsync = async kernel =>
 {
 // Optionally, import skills or perform additional configuration.
 await ValueTask.CompletedTask;
 }
};

Kernel openAiKernel = await semanticKernelCache.Get("openaiKernel", openAiOptions);

This design makes it straightforward to support multiple types of Semantic Kernel configurations using the same caching mechanism.

Product Versions Compatible and additional computed target framework versions.
.NET net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Soenneker.SemanticKernel.Cache:

Package Downloads
Soenneker.SemanticKernel.Pool

Manages a pool of Semantic Kernel instances with per-entry rate limiting.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.771 0 6/18/2026
4.0.770 101 6/17/2026
4.0.769 360 6/10/2026
4.0.768 614 6/6/2026
4.0.767 250 6/6/2026
4.0.766 209 6/6/2026
4.0.765 209 6/6/2026
4.0.764 98 6/5/2026
4.0.763 101 6/5/2026
4.0.761 612 5/28/2026
4.0.760 137 5/28/2026
4.0.759 356 5/13/2026
4.0.758 472 5/11/2026
4.0.757 146 5/11/2026
4.0.756 351 5/2/2026
4.0.755 219 5/1/2026
4.0.754 205 4/29/2026
4.0.753 177 4/29/2026
4.0.752 334 4/24/2026
4.0.751 225 4/23/2026
Loading failed

Update actions/checkout action to v7 (#1360)