Soenneker.SemanticKernel.Cache 3.0.472

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.472
                    
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.472
                    
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.472" />
                    
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.472" />
                    
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.472
                    
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.472"
                    
#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.
#addin nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.472
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.472
                    
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.483 109 7/4/2025
3.0.482 234 7/1/2025
3.0.481 104 7/1/2025
3.0.480 258 6/28/2025
3.0.479 72 6/28/2025
3.0.478 48 6/28/2025
3.0.477 132 6/28/2025
3.0.476 50 6/28/2025
3.0.475 148 6/28/2025
3.0.474 51 6/28/2025
3.0.473 48 6/28/2025
3.0.472 60 6/27/2025
3.0.471 58 6/27/2025
3.0.470 68 6/27/2025
3.0.469 278 6/26/2025
3.0.468 167 6/25/2025
3.0.467 189 6/25/2025
3.0.466 175 6/24/2025
3.0.465 323 6/16/2025
3.0.464 130 6/16/2025
3.0.463 359 6/11/2025
3.0.462 320 6/11/2025
3.0.461 340 6/11/2025
3.0.460 356 6/11/2025
3.0.459 273 6/11/2025
3.0.458 276 6/11/2025
3.0.457 271 6/11/2025
3.0.456 309 6/10/2025
3.0.455 396 6/3/2025
3.0.454 165 6/3/2025
3.0.453 307 6/3/2025
3.0.452 191 6/2/2025
3.0.451 184 6/2/2025
3.0.450 247 5/28/2025
3.0.449 187 5/28/2025
3.0.448 190 5/28/2025
3.0.447 137 5/28/2025
3.0.446 155 5/27/2025
3.0.445 136 5/27/2025
3.0.444 193 5/27/2025
3.0.443 139 5/27/2025
3.0.442 181 5/27/2025
3.0.441 132 5/27/2025
3.0.440 150 5/27/2025
3.0.439 296 5/26/2025
3.0.438 129 5/25/2025
3.0.437 133 5/25/2025
3.0.436 127 5/23/2025
3.0.435 145 5/23/2025
3.0.434 145 5/23/2025
3.0.433 104 5/23/2025
3.0.432 136 5/23/2025
3.0.431 115 5/23/2025
3.0.430 140 5/23/2025
3.0.429 157 5/23/2025
3.0.428 127 5/23/2025
3.0.427 141 5/22/2025
3.0.426 131 5/22/2025
3.0.425 164 5/22/2025
3.0.424 350 5/21/2025
3.0.423 163 5/21/2025
3.0.422 197 5/20/2025
3.0.421 132 5/20/2025
3.0.420 193 5/19/2025
3.0.419 303 5/18/2025
3.0.418 154 5/18/2025
3.0.417 157 5/18/2025
3.0.416 169 5/18/2025
3.0.414 97 5/18/2025
3.0.413 155 5/16/2025
3.0.412 181 5/16/2025
3.0.411 220 5/14/2025
3.0.410 220 5/14/2025
3.0.409 219 5/14/2025
3.0.408 219 5/14/2025
3.0.407 224 5/14/2025
3.0.406 137 5/8/2025
3.0.405 136 5/8/2025
3.0.404 139 5/8/2025
3.0.403 135 5/8/2025
3.0.402 134 5/8/2025
3.0.401 142 5/8/2025
3.0.400 138 5/8/2025
3.0.399 141 5/7/2025
3.0.398 139 5/6/2025
3.0.397 137 5/6/2025
3.0.396 138 5/6/2025
3.0.395 138 5/5/2025
3.0.394 143 5/5/2025
3.0.393 136 5/5/2025
3.0.392 141 5/5/2025
3.0.391 147 5/5/2025
3.0.390 137 5/5/2025
3.0.389 132 5/5/2025
3.0.388 137 5/5/2025
3.0.387 137 5/5/2025
3.0.386 139 5/5/2025
3.0.385 136 4/29/2025
3.0.384 131 4/27/2025
3.0.383 85 4/27/2025
3.0.382 81 4/26/2025
3.0.381 81 4/26/2025
3.0.380 170 4/18/2025
3.0.379 130 4/11/2025
3.0.378 165 4/9/2025
3.0.377 154 4/9/2025
3.0.376 171 4/9/2025
3.0.375 168 4/9/2025
3.0.374 162 4/8/2025
3.0.373 160 4/8/2025
3.0.372 158 4/8/2025
3.0.371 166 4/8/2025
3.0.370 164 4/8/2025
3.0.369 156 4/8/2025
3.0.368 161 4/8/2025
3.0.367 160 4/8/2025
3.0.366 154 4/8/2025
3.0.365 155 4/8/2025
3.0.364 155 4/8/2025
3.0.363 164 4/8/2025
3.0.362 159 4/8/2025
3.0.361 163 4/8/2025
3.0.360 164 4/8/2025
3.0.359 161 4/7/2025
3.0.358 151 4/7/2025
3.0.357 161 4/7/2025
3.0.356 159 4/7/2025
3.0.355 152 4/7/2025
3.0.354 156 4/7/2025
3.0.353 152 4/7/2025
3.0.352 161 4/7/2025
3.0.351 153 4/7/2025
3.0.350 157 4/7/2025
3.0.349 149 4/7/2025
3.0.348 162 4/7/2025
3.0.347 156 4/7/2025
3.0.346 155 4/7/2025
3.0.345 161 4/7/2025
3.0.344 158 4/7/2025
3.0.343 156 4/7/2025
3.0.342 163 4/6/2025
3.0.341 155 4/6/2025
3.0.340 157 4/6/2025
3.0.339 157 4/6/2025
3.0.338 155 4/6/2025
3.0.337 158 4/6/2025
3.0.336 161 4/6/2025
3.0.335 156 4/6/2025
3.0.334 131 4/6/2025
3.0.333 130 4/6/2025
3.0.332 133 4/6/2025
3.0.331 128 4/6/2025
3.0.330 140 4/6/2025
3.0.329 137 4/6/2025
3.0.328 103 4/6/2025
3.0.327 111 4/6/2025
3.0.326 100 4/6/2025
3.0.325 105 4/5/2025
3.0.324 116 4/5/2025
3.0.323 82 4/5/2025
3.0.322 80 4/5/2025
3.0.321 84 4/5/2025
3.0.320 86 4/5/2025
3.0.319 87 4/5/2025
3.0.318 88 4/5/2025
3.0.317 87 4/5/2025
3.0.316 99 4/4/2025
3.0.315 95 4/4/2025
3.0.314 96 4/4/2025
3.0.313 145 4/4/2025
3.0.312 146 4/4/2025
3.0.311 143 4/4/2025
3.0.310 165 4/4/2025
3.0.309 151 4/4/2025
3.0.308 157 4/3/2025
3.0.307 158 4/3/2025
3.0.306 150 4/2/2025
3.0.305 156 4/1/2025
3.0.304 157 4/1/2025
3.0.303 151 4/1/2025
3.0.302 146 4/1/2025
3.0.301 152 4/1/2025
3.0.300 146 4/1/2025
3.0.299 160 4/1/2025
3.0.298 154 4/1/2025
3.0.297 150 4/1/2025
3.0.296 142 4/1/2025
3.0.295 145 3/31/2025
3.0.294 156 3/31/2025
3.0.293 142 3/31/2025
3.0.292 162 3/31/2025
3.0.291 152 3/30/2025
3.0.290 150 3/29/2025
3.0.289 88 3/29/2025
3.0.288 88 3/29/2025
3.0.287 95 3/29/2025
3.0.286 88 3/29/2025
3.0.285 97 3/29/2025
3.0.284 131 3/27/2025
3.0.283 143 3/27/2025
3.0.282 133 3/27/2025
3.0.281 131 3/27/2025
3.0.280 136 3/26/2025
3.0.279 469 3/26/2025
3.0.278 468 3/26/2025
3.0.277 470 3/26/2025
3.0.276 472 3/25/2025
3.0.275 472 3/25/2025
3.0.274 467 3/25/2025
3.0.273 476 3/25/2025
3.0.272 475 3/25/2025
3.0.271 476 3/25/2025
3.0.270 488 3/25/2025
3.0.269 89 3/21/2025
3.0.268 82 3/21/2025
3.0.267 92 3/21/2025
3.0.266 108 3/21/2025
3.0.265 106 3/21/2025
3.0.264 134 3/21/2025
3.0.263 133 3/21/2025
3.0.262 143 3/20/2025
3.0.261 141 3/20/2025
3.0.260 138 3/19/2025
3.0.259 141 3/19/2025
3.0.258 137 3/18/2025
3.0.257 137 3/18/2025
3.0.256 135 3/18/2025
3.0.255 143 3/18/2025
3.0.254 143 3/18/2025
3.0.253 138 3/18/2025
3.0.252 138 3/18/2025
3.0.251 144 3/18/2025
3.0.250 82 3/15/2025
3.0.249 67 3/15/2025
3.0.248 69 3/15/2025
3.0.247 70 3/15/2025
3.0.246 67 3/15/2025
3.0.245 62 3/15/2025
3.0.244 152 3/12/2025
3.0.243 157 3/12/2025
3.0.242 157 3/12/2025
3.0.241 152 3/12/2025
3.0.240 143 3/12/2025
3.0.239 147 3/12/2025
3.0.238 152 3/12/2025
3.0.237 152 3/12/2025
3.0.236 151 3/12/2025
3.0.235 150 3/12/2025
3.0.234 149 3/12/2025
3.0.233 160 3/11/2025
3.0.232 157 3/11/2025
3.0.231 154 3/11/2025
3.0.230 159 3/11/2025
3.0.229 154 3/11/2025
3.0.228 160 3/11/2025
3.0.227 153 3/11/2025
3.0.226 153 3/11/2025
3.0.225 161 3/11/2025
3.0.224 154 3/11/2025
3.0.223 160 3/11/2025
3.0.222 160 3/11/2025
3.0.221 210 3/7/2025
3.0.220 205 3/7/2025
3.0.219 200 3/7/2025
3.0.218 216 3/7/2025
3.0.217 207 3/7/2025
3.0.216 207 3/7/2025
3.0.215 205 3/7/2025
3.0.214 205 3/7/2025
3.0.213 208 3/7/2025
3.0.212 209 3/3/2025
3.0.211 107 3/2/2025
3.0.210 110 3/2/2025
3.0.209 94 3/2/2025
3.0.208 94 3/2/2025
3.0.207 93 3/2/2025
3.0.206 92 3/2/2025
3.0.205 91 3/2/2025
3.0.204 107 3/2/2025
3.0.203 81 3/2/2025
3.0.202 90 3/2/2025
3.0.201 98 3/2/2025
3.0.200 92 3/2/2025
3.0.199 91 3/2/2025
3.0.198 97 3/1/2025
3.0.197 94 3/1/2025
3.0.196 95 3/1/2025
3.0.195 91 3/1/2025
3.0.194 91 3/1/2025
3.0.193 95 3/1/2025
3.0.192 92 3/1/2025
3.0.191 92 3/1/2025
3.0.190 85 3/1/2025
3.0.189 87 3/1/2025
3.0.188 93 3/1/2025
3.0.187 89 3/1/2025
3.0.186 97 2/28/2025
3.0.185 97 2/26/2025
3.0.184 96 2/26/2025
3.0.183 95 2/26/2025
3.0.182 100 2/26/2025
3.0.181 94 2/26/2025
3.0.180 98 2/25/2025
3.0.179 94 2/25/2025
3.0.178 101 2/25/2025
3.0.177 96 2/25/2025
3.0.176 102 2/25/2025
3.0.175 99 2/25/2025
3.0.174 93 2/25/2025
3.0.173 97 2/25/2025
3.0.172 96 2/25/2025
3.0.171 95 2/24/2025
3.0.170 99 2/24/2025
3.0.169 89 2/24/2025
3.0.168 128 2/23/2025
3.0.167 85 2/23/2025
3.0.166 93 2/23/2025
3.0.165 91 2/23/2025
3.0.164 93 2/23/2025
3.0.163 90 2/23/2025
3.0.162 96 2/23/2025
3.0.161 89 2/23/2025
3.0.160 96 2/22/2025
3.0.159 89 2/22/2025
3.0.158 94 2/22/2025
3.0.157 94 2/22/2025
3.0.156 90 2/22/2025
3.0.155 94 2/22/2025
3.0.154 89 2/22/2025
3.0.153 94 2/22/2025
3.0.152 100 2/22/2025
3.0.151 91 2/22/2025
3.0.150 97 2/22/2025
3.0.149 95 2/22/2025
3.0.148 101 2/22/2025
3.0.147 93 2/22/2025
3.0.146 100 2/22/2025
3.0.145 88 2/22/2025
3.0.144 96 2/22/2025
3.0.143 86 2/22/2025
3.0.142 94 2/22/2025
3.0.141 94 2/21/2025
3.0.140 93 2/21/2025
3.0.139 93 2/21/2025
3.0.138 95 2/21/2025
3.0.137 88 2/21/2025
3.0.136 95 2/21/2025
3.0.135 95 2/21/2025
3.0.134 100 2/20/2025
3.0.133 98 2/19/2025
3.0.132 101 2/19/2025
3.0.131 101 2/19/2025
3.0.130 97 2/19/2025
3.0.129 108 2/19/2025
3.0.128 107 2/19/2025
3.0.127 109 2/19/2025
3.0.126 98 2/19/2025
3.0.125 101 2/19/2025
3.0.124 98 2/19/2025
3.0.123 103 2/19/2025
3.0.122 100 2/18/2025
3.0.121 99 2/18/2025
3.0.120 108 2/18/2025
3.0.119 99 2/18/2025
3.0.118 104 2/18/2025
3.0.117 103 2/18/2025
3.0.116 120 2/18/2025
3.0.115 99 2/18/2025
3.0.114 105 2/16/2025
3.0.113 102 2/14/2025
3.0.112 95 2/14/2025
3.0.111 95 2/14/2025
3.0.110 98 2/14/2025
3.0.109 104 2/14/2025
3.0.108 112 2/14/2025
3.0.107 106 2/14/2025
3.0.106 117 2/14/2025
3.0.105 101 2/13/2025
3.0.104 96 2/13/2025
3.0.103 113 2/13/2025
3.0.102 91 2/13/2025
3.0.101 113 2/12/2025
3.0.100 101 2/12/2025
3.0.99 103 2/12/2025
3.0.98 112 2/12/2025
3.0.97 104 2/12/2025
3.0.96 108 2/12/2025
3.0.95 101 2/12/2025
3.0.94 103 2/12/2025
3.0.93 102 2/12/2025
3.0.92 106 2/12/2025
3.0.91 102 2/12/2025
3.0.90 104 2/12/2025
3.0.89 100 2/12/2025
3.0.88 103 2/12/2025
3.0.87 109 2/12/2025
3.0.86 100 2/12/2025
3.0.85 111 2/12/2025
3.0.84 107 2/12/2025
3.0.83 98 2/12/2025
3.0.82 99 2/11/2025
3.0.81 100 2/11/2025
3.0.80 105 2/11/2025
3.0.79 102 2/11/2025
3.0.78 110 2/11/2025
3.0.77 107 2/11/2025
3.0.76 99 2/11/2025
3.0.75 108 2/11/2025
3.0.74 106 2/11/2025
3.0.73 121 2/11/2025
3.0.72 103 2/11/2025
3.0.71 106 2/11/2025
3.0.70 106 2/10/2025
3.0.69 110 2/10/2025
3.0.68 113 2/10/2025
3.0.67 108 2/10/2025
3.0.66 102 2/10/2025
3.0.65 103 2/10/2025
3.0.64 106 2/9/2025
3.0.63 109 2/9/2025
3.0.62 91 2/9/2025
3.0.61 100 2/9/2025
3.0.60 101 2/9/2025
3.0.59 92 2/9/2025
3.0.58 103 2/8/2025
3.0.57 107 2/8/2025
3.0.56 95 2/8/2025
3.0.55 108 2/8/2025
3.0.54 100 2/8/2025
3.0.53 108 2/8/2025
3.0.52 101 2/8/2025
3.0.51 98 2/8/2025
3.0.50 103 2/8/2025
3.0.49 114 2/8/2025
3.0.48 102 2/8/2025
3.0.47 94 2/8/2025
3.0.46 103 2/7/2025
3.0.45 104 2/7/2025
3.0.44 109 2/7/2025
3.0.43 105 2/7/2025
3.0.42 103 2/7/2025
3.0.41 103 2/7/2025
3.0.40 120 2/7/2025
3.0.39 114 2/7/2025
3.0.38 113 2/7/2025
3.0.37 108 2/7/2025
3.0.36 100 2/7/2025
3.0.35 100 2/7/2025
3.0.34 98 2/7/2025
3.0.33 112 2/7/2025
3.0.32 104 2/7/2025
3.0.31 108 2/7/2025
3.0.30 101 2/6/2025
3.0.29 105 2/6/2025
3.0.28 95 2/6/2025
3.0.27 88 2/6/2025
3.0.26 107 2/6/2025
3.0.25 100 2/5/2025
3.0.24 104 2/5/2025
3.0.23 102 2/5/2025
3.0.22 107 2/5/2025
3.0.21 104 2/5/2025
3.0.20 104 2/5/2025
3.0.19 113 2/5/2025
3.0.18 103 2/5/2025
3.0.17 98 2/5/2025
3.0.16 106 2/5/2025
3.0.15 99 2/5/2025
3.0.14 109 2/5/2025
3.0.13 96 2/5/2025
3.0.12 99 2/5/2025
3.0.11 106 2/5/2025
3.0.10 107 2/5/2025
3.0.9 103 2/5/2025
3.0.8 100 2/5/2025
3.0.7 110 2/3/2025
3.0.6 106 2/3/2025
3.0.5 105 2/3/2025
3.0.4 107 2/3/2025
3.0.3 105 2/3/2025