Soenneker.SemanticKernel.Cache 3.0.371

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