Soenneker.SemanticKernel.Cache 3.0.445

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.SemanticKernel.Cache --version 3.0.445
                    
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.445
                    
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="3.0.445" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.SemanticKernel.Cache" Version="3.0.445" />
                    
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 3.0.445
                    
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.445"
                    
#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@3.0.445
                    
#: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=3.0.445
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.445
                    
Install as a Cake Tool

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 Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  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
3.0.489 337 7/9/2025
3.0.488 167 7/9/2025
3.0.487 143 7/9/2025
3.0.486 124 7/9/2025
3.0.485 176 7/8/2025
3.0.484 180 7/8/2025
3.0.483 354 7/4/2025
3.0.482 278 7/1/2025
3.0.481 127 7/1/2025
3.0.480 268 6/28/2025
3.0.479 78 6/28/2025
3.0.478 56 6/28/2025
3.0.477 141 6/28/2025
3.0.476 56 6/28/2025
3.0.475 154 6/28/2025
3.0.474 57 6/28/2025
3.0.473 55 6/28/2025
3.0.472 66 6/27/2025
3.0.471 64 6/27/2025
3.0.470 75 6/27/2025
3.0.469 282 6/26/2025
3.0.468 171 6/25/2025
3.0.467 198 6/25/2025
3.0.466 185 6/24/2025
3.0.465 330 6/16/2025
3.0.464 135 6/16/2025
3.0.463 362 6/11/2025
3.0.462 323 6/11/2025
3.0.461 345 6/11/2025
3.0.460 359 6/11/2025
3.0.459 276 6/11/2025
3.0.458 279 6/11/2025
3.0.457 274 6/11/2025
3.0.456 312 6/10/2025
3.0.455 401 6/3/2025
3.0.454 168 6/3/2025
3.0.453 313 6/3/2025
3.0.452 196 6/2/2025
3.0.451 187 6/2/2025
3.0.450 252 5/28/2025
3.0.449 191 5/28/2025
3.0.448 197 5/28/2025
3.0.447 141 5/28/2025
3.0.446 159 5/27/2025
3.0.445 140 5/27/2025
3.0.444 198 5/27/2025
3.0.443 143 5/27/2025
3.0.442 184 5/27/2025
3.0.441 135 5/27/2025
3.0.440 153 5/27/2025
3.0.439 299 5/26/2025
3.0.438 132 5/25/2025
3.0.437 136 5/25/2025
3.0.436 131 5/23/2025
3.0.435 148 5/23/2025
3.0.434 148 5/23/2025
3.0.433 111 5/23/2025
3.0.432 139 5/23/2025
3.0.431 118 5/23/2025
3.0.430 146 5/23/2025
3.0.429 164 5/23/2025
3.0.428 134 5/23/2025
3.0.427 144 5/22/2025
3.0.426 134 5/22/2025
3.0.425 167 5/22/2025
3.0.424 353 5/21/2025
3.0.423 167 5/21/2025
3.0.422 205 5/20/2025
3.0.421 139 5/20/2025
3.0.420 196 5/19/2025
3.0.419 306 5/18/2025
3.0.418 161 5/18/2025
3.0.417 160 5/18/2025
3.0.416 172 5/18/2025
3.0.414 100 5/18/2025
3.0.413 161 5/16/2025
3.0.412 184 5/16/2025
3.0.411 226 5/14/2025
3.0.410 223 5/14/2025
3.0.409 224 5/14/2025
3.0.408 223 5/14/2025
3.0.407 227 5/14/2025
3.0.406 139 5/8/2025
3.0.405 140 5/8/2025
3.0.404 142 5/8/2025
3.0.403 139 5/8/2025
3.0.402 137 5/8/2025
3.0.401 149 5/8/2025
3.0.400 144 5/8/2025
3.0.399 146 5/7/2025
3.0.398 146 5/6/2025
3.0.397 140 5/6/2025
3.0.396 141 5/6/2025
3.0.395 141 5/5/2025
3.0.394 147 5/5/2025
3.0.393 139 5/5/2025
3.0.392 144 5/5/2025
3.0.391 150 5/5/2025
3.0.390 140 5/5/2025
3.0.389 137 5/5/2025
3.0.388 140 5/5/2025
3.0.387 140 5/5/2025
3.0.386 142 5/5/2025
3.0.385 139 4/29/2025
3.0.384 135 4/27/2025
3.0.383 89 4/27/2025
3.0.382 85 4/26/2025
3.0.381 87 4/26/2025
3.0.380 175 4/18/2025
3.0.379 132 4/11/2025
3.0.378 168 4/9/2025
3.0.377 157 4/9/2025
3.0.376 178 4/9/2025
3.0.375 171 4/9/2025
3.0.374 165 4/8/2025
3.0.373 162 4/8/2025
3.0.372 160 4/8/2025
3.0.371 175 4/8/2025
3.0.370 167 4/8/2025
3.0.369 159 4/8/2025
3.0.368 165 4/8/2025
3.0.367 163 4/8/2025
3.0.366 157 4/8/2025
3.0.365 160 4/8/2025
3.0.364 166 4/8/2025
3.0.363 168 4/8/2025
3.0.362 162 4/8/2025
3.0.361 168 4/8/2025
3.0.360 167 4/8/2025
3.0.359 164 4/7/2025
3.0.358 154 4/7/2025
3.0.357 164 4/7/2025
3.0.356 165 4/7/2025
3.0.355 156 4/7/2025
3.0.354 159 4/7/2025
3.0.353 161 4/7/2025
3.0.352 164 4/7/2025
3.0.351 156 4/7/2025
3.0.350 163 4/7/2025
3.0.349 152 4/7/2025
3.0.348 165 4/7/2025
3.0.347 160 4/7/2025
3.0.346 158 4/7/2025
3.0.345 166 4/7/2025
3.0.344 163 4/7/2025
3.0.343 159 4/7/2025
3.0.342 168 4/6/2025
3.0.341 158 4/6/2025
3.0.340 161 4/6/2025
3.0.339 160 4/6/2025
3.0.338 158 4/6/2025
3.0.337 163 4/6/2025
3.0.336 164 4/6/2025
3.0.335 160 4/6/2025
3.0.334 139 4/6/2025
3.0.333 133 4/6/2025
3.0.332 136 4/6/2025
3.0.331 132 4/6/2025
3.0.330 144 4/6/2025
3.0.329 145 4/6/2025
3.0.328 106 4/6/2025
3.0.327 118 4/6/2025
3.0.326 104 4/6/2025
3.0.325 109 4/5/2025
3.0.324 121 4/5/2025
3.0.323 85 4/5/2025
3.0.322 83 4/5/2025
3.0.321 87 4/5/2025
3.0.320 90 4/5/2025
3.0.319 90 4/5/2025
3.0.318 92 4/5/2025
3.0.317 91 4/5/2025
3.0.316 102 4/4/2025
3.0.315 98 4/4/2025
3.0.314 99 4/4/2025
3.0.313 148 4/4/2025
3.0.312 153 4/4/2025
3.0.311 146 4/4/2025
3.0.310 169 4/4/2025
3.0.309 154 4/4/2025
3.0.308 162 4/3/2025
3.0.307 161 4/3/2025
3.0.306 156 4/2/2025
3.0.305 163 4/1/2025
3.0.304 160 4/1/2025
3.0.303 155 4/1/2025
3.0.302 153 4/1/2025
3.0.301 155 4/1/2025
3.0.300 149 4/1/2025
3.0.299 166 4/1/2025
3.0.298 157 4/1/2025
3.0.297 154 4/1/2025
3.0.296 145 4/1/2025
3.0.295 148 3/31/2025
3.0.294 160 3/31/2025
3.0.293 146 3/31/2025
3.0.292 166 3/31/2025
3.0.291 155 3/30/2025
3.0.290 154 3/29/2025
3.0.289 94 3/29/2025
3.0.288 93 3/29/2025
3.0.287 99 3/29/2025
3.0.286 91 3/29/2025
3.0.285 104 3/29/2025
3.0.284 134 3/27/2025
3.0.283 152 3/27/2025
3.0.282 136 3/27/2025
3.0.281 134 3/27/2025
3.0.280 139 3/26/2025
3.0.279 472 3/26/2025
3.0.278 471 3/26/2025
3.0.277 473 3/26/2025
3.0.276 477 3/25/2025
3.0.275 476 3/25/2025
3.0.274 471 3/25/2025
3.0.273 483 3/25/2025
3.0.272 478 3/25/2025
3.0.271 480 3/25/2025
3.0.270 491 3/25/2025
3.0.269 92 3/21/2025
3.0.268 85 3/21/2025
3.0.267 95 3/21/2025
3.0.266 111 3/21/2025
3.0.265 110 3/21/2025
3.0.264 139 3/21/2025
3.0.263 136 3/21/2025
3.0.262 147 3/20/2025
3.0.261 144 3/20/2025
3.0.260 142 3/19/2025
3.0.259 144 3/19/2025
3.0.258 140 3/18/2025
3.0.257 140 3/18/2025
3.0.256 138 3/18/2025
3.0.255 146 3/18/2025
3.0.254 146 3/18/2025
3.0.253 141 3/18/2025
3.0.252 141 3/18/2025
3.0.251 147 3/18/2025
3.0.250 88 3/15/2025
3.0.249 71 3/15/2025
3.0.248 74 3/15/2025
3.0.247 76 3/15/2025
3.0.246 70 3/15/2025
3.0.245 65 3/15/2025
3.0.244 155 3/12/2025
3.0.243 161 3/12/2025
3.0.242 160 3/12/2025
3.0.241 156 3/12/2025
3.0.240 146 3/12/2025
3.0.239 150 3/12/2025
3.0.238 155 3/12/2025
3.0.237 155 3/12/2025
3.0.236 154 3/12/2025
3.0.235 153 3/12/2025
3.0.234 152 3/12/2025
3.0.233 164 3/11/2025
3.0.232 160 3/11/2025
3.0.231 157 3/11/2025
3.0.230 163 3/11/2025
3.0.229 157 3/11/2025
3.0.228 163 3/11/2025
3.0.227 156 3/11/2025
3.0.226 156 3/11/2025
3.0.225 168 3/11/2025
3.0.224 162 3/11/2025
3.0.223 163 3/11/2025
3.0.222 163 3/11/2025
3.0.221 213 3/7/2025
3.0.220 208 3/7/2025
3.0.219 207 3/7/2025
3.0.218 224 3/7/2025
3.0.217 210 3/7/2025
3.0.216 214 3/7/2025
3.0.215 208 3/7/2025
3.0.214 212 3/7/2025
3.0.213 218 3/7/2025
3.0.212 213 3/3/2025
3.0.211 110 3/2/2025
3.0.210 113 3/2/2025
3.0.209 97 3/2/2025
3.0.208 101 3/2/2025
3.0.207 96 3/2/2025
3.0.206 96 3/2/2025
3.0.205 94 3/2/2025
3.0.204 114 3/2/2025
3.0.203 84 3/2/2025
3.0.202 93 3/2/2025
3.0.201 103 3/2/2025
3.0.200 96 3/2/2025
3.0.199 94 3/2/2025
3.0.198 101 3/1/2025
3.0.197 97 3/1/2025
3.0.196 98 3/1/2025
3.0.195 94 3/1/2025
3.0.194 94 3/1/2025
3.0.193 98 3/1/2025
3.0.192 98 3/1/2025
3.0.191 97 3/1/2025
3.0.190 88 3/1/2025
3.0.189 95 3/1/2025
3.0.188 103 3/1/2025
3.0.187 92 3/1/2025
3.0.186 99 2/28/2025
3.0.185 100 2/26/2025
3.0.184 102 2/26/2025
3.0.183 98 2/26/2025
3.0.182 104 2/26/2025
3.0.181 97 2/26/2025
3.0.180 103 2/25/2025
3.0.179 97 2/25/2025
3.0.178 104 2/25/2025
3.0.177 99 2/25/2025
3.0.176 107 2/25/2025
3.0.175 103 2/25/2025
3.0.174 96 2/25/2025
3.0.173 101 2/25/2025
3.0.172 99 2/25/2025
3.0.171 100 2/24/2025
3.0.170 102 2/24/2025
3.0.169 92 2/24/2025
3.0.168 131 2/23/2025
3.0.167 92 2/23/2025
3.0.166 96 2/23/2025
3.0.165 95 2/23/2025
3.0.164 99 2/23/2025
3.0.163 93 2/23/2025
3.0.162 99 2/23/2025
3.0.161 92 2/23/2025
3.0.160 102 2/22/2025
3.0.159 98 2/22/2025
3.0.158 100 2/22/2025
3.0.157 97 2/22/2025
3.0.156 93 2/22/2025
3.0.155 97 2/22/2025
3.0.154 92 2/22/2025
3.0.153 97 2/22/2025
3.0.152 103 2/22/2025
3.0.151 98 2/22/2025
3.0.150 100 2/22/2025
3.0.149 103 2/22/2025
3.0.148 104 2/22/2025
3.0.147 100 2/22/2025
3.0.146 105 2/22/2025
3.0.145 95 2/22/2025
3.0.144 100 2/22/2025
3.0.143 89 2/22/2025
3.0.142 101 2/22/2025
3.0.141 100 2/21/2025
3.0.140 96 2/21/2025
3.0.139 96 2/21/2025
3.0.138 98 2/21/2025
3.0.137 91 2/21/2025
3.0.136 100 2/21/2025
3.0.135 101 2/21/2025
3.0.134 103 2/20/2025
3.0.133 105 2/19/2025
3.0.132 106 2/19/2025
3.0.131 105 2/19/2025
3.0.130 102 2/19/2025
3.0.129 112 2/19/2025
3.0.128 110 2/19/2025
3.0.127 114 2/19/2025
3.0.126 101 2/19/2025
3.0.125 104 2/19/2025
3.0.124 105 2/19/2025
3.0.123 106 2/19/2025
3.0.122 106 2/18/2025
3.0.121 102 2/18/2025
3.0.120 111 2/18/2025
3.0.119 102 2/18/2025
3.0.118 109 2/18/2025
3.0.117 110 2/18/2025
3.0.116 123 2/18/2025
3.0.115 102 2/18/2025
3.0.114 108 2/16/2025
3.0.113 109 2/14/2025
3.0.112 98 2/14/2025
3.0.111 98 2/14/2025
3.0.110 101 2/14/2025
3.0.109 109 2/14/2025
3.0.108 118 2/14/2025
3.0.107 111 2/14/2025
3.0.106 122 2/14/2025
3.0.105 104 2/13/2025
3.0.104 99 2/13/2025
3.0.103 116 2/13/2025
3.0.102 94 2/13/2025
3.0.101 117 2/12/2025
3.0.100 107 2/12/2025
3.0.99 109 2/12/2025
3.0.98 115 2/12/2025
3.0.97 109 2/12/2025
3.0.96 114 2/12/2025
3.0.95 105 2/12/2025
3.0.94 110 2/12/2025
3.0.93 107 2/12/2025
3.0.92 109 2/12/2025
3.0.91 105 2/12/2025
3.0.90 108 2/12/2025
3.0.89 104 2/12/2025
3.0.88 106 2/12/2025
3.0.87 112 2/12/2025
3.0.86 106 2/12/2025
3.0.85 114 2/12/2025
3.0.84 110 2/12/2025
3.0.83 104 2/12/2025
3.0.82 102 2/11/2025
3.0.81 103 2/11/2025
3.0.80 110 2/11/2025
3.0.79 105 2/11/2025
3.0.78 113 2/11/2025
3.0.77 114 2/11/2025
3.0.76 102 2/11/2025
3.0.75 112 2/11/2025
3.0.74 112 2/11/2025
3.0.73 129 2/11/2025
3.0.72 106 2/11/2025
3.0.71 109 2/11/2025
3.0.70 111 2/10/2025
3.0.69 113 2/10/2025
3.0.68 117 2/10/2025
3.0.67 111 2/10/2025
3.0.66 105 2/10/2025
3.0.65 106 2/10/2025
3.0.64 109 2/9/2025
3.0.63 112 2/9/2025
3.0.62 94 2/9/2025
3.0.61 105 2/9/2025
3.0.60 104 2/9/2025
3.0.59 95 2/9/2025
3.0.58 109 2/8/2025
3.0.57 111 2/8/2025
3.0.56 99 2/8/2025
3.0.55 115 2/8/2025
3.0.54 104 2/8/2025
3.0.53 112 2/8/2025
3.0.52 105 2/8/2025
3.0.51 101 2/8/2025
3.0.50 106 2/8/2025
3.0.49 118 2/8/2025
3.0.48 106 2/8/2025
3.0.47 99 2/8/2025
3.0.46 114 2/7/2025
3.0.45 107 2/7/2025
3.0.44 117 2/7/2025
3.0.43 108 2/7/2025
3.0.42 106 2/7/2025
3.0.41 106 2/7/2025
3.0.40 123 2/7/2025
3.0.39 116 2/7/2025
3.0.38 115 2/7/2025
3.0.37 113 2/7/2025
3.0.36 107 2/7/2025
3.0.35 106 2/7/2025
3.0.34 101 2/7/2025
3.0.33 119 2/7/2025
3.0.32 111 2/7/2025
3.0.31 112 2/7/2025
3.0.30 104 2/6/2025
3.0.29 109 2/6/2025
3.0.28 98 2/6/2025
3.0.27 91 2/6/2025
3.0.26 111 2/6/2025
3.0.25 103 2/5/2025
3.0.24 107 2/5/2025
3.0.23 106 2/5/2025
3.0.22 113 2/5/2025
3.0.21 107 2/5/2025
3.0.20 108 2/5/2025
3.0.19 116 2/5/2025
3.0.18 106 2/5/2025
3.0.17 104 2/5/2025
3.0.16 112 2/5/2025
3.0.15 102 2/5/2025
3.0.14 112 2/5/2025
3.0.13 99 2/5/2025
3.0.12 108 2/5/2025
3.0.11 111 2/5/2025
3.0.10 111 2/5/2025
3.0.9 107 2/5/2025
3.0.8 103 2/5/2025
3.0.7 113 2/3/2025
3.0.6 112 2/3/2025
3.0.5 109 2/3/2025
3.0.4 113 2/3/2025
3.0.3 109 2/3/2025