2

我将为我的 .net 项目使用CacheManager。问题是我找不到任何 CacheManager.Memcached 用法的示例。

这就是我使用它的方式:

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"));
               }
          }
     }
}

网络配置:

   <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>

我遇到的错误是: http ://c2n.me/3hSHqvR.png - web 配置中的未知部分。

如果我删除所有这些部分,我会遇到另一个运行时错误: http ://c2n.me/3hSI745.png - 配置错误。

我尝试使用 settings.WithSystemRuntimeCacheHandle() 而不是 settings.WithMemcachedCacheHandle() 并且它可以在没有任何配置部分的情况下正常工作。但在这种情况下,每次我重新启动我的应用程序时都会清除我的缓存。我想要的是 - 将缓存存储在 memcached 存储中,与我的应用程序没有任何关系。

因此,如果您有一些关于如何将 memcached 与 CacheManager 一起使用的示例或小教程 - 我将不胜感激。

提前致谢!

4

1 回答 1

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 ;)

于 2015-05-18T23:07:16.773 回答