cnblogs / EnyimMemcachedCore

.NET Memcached client. Available on https://www.nuget.org/packages/EnyimMemcachedCore
Apache License 2.0
162 stars 45 forks source link

Please provide a way to easily instantiate a MemcachedClient #97

Closed rgelb closed 5 years ago

rgelb commented 5 years ago

At the moment, the implementation and all the examples are geared toward injecting Memcached into middleware and its usage as DI.

But imagine the following project structure. Widget.UI project (MVC) calls Widget.Business calls Widget.Services calls Widget.Repository. Let's say I want to implement distributed caching in Widget.Repository (last project in the chain). I'd have to build constructors to accept Memcached client from Widget.Business all the way down to Widget.Repository.

This would make usage of the intermediate projects from other places in code difficult to say the least.

I understand that I can create a Memcached instance via GenericHost approach. I don't think that's a good way to go because I am building out an entire host infrastructure in a class that doesn't know anything about that.

Please provide a constructor to enable something similar to the following:

var options = new MemcachedOptions {.Servers = "11.22.33.44"}; var cache = new MemcachedClient(options);

kt81 commented 5 years ago

You can instanciate MemcachedClient without DI like below.

var loggerFactory = new Microsoft.Extensions.Logging.Abstractions.NullLoggerFactory();
var configuration = new MemcachedClientConfiguration(loggerFactory, new MemcachedClientOptions
{
    Servers = new List<Server>
    {
        new Server {Address = "127.0.0.1", Port = 11211}
    },
});
var client = new MemcachedClient(loggerFactory, configuration);

await client.SetAsync("testing", "it works!!", 3600);
var cached = await client.GetAsync<string>("testing");