Soenneker.SemanticKernel.Cache 3.0.121

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.121
                    
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.121
                    
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.121" />
                    
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.121" />
                    
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.121
                    
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.121"
                    
#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.121
                    
Install Soenneker.SemanticKernel.Cache as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.121
                    
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.400 0 5/8/2025
3.0.399 0 5/7/2025
3.0.398 36 5/6/2025
3.0.397 28 5/6/2025
3.0.396 33 5/6/2025
3.0.395 49 5/5/2025
3.0.394 51 5/5/2025
3.0.393 48 5/5/2025
3.0.392 51 5/5/2025
3.0.391 54 5/5/2025
3.0.390 50 5/5/2025
3.0.389 47 5/5/2025
3.0.388 50 5/5/2025
3.0.387 50 5/5/2025
3.0.386 50 5/5/2025
3.0.385 122 4/29/2025
3.0.384 118 4/27/2025
3.0.383 75 4/27/2025
3.0.382 66 4/26/2025
3.0.381 69 4/26/2025
3.0.380 161 4/18/2025
3.0.379 115 4/11/2025
3.0.378 156 4/9/2025
3.0.377 143 4/9/2025
3.0.376 158 4/9/2025
3.0.375 158 4/9/2025
3.0.374 154 4/8/2025
3.0.373 153 4/8/2025
3.0.372 146 4/8/2025
3.0.371 155 4/8/2025
3.0.370 153 4/8/2025
3.0.369 143 4/8/2025
3.0.368 151 4/8/2025
3.0.367 146 4/8/2025
3.0.366 146 4/8/2025
3.0.365 143 4/8/2025
3.0.364 146 4/8/2025
3.0.363 154 4/8/2025
3.0.362 149 4/8/2025
3.0.361 152 4/8/2025
3.0.360 153 4/8/2025
3.0.359 150 4/7/2025
3.0.358 137 4/7/2025
3.0.357 152 4/7/2025
3.0.356 150 4/7/2025
3.0.355 142 4/7/2025
3.0.354 144 4/7/2025
3.0.353 143 4/7/2025
3.0.352 150 4/7/2025
3.0.351 142 4/7/2025
3.0.350 149 4/7/2025
3.0.349 138 4/7/2025
3.0.348 149 4/7/2025
3.0.347 145 4/7/2025
3.0.346 144 4/7/2025
3.0.345 147 4/7/2025
3.0.344 145 4/7/2025
3.0.343 146 4/7/2025
3.0.342 150 4/6/2025
3.0.341 146 4/6/2025
3.0.340 145 4/6/2025
3.0.339 145 4/6/2025
3.0.338 142 4/6/2025
3.0.337 148 4/6/2025
3.0.336 149 4/6/2025
3.0.335 138 4/6/2025
3.0.334 121 4/6/2025
3.0.333 119 4/6/2025
3.0.332 118 4/6/2025
3.0.331 114 4/6/2025
3.0.330 132 4/6/2025
3.0.329 127 4/6/2025
3.0.328 93 4/6/2025
3.0.327 99 4/6/2025
3.0.326 87 4/6/2025
3.0.325 96 4/5/2025
3.0.324 99 4/5/2025
3.0.323 70 4/5/2025
3.0.322 69 4/5/2025
3.0.321 67 4/5/2025
3.0.320 72 4/5/2025
3.0.319 71 4/5/2025
3.0.318 79 4/5/2025
3.0.317 75 4/5/2025
3.0.316 88 4/4/2025
3.0.315 84 4/4/2025
3.0.314 88 4/4/2025
3.0.313 132 4/4/2025
3.0.312 132 4/4/2025
3.0.311 131 4/4/2025
3.0.310 148 4/4/2025
3.0.309 139 4/4/2025
3.0.308 144 4/3/2025
3.0.307 145 4/3/2025
3.0.306 136 4/2/2025
3.0.305 145 4/1/2025
3.0.304 143 4/1/2025
3.0.303 142 4/1/2025
3.0.302 138 4/1/2025
3.0.301 142 4/1/2025
3.0.300 133 4/1/2025
3.0.299 146 4/1/2025
3.0.298 136 4/1/2025
3.0.297 137 4/1/2025
3.0.296 133 4/1/2025
3.0.295 133 3/31/2025
3.0.294 146 3/31/2025
3.0.293 132 3/31/2025
3.0.292 148 3/31/2025
3.0.291 143 3/30/2025
3.0.290 138 3/29/2025
3.0.289 76 3/29/2025
3.0.288 73 3/29/2025
3.0.287 81 3/29/2025
3.0.286 79 3/29/2025
3.0.285 87 3/29/2025
3.0.284 123 3/27/2025
3.0.283 128 3/27/2025
3.0.282 125 3/27/2025
3.0.281 123 3/27/2025
3.0.280 126 3/26/2025
3.0.279 459 3/26/2025
3.0.278 459 3/26/2025
3.0.277 462 3/26/2025
3.0.276 458 3/25/2025
3.0.275 463 3/25/2025
3.0.274 460 3/25/2025
3.0.273 468 3/25/2025
3.0.272 463 3/25/2025
3.0.271 469 3/25/2025
3.0.270 480 3/25/2025
3.0.269 79 3/21/2025
3.0.268 73 3/21/2025
3.0.267 83 3/21/2025
3.0.266 102 3/21/2025
3.0.265 99 3/21/2025
3.0.264 125 3/21/2025
3.0.263 122 3/21/2025
3.0.262 135 3/20/2025
3.0.261 131 3/20/2025
3.0.260 132 3/19/2025
3.0.259 131 3/19/2025
3.0.258 130 3/18/2025
3.0.257 130 3/18/2025
3.0.256 128 3/18/2025
3.0.255 133 3/18/2025
3.0.254 135 3/18/2025
3.0.253 132 3/18/2025
3.0.252 128 3/18/2025
3.0.251 132 3/18/2025
3.0.250 62 3/15/2025
3.0.249 60 3/15/2025
3.0.248 59 3/15/2025
3.0.247 56 3/15/2025
3.0.246 57 3/15/2025
3.0.245 54 3/15/2025
3.0.244 140 3/12/2025
3.0.243 147 3/12/2025
3.0.242 145 3/12/2025
3.0.241 144 3/12/2025
3.0.240 136 3/12/2025
3.0.239 139 3/12/2025
3.0.238 142 3/12/2025
3.0.237 142 3/12/2025
3.0.236 141 3/12/2025
3.0.235 139 3/12/2025
3.0.234 140 3/12/2025
3.0.233 144 3/11/2025
3.0.232 149 3/11/2025
3.0.231 141 3/11/2025
3.0.230 150 3/11/2025
3.0.229 147 3/11/2025
3.0.228 151 3/11/2025
3.0.227 146 3/11/2025
3.0.226 145 3/11/2025
3.0.225 151 3/11/2025
3.0.224 145 3/11/2025
3.0.223 150 3/11/2025
3.0.222 147 3/11/2025
3.0.221 199 3/7/2025
3.0.220 196 3/7/2025
3.0.219 194 3/7/2025
3.0.218 196 3/7/2025
3.0.217 197 3/7/2025
3.0.216 195 3/7/2025
3.0.215 198 3/7/2025
3.0.214 194 3/7/2025
3.0.213 196 3/7/2025
3.0.212 195 3/3/2025
3.0.211 99 3/2/2025
3.0.210 103 3/2/2025
3.0.209 84 3/2/2025
3.0.208 84 3/2/2025
3.0.207 84 3/2/2025
3.0.206 85 3/2/2025
3.0.205 83 3/2/2025
3.0.204 95 3/2/2025
3.0.203 73 3/2/2025
3.0.202 83 3/2/2025
3.0.201 86 3/2/2025
3.0.200 84 3/2/2025
3.0.199 85 3/2/2025
3.0.198 90 3/1/2025
3.0.197 82 3/1/2025
3.0.196 84 3/1/2025
3.0.195 83 3/1/2025
3.0.194 83 3/1/2025
3.0.193 85 3/1/2025
3.0.192 82 3/1/2025
3.0.191 82 3/1/2025
3.0.190 78 3/1/2025
3.0.189 76 3/1/2025
3.0.188 81 3/1/2025
3.0.187 80 3/1/2025
3.0.186 88 2/28/2025
3.0.185 86 2/26/2025
3.0.184 87 2/26/2025
3.0.183 87 2/26/2025
3.0.182 91 2/26/2025
3.0.181 87 2/26/2025
3.0.180 84 2/25/2025
3.0.179 85 2/25/2025
3.0.178 90 2/25/2025
3.0.177 87 2/25/2025
3.0.176 94 2/25/2025
3.0.175 87 2/25/2025
3.0.174 83 2/25/2025
3.0.173 86 2/25/2025
3.0.172 81 2/25/2025
3.0.171 85 2/24/2025
3.0.170 86 2/24/2025
3.0.169 80 2/24/2025
3.0.168 115 2/23/2025
3.0.167 74 2/23/2025
3.0.166 85 2/23/2025
3.0.165 82 2/23/2025
3.0.164 83 2/23/2025
3.0.163 81 2/23/2025
3.0.162 87 2/23/2025
3.0.161 77 2/23/2025
3.0.160 83 2/22/2025
3.0.159 80 2/22/2025
3.0.158 87 2/22/2025
3.0.157 86 2/22/2025
3.0.156 82 2/22/2025
3.0.155 86 2/22/2025
3.0.154 82 2/22/2025
3.0.153 87 2/22/2025
3.0.152 92 2/22/2025
3.0.151 83 2/22/2025
3.0.150 88 2/22/2025
3.0.149 79 2/22/2025
3.0.148 91 2/22/2025
3.0.147 84 2/22/2025
3.0.146 90 2/22/2025
3.0.145 80 2/22/2025
3.0.144 82 2/22/2025
3.0.143 78 2/22/2025
3.0.142 85 2/22/2025
3.0.141 85 2/21/2025
3.0.140 86 2/21/2025
3.0.139 86 2/21/2025
3.0.138 87 2/21/2025
3.0.137 80 2/21/2025
3.0.136 85 2/21/2025
3.0.135 86 2/21/2025
3.0.134 93 2/20/2025
3.0.133 88 2/19/2025
3.0.132 92 2/19/2025
3.0.131 94 2/19/2025
3.0.130 85 2/19/2025
3.0.129 95 2/19/2025
3.0.128 92 2/19/2025
3.0.127 101 2/19/2025
3.0.126 89 2/19/2025
3.0.125 89 2/19/2025
3.0.124 88 2/19/2025
3.0.123 96 2/19/2025
3.0.122 91 2/18/2025
3.0.121 89 2/18/2025
3.0.120 100 2/18/2025
3.0.119 91 2/18/2025
3.0.118 94 2/18/2025
3.0.117 96 2/18/2025
3.0.116 108 2/18/2025
3.0.115 92 2/18/2025
3.0.114 93 2/16/2025
3.0.113 91 2/14/2025
3.0.112 86 2/14/2025
3.0.111 87 2/14/2025
3.0.110 89 2/14/2025
3.0.109 97 2/14/2025
3.0.108 96 2/14/2025
3.0.107 92 2/14/2025
3.0.106 100 2/14/2025
3.0.105 92 2/13/2025
3.0.104 86 2/13/2025
3.0.103 101 2/13/2025
3.0.102 82 2/13/2025
3.0.101 104 2/12/2025
3.0.100 88 2/12/2025
3.0.99 95 2/12/2025
3.0.98 100 2/12/2025
3.0.97 97 2/12/2025
3.0.96 94 2/12/2025
3.0.95 93 2/12/2025
3.0.94 88 2/12/2025
3.0.93 94 2/12/2025
3.0.92 98 2/12/2025
3.0.91 95 2/12/2025
3.0.90 96 2/12/2025
3.0.89 91 2/12/2025
3.0.88 91 2/12/2025
3.0.87 97 2/12/2025
3.0.86 89 2/12/2025
3.0.85 101 2/12/2025
3.0.84 96 2/12/2025
3.0.83 88 2/12/2025
3.0.82 89 2/11/2025
3.0.81 89 2/11/2025
3.0.80 96 2/11/2025
3.0.79 94 2/11/2025
3.0.78 100 2/11/2025
3.0.77 89 2/11/2025
3.0.76 91 2/11/2025
3.0.75 96 2/11/2025
3.0.74 93 2/11/2025
3.0.73 107 2/11/2025
3.0.72 96 2/11/2025
3.0.71 97 2/11/2025
3.0.70 95 2/10/2025
3.0.69 96 2/10/2025
3.0.68 102 2/10/2025
3.0.67 95 2/10/2025
3.0.66 90 2/10/2025
3.0.65 92 2/10/2025
3.0.64 96 2/9/2025
3.0.63 99 2/9/2025
3.0.62 83 2/9/2025
3.0.61 85 2/9/2025
3.0.60 91 2/9/2025
3.0.59 81 2/9/2025
3.0.58 95 2/8/2025
3.0.57 94 2/8/2025
3.0.56 87 2/8/2025
3.0.55 91 2/8/2025
3.0.54 94 2/8/2025
3.0.53 97 2/8/2025
3.0.52 91 2/8/2025
3.0.51 89 2/8/2025
3.0.50 95 2/8/2025
3.0.49 105 2/8/2025
3.0.48 91 2/8/2025
3.0.47 84 2/8/2025
3.0.46 91 2/7/2025
3.0.45 93 2/7/2025
3.0.44 99 2/7/2025
3.0.43 93 2/7/2025
3.0.42 95 2/7/2025
3.0.41 95 2/7/2025
3.0.40 104 2/7/2025
3.0.39 103 2/7/2025
3.0.38 104 2/7/2025
3.0.37 100 2/7/2025
3.0.36 87 2/7/2025
3.0.35 88 2/7/2025
3.0.34 88 2/7/2025
3.0.33 93 2/7/2025
3.0.32 95 2/7/2025
3.0.31 95 2/7/2025
3.0.30 90 2/6/2025
3.0.29 94 2/6/2025
3.0.28 87 2/6/2025
3.0.27 80 2/6/2025
3.0.26 99 2/6/2025
3.0.25 92 2/5/2025
3.0.24 95 2/5/2025
3.0.23 90 2/5/2025
3.0.22 94 2/5/2025
3.0.21 95 2/5/2025
3.0.20 97 2/5/2025
3.0.19 103 2/5/2025
3.0.18 95 2/5/2025
3.0.17 89 2/5/2025
3.0.16 98 2/5/2025
3.0.15 92 2/5/2025
3.0.14 94 2/5/2025
3.0.13 87 2/5/2025
3.0.12 91 2/5/2025
3.0.11 92 2/5/2025
3.0.10 95 2/5/2025
3.0.9 94 2/5/2025
3.0.8 92 2/5/2025
3.0.7 97 2/3/2025
3.0.6 94 2/3/2025
3.0.5 95 2/3/2025
3.0.4 96 2/3/2025
3.0.3 97 2/3/2025