Memcache - 缓存数据始终为null

时间:2016-12-22 08:52:28

标签: c# asp.net caching memcached enyim.caching

以下是我尝试在控制台应用程序中使用Memcache

App.Config中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="enyim.com"/>
       <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection,Enyim.Caching"/>
   </configSections>

 <startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>

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

   </memcached>
  </enyim.com>
 </configuration>

C#

static void Main(string[] args)
    {
        DateTime dt = DateTime.Now;
        try
        {

            using (MemcachedClient memcache = new MemcachedClient())
            {
                string cachedTime = memcache.Get<string>("CachedTime");
                if (cachedTime == null)
                {
                    memcache.Store(Enyim.Caching.Memcached.StoreMode.Set, "CachedTime", DateTime.Now.ToString());
                 cachedTime = memcache.Get<string>("CachedTime");
                }

                Console.WriteLine(cachedTime);
                Console.WriteLine(dt);
            }   
        }
        catch (Exception ex)
        {

        }
    }
  

每次cachedDate的值为NULL

1 个答案:

答案 0 :(得分:0)

将App.Config更改为以下内容,它应该有效:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="enyim.com">
      <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </sectionGroup>
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <enyim.com>
    <memcached>
      <servers>
        <add address="127.0.0.1" port="11211" />
      </servers>
      <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
    </memcached>
  </enyim.com>
</configuration>