Soenneker.SemanticKernel.Cache 3.0.405

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.405
                    
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.405
                    
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.405" />
                    
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.405" />
                    
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.405
                    
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.405"
                    
#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.405
                    
#: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.405
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.405
                    
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 recommended to maintain long-lived kernel instances rather than re-creating them for each consumer or request. This avoids the overhead of reconfiguring connectors or plugins every time you need to perform a semantic operation. The SemanticKernelCache provides a thread-safe singleton cache per key via dependency injection. Kernel instances are created lazily using customizable options and disposed on 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.498 167 8/6/2025
3.0.497 167 8/5/2025
3.0.496 168 8/5/2025
3.0.495 168 8/5/2025
3.0.494 168 8/5/2025
3.0.493 86 7/30/2025
3.0.492 84 7/29/2025
3.0.491 437 7/24/2025
3.0.490 432 7/24/2025
3.0.489 342 7/9/2025
3.0.488 171 7/9/2025
3.0.487 147 7/9/2025
3.0.486 128 7/9/2025
3.0.485 180 7/8/2025
3.0.484 183 7/8/2025
3.0.483 357 7/4/2025
3.0.482 281 7/1/2025
3.0.481 130 7/1/2025
3.0.480 271 6/28/2025
3.0.479 81 6/28/2025
3.0.478 59 6/28/2025
3.0.477 144 6/28/2025
3.0.476 59 6/28/2025
3.0.475 158 6/28/2025
3.0.474 60 6/28/2025
3.0.473 58 6/28/2025
3.0.472 69 6/27/2025
3.0.471 67 6/27/2025
3.0.470 78 6/27/2025
3.0.469 285 6/26/2025
3.0.468 175 6/25/2025
3.0.467 201 6/25/2025
3.0.466 188 6/24/2025
3.0.465 332 6/16/2025
3.0.464 140 6/16/2025
3.0.463 364 6/11/2025
3.0.462 325 6/11/2025
3.0.461 347 6/11/2025
3.0.460 361 6/11/2025
3.0.459 278 6/11/2025
3.0.458 281 6/11/2025
3.0.457 276 6/11/2025
3.0.456 314 6/10/2025
3.0.455 405 6/3/2025
3.0.454 170 6/3/2025
3.0.453 316 6/3/2025
3.0.452 198 6/2/2025
3.0.451 190 6/2/2025
3.0.450 254 5/28/2025
3.0.449 193 5/28/2025
3.0.448 198 5/28/2025
3.0.447 143 5/28/2025
3.0.446 160 5/27/2025
3.0.445 141 5/27/2025
3.0.444 199 5/27/2025
3.0.443 144 5/27/2025
3.0.442 186 5/27/2025
3.0.441 137 5/27/2025
3.0.440 162 5/27/2025
3.0.439 300 5/26/2025
3.0.438 134 5/25/2025
3.0.437 138 5/25/2025
3.0.436 133 5/23/2025
3.0.435 150 5/23/2025
3.0.434 149 5/23/2025
3.0.433 115 5/23/2025
3.0.432 140 5/23/2025
3.0.431 119 5/23/2025
3.0.430 147 5/23/2025
3.0.429 167 5/23/2025
3.0.428 136 5/23/2025
3.0.427 145 5/22/2025
3.0.426 136 5/22/2025
3.0.425 169 5/22/2025
3.0.424 354 5/21/2025
3.0.423 169 5/21/2025
3.0.422 208 5/20/2025
3.0.421 141 5/20/2025
3.0.420 197 5/19/2025
3.0.419 308 5/18/2025
3.0.418 162 5/18/2025
3.0.417 161 5/18/2025
3.0.416 173 5/18/2025
3.0.414 101 5/18/2025
3.0.413 162 5/16/2025
3.0.412 185 5/16/2025
3.0.411 229 5/14/2025
3.0.410 225 5/14/2025
3.0.409 226 5/14/2025
3.0.408 225 5/14/2025
3.0.407 228 5/14/2025
3.0.406 140 5/8/2025
3.0.405 142 5/8/2025
3.0.404 144 5/8/2025
3.0.403 140 5/8/2025
3.0.402 139 5/8/2025
3.0.401 150 5/8/2025
3.0.400 146 5/8/2025
3.0.399 148 5/7/2025
3.0.398 147 5/6/2025
3.0.397 142 5/6/2025
3.0.396 142 5/6/2025
3.0.395 143 5/5/2025
3.0.394 151 5/5/2025
3.0.393 140 5/5/2025
3.0.392 147 5/5/2025
3.0.391 153 5/5/2025
3.0.390 141 5/5/2025
3.0.389 140 5/5/2025
3.0.388 141 5/5/2025
3.0.387 142 5/5/2025
3.0.386 143 5/5/2025
3.0.385 140 4/29/2025
3.0.384 137 4/27/2025
3.0.383 90 4/27/2025
3.0.382 88 4/26/2025
3.0.381 90 4/26/2025
3.0.380 178 4/18/2025
3.0.379 133 4/11/2025
3.0.378 169 4/9/2025
3.0.377 160 4/9/2025
3.0.376 179 4/9/2025
3.0.375 174 4/9/2025
3.0.374 166 4/8/2025
3.0.373 163 4/8/2025
3.0.372 162 4/8/2025
3.0.371 178 4/8/2025
3.0.370 168 4/8/2025
3.0.369 161 4/8/2025
3.0.368 166 4/8/2025
3.0.367 164 4/8/2025
3.0.366 158 4/8/2025
3.0.365 164 4/8/2025
3.0.364 168 4/8/2025
3.0.363 169 4/8/2025
3.0.362 163 4/8/2025
3.0.361 170 4/8/2025
3.0.360 170 4/8/2025
3.0.359 165 4/7/2025
3.0.358 155 4/7/2025
3.0.357 165 4/7/2025
3.0.356 168 4/7/2025
3.0.355 158 4/7/2025
3.0.354 161 4/7/2025
3.0.353 162 4/7/2025
3.0.352 166 4/7/2025
3.0.351 158 4/7/2025
3.0.350 165 4/7/2025
3.0.349 154 4/7/2025
3.0.348 166 4/7/2025
3.0.347 162 4/7/2025
3.0.346 160 4/7/2025
3.0.345 167 4/7/2025
3.0.344 165 4/7/2025
3.0.343 161 4/7/2025
3.0.342 170 4/6/2025
3.0.341 159 4/6/2025
3.0.340 162 4/6/2025
3.0.339 162 4/6/2025
3.0.338 159 4/6/2025
3.0.337 165 4/6/2025
3.0.336 166 4/6/2025
3.0.335 163 4/6/2025
3.0.334 142 4/6/2025
3.0.333 134 4/6/2025
3.0.332 137 4/6/2025
3.0.331 134 4/6/2025
3.0.330 146 4/6/2025
3.0.329 155 4/6/2025
3.0.328 107 4/6/2025
3.0.327 120 4/6/2025
3.0.326 106 4/6/2025
3.0.325 110 4/5/2025
3.0.324 124 4/5/2025
3.0.323 86 4/5/2025
3.0.322 84 4/5/2025
3.0.321 89 4/5/2025
3.0.320 92 4/5/2025
3.0.319 92 4/5/2025
3.0.318 93 4/5/2025
3.0.317 92 4/5/2025
3.0.316 103 4/4/2025
3.0.315 102 4/4/2025
3.0.314 100 4/4/2025
3.0.313 151 4/4/2025
3.0.312 154 4/4/2025
3.0.311 149 4/4/2025
3.0.310 171 4/4/2025
3.0.309 156 4/4/2025
3.0.308 163 4/3/2025
3.0.307 163 4/3/2025
3.0.306 157 4/2/2025
3.0.305 167 4/1/2025
3.0.304 161 4/1/2025
3.0.303 156 4/1/2025
3.0.302 156 4/1/2025
3.0.301 157 4/1/2025
3.0.300 151 4/1/2025
3.0.299 169 4/1/2025
3.0.298 159 4/1/2025
3.0.297 155 4/1/2025
3.0.296 146 4/1/2025
3.0.295 150 3/31/2025
3.0.294 160 3/31/2025
3.0.293 147 3/31/2025
3.0.292 168 3/31/2025
3.0.291 158 3/30/2025
3.0.290 156 3/29/2025
3.0.289 97 3/29/2025
3.0.288 97 3/29/2025
3.0.287 100 3/29/2025
3.0.286 93 3/29/2025
3.0.285 105 3/29/2025
3.0.284 136 3/27/2025
3.0.283 155 3/27/2025
3.0.282 138 3/27/2025
3.0.281 136 3/27/2025
3.0.280 141 3/26/2025
3.0.279 473 3/26/2025
3.0.278 473 3/26/2025
3.0.277 474 3/26/2025
3.0.276 479 3/25/2025
3.0.275 477 3/25/2025
3.0.274 473 3/25/2025
3.0.273 487 3/25/2025
3.0.272 479 3/25/2025
3.0.271 481 3/25/2025
3.0.270 492 3/25/2025
3.0.269 93 3/21/2025
3.0.268 86 3/21/2025
3.0.267 96 3/21/2025
3.0.266 113 3/21/2025
3.0.265 111 3/21/2025
3.0.264 141 3/21/2025
3.0.263 138 3/21/2025
3.0.262 148 3/20/2025
3.0.261 145 3/20/2025
3.0.260 143 3/19/2025
3.0.259 145 3/19/2025
3.0.258 142 3/18/2025
3.0.257 141 3/18/2025
3.0.256 140 3/18/2025
3.0.255 148 3/18/2025
3.0.254 147 3/18/2025
3.0.253 143 3/18/2025
3.0.252 145 3/18/2025
3.0.251 148 3/18/2025
3.0.250 90 3/15/2025
3.0.249 72 3/15/2025
3.0.248 77 3/15/2025
3.0.247 78 3/15/2025
3.0.246 72 3/15/2025
3.0.245 66 3/15/2025
3.0.244 156 3/12/2025
3.0.243 162 3/12/2025
3.0.242 161 3/12/2025
3.0.241 158 3/12/2025
3.0.240 147 3/12/2025
3.0.239 152 3/12/2025
3.0.238 156 3/12/2025
3.0.237 157 3/12/2025
3.0.236 156 3/12/2025
3.0.235 155 3/12/2025
3.0.234 153 3/12/2025
3.0.233 167 3/11/2025
3.0.232 162 3/11/2025
3.0.231 159 3/11/2025
3.0.230 165 3/11/2025
3.0.229 159 3/11/2025
3.0.228 166 3/11/2025
3.0.227 157 3/11/2025
3.0.226 157 3/11/2025
3.0.225 170 3/11/2025
3.0.224 164 3/11/2025
3.0.223 164 3/11/2025
3.0.222 165 3/11/2025
3.0.221 215 3/7/2025
3.0.220 209 3/7/2025
3.0.219 209 3/7/2025
3.0.218 225 3/7/2025
3.0.217 212 3/7/2025
3.0.216 215 3/7/2025
3.0.215 211 3/7/2025
3.0.214 213 3/7/2025
3.0.213 219 3/7/2025
3.0.212 214 3/3/2025
3.0.211 112 3/2/2025
3.0.210 114 3/2/2025
3.0.209 98 3/2/2025
3.0.208 102 3/2/2025
3.0.207 97 3/2/2025
3.0.206 98 3/2/2025
3.0.205 96 3/2/2025
3.0.204 115 3/2/2025
3.0.203 87 3/2/2025
3.0.202 94 3/2/2025
3.0.201 105 3/2/2025
3.0.200 98 3/2/2025
3.0.199 95 3/2/2025
3.0.198 102 3/1/2025
3.0.197 98 3/1/2025
3.0.196 100 3/1/2025
3.0.195 96 3/1/2025
3.0.194 96 3/1/2025
3.0.193 99 3/1/2025
3.0.192 100 3/1/2025
3.0.191 100 3/1/2025
3.0.190 89 3/1/2025
3.0.189 96 3/1/2025
3.0.188 104 3/1/2025
3.0.187 94 3/1/2025
3.0.186 100 2/28/2025
3.0.185 101 2/26/2025
3.0.184 104 2/26/2025
3.0.183 100 2/26/2025
3.0.182 106 2/26/2025
3.0.181 98 2/26/2025
3.0.180 106 2/25/2025
3.0.179 99 2/25/2025
3.0.178 106 2/25/2025
3.0.177 101 2/25/2025
3.0.176 108 2/25/2025
3.0.175 104 2/25/2025
3.0.174 97 2/25/2025
3.0.173 103 2/25/2025
3.0.172 101 2/25/2025
3.0.171 103 2/24/2025
3.0.170 104 2/24/2025
3.0.169 95 2/24/2025
3.0.168 132 2/23/2025
3.0.167 94 2/23/2025
3.0.166 97 2/23/2025
3.0.165 96 2/23/2025
3.0.164 103 2/23/2025
3.0.163 94 2/23/2025
3.0.162 100 2/23/2025
3.0.161 94 2/23/2025
3.0.160 103 2/22/2025
3.0.159 100 2/22/2025
3.0.158 103 2/22/2025
3.0.157 100 2/22/2025
3.0.156 95 2/22/2025
3.0.155 99 2/22/2025
3.0.154 93 2/22/2025
3.0.153 98 2/22/2025
3.0.152 104 2/22/2025
3.0.151 101 2/22/2025
3.0.150 102 2/22/2025
3.0.149 105 2/22/2025
3.0.148 106 2/22/2025
3.0.147 102 2/22/2025
3.0.146 106 2/22/2025
3.0.145 97 2/22/2025
3.0.144 101 2/22/2025
3.0.143 90 2/22/2025
3.0.142 103 2/22/2025
3.0.141 102 2/21/2025
3.0.140 97 2/21/2025
3.0.139 98 2/21/2025
3.0.138 100 2/21/2025
3.0.137 92 2/21/2025
3.0.136 102 2/21/2025
3.0.135 102 2/21/2025
3.0.134 104 2/20/2025
3.0.133 109 2/19/2025
3.0.132 109 2/19/2025
3.0.131 107 2/19/2025
3.0.130 105 2/19/2025
3.0.129 113 2/19/2025
3.0.128 111 2/19/2025
3.0.127 118 2/19/2025
3.0.126 103 2/19/2025
3.0.125 105 2/19/2025
3.0.124 107 2/19/2025
3.0.123 107 2/19/2025
3.0.122 108 2/18/2025
3.0.121 103 2/18/2025
3.0.120 113 2/18/2025
3.0.119 104 2/18/2025
3.0.118 113 2/18/2025
3.0.117 111 2/18/2025
3.0.116 124 2/18/2025
3.0.115 103 2/18/2025
3.0.114 109 2/16/2025
3.0.113 111 2/14/2025
3.0.112 101 2/14/2025
3.0.111 101 2/14/2025
3.0.110 103 2/14/2025
3.0.109 114 2/14/2025
3.0.108 121 2/14/2025
3.0.107 113 2/14/2025
3.0.106 125 2/14/2025
3.0.105 107 2/13/2025
3.0.104 103 2/13/2025
3.0.103 119 2/13/2025
3.0.102 96 2/13/2025
3.0.101 121 2/12/2025
3.0.100 111 2/12/2025
3.0.99 111 2/12/2025
3.0.98 118 2/12/2025
3.0.97 111 2/12/2025
3.0.96 116 2/12/2025
3.0.95 106 2/12/2025
3.0.94 112 2/12/2025
3.0.93 109 2/12/2025
3.0.92 110 2/12/2025
3.0.91 107 2/12/2025
3.0.90 111 2/12/2025
3.0.89 106 2/12/2025
3.0.88 108 2/12/2025
3.0.87 114 2/12/2025
3.0.86 108 2/12/2025
3.0.85 116 2/12/2025
3.0.84 112 2/12/2025
3.0.83 106 2/12/2025
3.0.82 104 2/11/2025
3.0.81 105 2/11/2025
3.0.80 111 2/11/2025
3.0.79 108 2/11/2025
3.0.78 115 2/11/2025
3.0.77 115 2/11/2025
3.0.76 104 2/11/2025
3.0.75 113 2/11/2025
3.0.74 114 2/11/2025
3.0.73 131 2/11/2025
3.0.72 108 2/11/2025
3.0.71 110 2/11/2025
3.0.70 112 2/10/2025
3.0.69 115 2/10/2025
3.0.68 119 2/10/2025
3.0.67 112 2/10/2025
3.0.66 106 2/10/2025
3.0.65 108 2/10/2025
3.0.64 111 2/9/2025
3.0.63 113 2/9/2025
3.0.62 96 2/9/2025
3.0.61 107 2/9/2025
3.0.60 105 2/9/2025
3.0.59 96 2/9/2025
3.0.58 112 2/8/2025
3.0.57 113 2/8/2025
3.0.56 100 2/8/2025
3.0.55 116 2/8/2025
3.0.54 106 2/8/2025
3.0.53 115 2/8/2025
3.0.52 107 2/8/2025
3.0.51 102 2/8/2025
3.0.50 108 2/8/2025
3.0.49 120 2/8/2025
3.0.48 108 2/8/2025
3.0.47 102 2/8/2025
3.0.46 116 2/7/2025
3.0.45 109 2/7/2025
3.0.44 119 2/7/2025
3.0.43 109 2/7/2025
3.0.42 108 2/7/2025
3.0.41 108 2/7/2025
3.0.40 124 2/7/2025
3.0.39 117 2/7/2025
3.0.38 118 2/7/2025
3.0.37 116 2/7/2025
3.0.36 108 2/7/2025
3.0.35 108 2/7/2025
3.0.34 103 2/7/2025
3.0.33 120 2/7/2025
3.0.32 113 2/7/2025
3.0.31 114 2/7/2025
3.0.30 106 2/6/2025
3.0.29 110 2/6/2025
3.0.28 99 2/6/2025
3.0.27 93 2/6/2025
3.0.26 113 2/6/2025
3.0.25 104 2/5/2025
3.0.24 110 2/5/2025
3.0.23 107 2/5/2025
3.0.22 115 2/5/2025
3.0.21 108 2/5/2025
3.0.20 110 2/5/2025
3.0.19 118 2/5/2025
3.0.18 107 2/5/2025
3.0.17 106 2/5/2025
3.0.16 114 2/5/2025
3.0.15 103 2/5/2025
3.0.14 113 2/5/2025
3.0.13 101 2/5/2025
3.0.12 109 2/5/2025
3.0.11 112 2/5/2025
3.0.10 113 2/5/2025
3.0.9 110 2/5/2025
3.0.8 104 2/5/2025
3.0.7 114 2/3/2025
3.0.6 115 2/3/2025
3.0.5 111 2/3/2025
3.0.4 117 2/3/2025
3.0.3 111 2/3/2025