C# Usage

EnyimMemcached for C#

1. Library Overview

EnyimMemcached is the mainstream official .NET client library for Memcached targeting .NET Framework/.NET, maintained by Attila Kiskó. Install it via NuGet, it connects to Memcached server using either Text or Binary protocol.

Note: The newer release of EnyimMemcached is distributed under the NuGet package Enyim.Caching

2. Environment Setup

  1. Start local Memcached service, default port: 11211
  2. Create a .NET Framework console project inside Visual Studio
  3. Search and install NuGet package: EnyimMemcached (package id: Enyim.Caching)

3. Full App.config Configuration

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>

  <!-- Register configuration section handler first -->
  <configSections>
    <sectionGroup name="enyim.com">
      <section name="memcached"
               type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </sectionGroup>
  </configSections>

  <!-- Memcached connection settings -->
  <enyim.com>
    <memcached protocol="Text">
      <!-- Memcached server endpoint -->
      <servers>
        <add address="127.0.0.1" port="11211" />
      </servers>
      <!-- Socket pool parameters -->
      <socketPool minPoolSize="10" 
                  maxPoolSize="20" 
                  connectionTimeout="00:00:10"
                  deadTimeout="00:02:00" />
    </memcached>
  </enyim.com>
</configuration>Code language: HTML, XML (xml)

Configuration explanation:

  • protocol="Text": Use Memcached text‑based protocol, can switch to Binary binary protocol
  • minPoolSize/maxPoolSize: Minimum and maximum connections for socket pool
  • address/port: Host address and port of Memcached instance

4. Usage Demo (Console App)

using System;
using Enyim.Caching;
using Enyim.Caching.Memcached;

namespace ConsoleApplication1
{
    class Program
    {
        // Singleton Memcached client (reuse globally, avoid frequent instantiation)
        private static readonly MemcachedClient uniqueInstance = new MemcachedClient();

        static void Main(string[] args)
        {
            // StoreMode.Set: Overwrite existing key, create new entry if missing
            uniqueInstance.Store(StoreMode.Set, "site", "foxdevelop.com");

            // Retrieve string cache value
            string value = uniqueInstance.Get<string>("site");
            Console.WriteLine(value);

            Console.Read();
        }
    }
}Code language: JavaScript (javascript)

Common APIs

MethodDescription
Store(StoreMode.Set, key, value)Write cache entry (overwrite)
Store(StoreMode.Add, key, value)Write only when target key does not exist
Get<T>(key)Generic cache value retrieval
Remove(key)Delete specified cache key
GetCas(key)CAS atomic read for concurrency scenarios

The traditional App.config approach works only for .NET Framework;
.NET Core/.NET 6+ prefers in‑code configuration instead of XML config files.

.NET Core/.NET6 In‑Code Configuration (No App.config)

var config = new MemcachedClientConfiguration();
config.Servers.Add(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 11211));
config.Protocol = MemcachedProtocol.Text;
var client = new MemcachedClient(config);Code language: JavaScript (javascript)

Feel free to test this on .NET Core and newer .NET releases

C# Usage

Leave a Reply

Your email address will not be published. Required fields are marked *