猿问

使用了EnyimMemcachedCore封装了一个类库

public class MemCachedHelper : ICacheHelper
    {        private IMemcachedClient memcachedClient;        public MemCachedHelper(IMemcachedClient memcachedClient)
        {            this.memcachedClient = memcachedClient;
        }        public object Get(string key)
        {            return memcachedClient.Get(key);
        }        public void Set(string key, object obj)
        {
            memcachedClient.Set(key, obj, int.MaxValue);
        }        public void Set(string key, object obj, int seconds)
        {
            memcachedClient.Set(key, obj, seconds);
        }        public void Remove(string key)
        {
            memcachedClient.Remove(key);
        }        public void RemoveAllCache()
        {
            memcachedClient.FlushAll();
        }
    }


放在类库项目中。那这个MemCachedHelper要如何使用?参数是?

MemCachedHelper cache= new MemCachedHelper (????);


繁花不似锦
浏览 767回答 1
1回答

慕工程0101907

Startup 中注册到依赖注入容器public&nbsp;void&nbsp;ConfigureServices(IServiceCollection&nbsp;services){ &nbsp;&nbsp;&nbsp;&nbsp;services.AddTransient<MemCachedHelper>(); }使用的地方通过构造函数注入public&nbsp;class&nbsp;UserService{&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;readonly&nbsp;MemCachedHelper&nbsp;_memCachedHelper;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;UserService(MemCachedHelper&nbsp;memCachedHelper)&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_memCachedHelper&nbsp;=&nbsp;memCachedHelper; &nbsp;&nbsp;&nbsp;&nbsp;} }
随时随地看视频慕课网APP
我要回答