Soenneker.SemanticKernel.Cache 3.0.197

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