CacheManager memcached configuration

拥有回忆 提交于 2019-12-10 10:48:51

问题


I'm going to use CacheManager for my .net project. The problem is that I can't find any examples of CacheManager.Memcached usage.

This is how I use it:

public class GlobalCache
{
     private static ICacheManager<object> memcachedClient { get; set; }

     private static readonly object locker = new object();

     static GlobalCache()
     {
          if (memcachedClient == null)
          {
               lock (locker)
               {
                  memcachedClient = CacheFactory.Build("memcached", settings => settings.WithMemcachedCacheHandle("memcached"));
               }
          }
     }
}

Web.config:

   <configuration>
     <enyim.com>
       <memcached protocol="Binary">
         <servers>
           <add address="127.0.0.1" port="11211" />
         </servers>
       </memcached>
     </enyim.com>

     <cache name="memcached">
        <handle name="memcached"/>
     </cache>
   </configuration>

The error I have is: http://c2n.me/3hSHqvR.png - unknown section in web config.

If I remove all these sections, I have another runtime error: http://c2n.me/3hSI745.png - config error.

I tried to use settings.WithSystemRuntimeCacheHandle() instead of settings.WithMemcachedCacheHandle() and it works fine without any config sections. But in this case, my cache is cleared every time I restart my app. And what I want is - store cache in memcached storage, without any relation to my application.

So if you have some examples or small tutorial of how to use memcached with CacheManager - I will be much appreciated.

Thanks in advance!


回答1:


Regarding the error for the <cache name="memcached"> section , most likely you have not defined any section inside your configuration file with that name. At least there is no section like that coming from CacheManager nor from enyim.

Then regarding the memcached handle. The name of the handle must match the section so that CacheManager can pick up the configuration. The default is enyim.com/memcached which you have. So you could either put enyim.com/memcached or default as the name of the memcached cache handle.

Like

CacheFactory.Build("memcached", settings => settings
    .WithMemcachedCacheHandle("enyim.com/memcached"));

Let me know if this works for you.

I know this is not very well documented, yet. I might add something when I find some time ;)



来源:https://stackoverflow.com/questions/30308226/cachemanager-memcached-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!