title: Memcache in .NET Core by EasyCaching
body: <p>I use this link to make the provider to Memcached <a href="https://easycaching.readthedocs.io/en/latest/Memcached/" rel="nofollow noreferrer">DefaultMemcachedCachingProvider</a>.</p>
  
  <p>1) I download the <code>EasyCaching.Memcached</code> via Nuget</p>
  
  <p>2) add config in <code>startup.cs</code> class</p>
  
  <pre><code> public void ConfigureServices(IServiceCollection services)
      {
          services.AddEasyCaching(option =&gt;
          {
              //use memmemcachedory cache
              option.UseMemcached(config =&gt;
              {
                  config.DBConfig.AddServer("127.0.0.1", 11211);
              });
          });
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
      }
  </code></pre>
  
  <p>and the <code>app.UseEasyCaching();</code> line in <code>Configure</code></p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          app.UseEasyCaching();
  
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseMvc();
      }
  </code></pre>
  
  <p>3) and finally call the provider</p>
  
  <pre><code>[Route("api/[controller]")]
  [ApiController]
  public class ValuesController : ControllerBase
  {
      private readonly IEasyCachingProvider _provider;
  
      public ValuesController(IEasyCachingProvider provider)
      {
          this._provider = provider;
      }
  
      // GET api/values
      [HttpGet]
      public async Task&lt;IActionResult&gt; GetAsync()
      {
          try
          {
              await _provider.RemoveAsync("demo");
  
              //Set Async  
              await this._provider.SetAsync("demo", "Hello world", TimeSpan.FromMinutes(1));
  
  
              //Get Async      
              var res = await this._provider.GetAsync&lt;string&gt;("demo");
  
              return Ok(res);
          }
          catch(Exception ex)
          {
              return StatusCode(500, ex.Message);
          }
  
      }
  }
  </code></pre>
  
  <p>in <code>exception ex</code> I take the error </p>
  
  <blockquote>
    <p>"Connection failed because the destination computer rejected the connection request 127.0.0.1:11111"</p>
  </blockquote>
  
  <p>And I don't know how fixed it, what I did wrong? I did it by tutorial</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>startup class</p>
  
  <pre><code>public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddEasyCaching(option =&gt;
          {
              //use memmemcachedory cache
              option.UseMemcached(config =&gt;
              {
                  config.DBConfig.AddServer("127.0.0.1", 11211);
              });
          });
  
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          app.UseEasyCaching();
  
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseMvc();
      }
  </code></pre>
  
  <p>My controller </p>
  
  <pre><code>public class ValuesController : ControllerBase
  {
      private readonly IEasyCachingProvider _provider;
  
      public ValuesController(IEasyCachingProvider provider)
      {
          this._provider = provider;
      }
  
      // GET api/values
      [HttpGet]
      public IActionResult Get()
      {
          try
          {
              //Set
              _provider.Set&lt;string&gt;("demo", "Hello", TimeSpan.FromMinutes(1));
  
              //Get
              var res = _provider.Get&lt;string&gt;("demo");
  
              return Ok(res);
          }
          catch(Exception ex)
          {
              return StatusCode(500, ex.Message);
          }
  
      }
  }
  </code></pre>