LogIn
I don't have account.

How to configure memcached in mvc dot net core application

DevSniper

197 Views

#caching

#memcached

Memcached is an in-memory key-value store. it is a layer between applications and data storage (typically a database). Memcached mostly used to reduce the time to fetch data.

Configuring Memcached in a .NET Core application/project involves a few steps to set up the necessary dependencies and configure the caching provider.

Step 1 :- Install Required Packages

First step is to install the Memcached client library for .NET Core in your application. One of the popular library is EnyimMemcachedCore. you can install it using NuGet Package Manager Console or add it to your .csproj file:

dotnet add package EnyimMemcachedCore
OR
<ItemGroup>
     // .....................................................
    <PackageReference Include="EnyimMemcachedCore" Version="2.7.0" />
     // ....................................................
  </ItemGroup>

Note :- Update Version with latest version. For latest version visit official nuget site https://www.nuget.org/packages/EnyimMemcachedCore

Step 2 :- Configure Memcached in appsettings.json
{
//............................................
"Memcached": {
    "Servers": [
      {
        "Address": "localhost",
        "Port": 11211
      }
      // Add more servers if your setup is distributed
    ],
    "Options": {
      // Optional: Add any client options here (e.g., connection pooling, serialization settings)
    }
  }
// ........................................
}
Step 3 :- Configure Memcached Client

in your application , use any of the configuration file to configure memcached client library. Memcached client library provide many way for configuration.

using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Microsoft.Extensions.Configuration;

public class Startup
{
    // .................................................
    public void ConfigureServices(IServiceCollection services)
    {
        // Configure Memcached client using services.Configure 
        services.Configure<MemcachedOptions>(Configuration.GetSection("Memcached"));
        services.AddEnyimMemcached();
        // .......................................
    }
   //......................................................
}
OR
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Microsoft.Extensions.Configuration;

public class Startup
{
    // .................................................
    public void ConfigureServices(IServiceCollection services)
    {
        // Configure Memcached client using configuration section
        services.AddEnyimMemcached(Configuration.GetSection("Memcached"));
        // .......................................
    }
   //......................................................
}
OR
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Microsoft.Extensions.Configuration;

public class Startup
{
    // .................................................
    public void ConfigureServices(IServiceCollection services)
    {
        // Manually read and configure Memcached options
        var memcachedServers = Configuration.GetSection("Memcached:Servers").Get<List<MemcachedServerSettings>>();
        var memcachedOptions = Configuration.GetSection("Memcached:Options").Get<MemcachedClientOptions>();

        services.AddEnyimMemcached(memcachedServers, options =>
        {
            // Configure additional options if needed
            // For example:
            // options.Protocol = MemcachedProtocol.Binary;
        });
        // .......................................
    }
   //......................................................
}
Step 4 :- Using Memcached in Your Application
using Enyim.Caching.Memcached;
using System;

public class MemcachedFile
{
    private readonly IMemcachedClient _memcachedClient;

    public MemcachedFile(IMemcachedClient memcachedClient)
    {
        _memcachedClient = memcachedClient;
    }

    public string SetData(string key, string value)
    {
        var expiration = TimeSpan.FromMinutes(10);
        // Set data in Memcached
        _memcachedClient.Set(key, value, expiration);
        return true;
    }
    public string GetData(string key)
    {
        return _memcachedClient.Get<string>(key);
    }
}