Soenneker.SemanticKernel.Cache 3.0.189

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