Soenneker.SemanticKernel.Cache 3.0.403

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