Soenneker.SemanticKernel.Cache 3.0.477

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.477
                    
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.477
                    
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.477" />
                    
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.477" />
                    
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.477
                    
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.477"
                    
#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.477
                    
#: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.477
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.477
                    
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.498 89 8/6/2025
3.0.497 88 8/5/2025
3.0.496 91 8/5/2025
3.0.495 90 8/5/2025
3.0.494 90 8/5/2025
3.0.493 85 7/30/2025
3.0.492 83 7/29/2025
3.0.491 436 7/24/2025
3.0.490 431 7/24/2025
3.0.489 341 7/9/2025
3.0.488 170 7/9/2025
3.0.487 146 7/9/2025
3.0.486 127 7/9/2025
3.0.485 179 7/8/2025
3.0.484 182 7/8/2025
3.0.483 356 7/4/2025
3.0.482 280 7/1/2025
3.0.481 129 7/1/2025
3.0.480 270 6/28/2025
3.0.479 80 6/28/2025
3.0.478 58 6/28/2025
3.0.477 143 6/28/2025
3.0.476 58 6/28/2025
3.0.475 157 6/28/2025
3.0.474 59 6/28/2025
3.0.473 57 6/28/2025
3.0.472 68 6/27/2025
3.0.471 66 6/27/2025
3.0.470 77 6/27/2025
3.0.469 284 6/26/2025
3.0.468 174 6/25/2025
3.0.467 200 6/25/2025
3.0.466 187 6/24/2025
3.0.465 331 6/16/2025
3.0.464 139 6/16/2025
3.0.463 363 6/11/2025
3.0.462 324 6/11/2025
3.0.461 346 6/11/2025
3.0.460 360 6/11/2025
3.0.459 277 6/11/2025
3.0.458 280 6/11/2025
3.0.457 275 6/11/2025
3.0.456 313 6/10/2025
3.0.455 404 6/3/2025
3.0.454 169 6/3/2025
3.0.453 315 6/3/2025
3.0.452 197 6/2/2025
3.0.451 188 6/2/2025
3.0.450 253 5/28/2025
3.0.449 191 5/28/2025
3.0.448 197 5/28/2025
3.0.447 142 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 136 5/27/2025
3.0.440 161 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 113 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 165 5/23/2025
3.0.428 135 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 354 5/21/2025
3.0.423 169 5/21/2025
3.0.422 208 5/20/2025
3.0.421 140 5/20/2025
3.0.420 197 5/19/2025
3.0.419 307 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 224 5/14/2025
3.0.409 225 5/14/2025
3.0.408 224 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 138 5/8/2025
3.0.401 150 5/8/2025
3.0.400 146 5/8/2025
3.0.399 147 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 142 5/5/2025
3.0.394 150 5/5/2025
3.0.393 140 5/5/2025
3.0.392 146 5/5/2025
3.0.391 152 5/5/2025
3.0.390 141 5/5/2025
3.0.389 139 5/5/2025
3.0.388 141 5/5/2025
3.0.387 141 5/5/2025
3.0.386 143 5/5/2025
3.0.385 140 4/29/2025
3.0.384 136 4/27/2025
3.0.383 90 4/27/2025
3.0.382 87 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 158 4/9/2025
3.0.376 179 4/9/2025
3.0.375 173 4/9/2025
3.0.374 166 4/8/2025
3.0.373 163 4/8/2025
3.0.372 161 4/8/2025
3.0.371 177 4/8/2025
3.0.370 168 4/8/2025
3.0.369 160 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 169 4/8/2025
3.0.360 168 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 157 4/7/2025
3.0.354 160 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 164 4/7/2025
3.0.349 153 4/7/2025
3.0.348 166 4/7/2025
3.0.347 162 4/7/2025
3.0.346 159 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 164 4/6/2025
3.0.336 165 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 145 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 88 4/5/2025
3.0.320 91 4/5/2025
3.0.319 91 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 101 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 162 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 155 4/1/2025
3.0.301 156 4/1/2025
3.0.300 150 4/1/2025
3.0.299 168 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 149 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 157 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 92 3/29/2025
3.0.285 105 3/29/2025
3.0.284 135 3/27/2025
3.0.283 154 3/27/2025
3.0.282 137 3/27/2025
3.0.281 136 3/27/2025
3.0.280 140 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 472 3/25/2025
3.0.273 486 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 141 3/18/2025
3.0.257 141 3/18/2025
3.0.256 139 3/18/2025
3.0.255 147 3/18/2025
3.0.254 147 3/18/2025
3.0.253 142 3/18/2025
3.0.252 145 3/18/2025
3.0.251 148 3/18/2025
3.0.250 89 3/15/2025
3.0.249 72 3/15/2025
3.0.248 76 3/15/2025
3.0.247 78 3/15/2025
3.0.246 71 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 157 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 156 3/12/2025
3.0.236 155 3/12/2025
3.0.235 154 3/12/2025
3.0.234 153 3/12/2025
3.0.233 167 3/11/2025
3.0.232 161 3/11/2025
3.0.231 158 3/11/2025
3.0.230 164 3/11/2025
3.0.229 158 3/11/2025
3.0.228 165 3/11/2025
3.0.227 157 3/11/2025
3.0.226 157 3/11/2025
3.0.225 169 3/11/2025
3.0.224 163 3/11/2025
3.0.223 164 3/11/2025
3.0.222 164 3/11/2025
3.0.221 215 3/7/2025
3.0.220 209 3/7/2025
3.0.219 208 3/7/2025
3.0.218 225 3/7/2025
3.0.217 211 3/7/2025
3.0.216 215 3/7/2025
3.0.215 210 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 97 3/2/2025
3.0.205 95 3/2/2025
3.0.204 115 3/2/2025
3.0.203 86 3/2/2025
3.0.202 94 3/2/2025
3.0.201 105 3/2/2025
3.0.200 97 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 99 3/1/2025
3.0.195 95 3/1/2025
3.0.194 96 3/1/2025
3.0.193 99 3/1/2025
3.0.192 99 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 93 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 105 2/26/2025
3.0.181 98 2/26/2025
3.0.180 106 2/25/2025
3.0.179 98 2/25/2025
3.0.178 106 2/25/2025
3.0.177 100 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 102 2/25/2025
3.0.172 100 2/25/2025
3.0.171 103 2/24/2025
3.0.170 103 2/24/2025
3.0.169 94 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 102 2/23/2025
3.0.163 94 2/23/2025
3.0.162 100 2/23/2025
3.0.161 93 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 99 2/22/2025
3.0.156 94 2/22/2025
3.0.155 98 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 100 2/22/2025
3.0.150 101 2/22/2025
3.0.149 105 2/22/2025
3.0.148 105 2/22/2025
3.0.147 101 2/22/2025
3.0.146 106 2/22/2025
3.0.145 96 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 101 2/21/2025
3.0.140 97 2/21/2025
3.0.139 97 2/21/2025
3.0.138 99 2/21/2025
3.0.137 92 2/21/2025
3.0.136 101 2/21/2025
3.0.135 102 2/21/2025
3.0.134 104 2/20/2025
3.0.133 108 2/19/2025
3.0.132 109 2/19/2025
3.0.131 106 2/19/2025
3.0.130 104 2/19/2025
3.0.129 113 2/19/2025
3.0.128 111 2/19/2025
3.0.127 117 2/19/2025
3.0.126 102 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 112 2/18/2025
3.0.119 103 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 110 2/14/2025
3.0.112 100 2/14/2025
3.0.111 100 2/14/2025
3.0.110 103 2/14/2025
3.0.109 113 2/14/2025
3.0.108 120 2/14/2025
3.0.107 113 2/14/2025
3.0.106 124 2/14/2025
3.0.105 106 2/13/2025
3.0.104 101 2/13/2025
3.0.103 118 2/13/2025
3.0.102 96 2/13/2025
3.0.101 121 2/12/2025
3.0.100 110 2/12/2025
3.0.99 111 2/12/2025
3.0.98 117 2/12/2025
3.0.97 110 2/12/2025
3.0.96 115 2/12/2025
3.0.95 106 2/12/2025
3.0.94 112 2/12/2025
3.0.93 108 2/12/2025
3.0.92 110 2/12/2025
3.0.91 106 2/12/2025
3.0.90 111 2/12/2025
3.0.89 105 2/12/2025
3.0.88 107 2/12/2025
3.0.87 113 2/12/2025
3.0.86 108 2/12/2025
3.0.85 115 2/12/2025
3.0.84 111 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 107 2/11/2025
3.0.78 114 2/11/2025
3.0.77 115 2/11/2025
3.0.76 103 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 107 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 107 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 106 2/9/2025
3.0.60 105 2/9/2025
3.0.59 96 2/9/2025
3.0.58 111 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 105 2/8/2025
3.0.53 114 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 107 2/8/2025
3.0.47 101 2/8/2025
3.0.46 116 2/7/2025
3.0.45 108 2/7/2025
3.0.44 119 2/7/2025
3.0.43 109 2/7/2025
3.0.42 107 2/7/2025
3.0.41 107 2/7/2025
3.0.40 124 2/7/2025
3.0.39 117 2/7/2025
3.0.38 117 2/7/2025
3.0.37 115 2/7/2025
3.0.36 108 2/7/2025
3.0.35 108 2/7/2025
3.0.34 102 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 105 2/6/2025
3.0.29 110 2/6/2025
3.0.28 99 2/6/2025
3.0.27 92 2/6/2025
3.0.26 112 2/6/2025
3.0.25 104 2/5/2025
3.0.24 109 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 117 2/5/2025
3.0.18 107 2/5/2025
3.0.17 105 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 100 2/5/2025
3.0.12 109 2/5/2025
3.0.11 112 2/5/2025
3.0.10 112 2/5/2025
3.0.9 109 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 110 2/3/2025
3.0.4 117 2/3/2025
3.0.3 110 2/3/2025